We help IT Professionals succeed at work.

PowerShell FORM with an OK prompt

CMILLER
CMILLER asked
on
52 Views
Last Modified: 2020-06-15
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

Comment
Watch Question

Principal: IT
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Brent ChallisPrincipal: IT
CERTIFIED EXPERT

Commented:
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
        }
    }
}


CMILLERCyber Security

Author

Commented:
Brent, The first example ran fine except it didnt populate the username into the confirmations.csv file
CMILLERCyber Security

Author

Commented:
Brent, I fixed it.

  User = $env:UserName

thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.