Avatar of Chris Miller
Chris MillerFlag for United States of America

asked on 

PowerShell FORM with an OK prompt

Im needing to add to this PS Logon FORM Script so I can use to show the user work Information and I want to be able to capture that they acknowledged the information. my thought is to use the OK button to write to an xlsx file their AD Account username and date. Here is the FORM that I am using.

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Information"
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Form.Icon = $Icon
$Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::Italic)
    # Font styles are: Regular, Bold, Italic, Underline, Strikeout
$Form.Font = $Font
#$Form.BackColor = "Red"

$label1 = New-Object System.Windows.Forms.Label
$label1.Location = '5, 5'
$label1.Size = '900, 700'
$label1.Text = '1. TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT                2. TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT                 3. TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT'
$Form.Controls.Add($label1)
$Form.Width = 1000
$Form.Height = 800
$Form.StartPosition = "CenterScreen"
$Form.ShowDialog()

Open in new window

PowershellVBA

Avatar of undefined
Last Comment
Chris Miller
ASKER CERTIFIED SOLUTION
Avatar of Brent Challis
Brent Challis
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Brent Challis
Brent Challis
Flag of Australia image

Here is an extended version of the code as a function that could be included in a module to make it available to all your PowerShell scripts.
<#
.SYNOPSIS
    Get-Confirmation display a dialog box with a message and asks the
    use to confirm they have read it.
.DESCRIPTION
    Get-Confirmation displays a message to a user for them to confirm
    that they have read it. They also have an option of cancelling.
    The function returns the result as either as DialogResult or
    a boolean flag.
.PARAMETER Message
   The text to be displayed to the user.
.PARAMETER Title
   The title for the dialog form.
.PARAMETER ConfirmButtonText
   The text to be displayed on the confirm button.
.PARAMETER Style
   Valid values: Regular, Bold, Italic, Underline, Strikeout
   The style to apply to the text in the message.
.PARAMETER BooleanOutput
   This swutch defines whether or not to retunr a tru/False result
    or the actual DialogResult.
.EXAMPLE
    Get-Confirmation "Confirm that you have read the message."
.EXAMPLE
    Get-Confirmation "Confirm that you have read the message." -Style Italic -BooleanOutput
#>
function Get-Confirmation
{
    [CmdletBinding(ConfirmImpact='Low')]
    Param
    (
        [String[]]$Message,
        [String]$Title = "Please Confirm",
        [String]$ConfirmButtonText = "Confirm",
        [ValidateSet("Regular","Bold","Italic","Underline","Strikeout")]
        [String]$Style = "Regular",
        [Switch]$BooleanOutput
    )
    Begin
    {
        function Process-Confirm
        {
            $Form.DialogResult = [System.Windows.Forms.DialogResult]::OK
            $Form.Close()
        }
        function Process-Cancel
        {
            $Form.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
            $Form.Close()
        }

        Add-Type -AssemblyName System.Windows.Forms
        $Form = New-Object system.Windows.Forms.Form
        $Form.Text = $Title
        $Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
        $Form.Icon = $Icon

        $fontStyle = [System.Drawing.FontStyle]::$Style

        $Font = New-Object System.Drawing.Font("Lucinda Console",12,$fontStyle)
            # Font styles are: Regular, Bold, Italic, Underline, Strikeout
        $Form.Font = $Font
        $Form.Size = New-Object System.Drawing.Size(505, 300)
        $txtMessage = New-Object System.Windows.Forms.TextBox
        $txtMessage.Multiline = $true
        $txtMessage.ReadOnly = $true

        $txtMessage.Location = '5, 5'
        $txtMessage.Size = '480, 200'
        $txtMessage.Text = $Message

        #Create the buttons
       $btnOK = New-Object System.Windows.Forms.Button
       $btnOK.Width=100
       $btnOK.Location = New-Object System.Drawing.Size(280, 210)
       $btnOK.Text = $ConfirmButtonText

        $btnCancel = New-Object System.Windows.Forms.Button
       $btnCancel.Width=100
       $btnCancel.Location = New-Object System.Drawing.Size(385, 210)
       $btnCancel.Text = "Cancel"

        $Form.Controls.Add($txtMessage)
        $Form.Controls.Add($btnOK)
        $Form.Controls.Add($btnCancel)
        $btnOK.add_click({Process-Confirm})
        $btnCancel.add_click({Process-Cancel})

        $Form.StartPosition = "CenterScreen"
}

    Process
    {
        [System.Windows.Forms.DialogResult]$confirmationStatus = $Form.ShowDialog()
        if ($BooleanOutput)
        {
            return ($confirmationStatus -eq [System.Windows.Forms.DialogResult]::OK)
        }
        else
        {
            return $confirmationStatus
        }
    }
}


Avatar of Chris Miller
Chris Miller
Flag of United States of America image

ASKER

Brent, The first example ran fine except it didnt populate the username into the confirmations.csv file
Avatar of Chris Miller
Chris Miller
Flag of United States of America image

ASKER

Brent, I fixed it.

  User = $env:UserName

thanks
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo