Avatar of CMILLER
CMILLERFlag 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
CMILLER

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Brent Challis

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Brent Challis

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


ASKER
CMILLER

Brent, The first example ran fine except it didnt populate the username into the confirmations.csv file
ASKER
CMILLER

Brent, I fixed it.

  User = $env:UserName

thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy