Avatar of Mase2k
Mase2k
Flag for United States of America asked on

Query for first name and add to powershell forms string

I am trying to figure out how to query for Active Directory Display Name and then enter it into the string below where you see (Insert first name here). This is a Powershell script which I am running to announce to users that their application is launching. Trying to personalize the popup. Everything works with the exception of the users name appearing.

Function Generate-Form {
    Add-Type -AssemblyName System.Windows.Forms    
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Session is loading . . . "
    $objForm.Size = New-Object System.Drawing.Size(400,400)
    $Icon = [system.drawing.icon]::ExtractAssociatedIcon("C:\test.ico")
    $objForm.Icon = $Icon
    $objForm.MinimizeBox = $False
    $objForm.MaximizeBox = $False
    $objForm.WindowState = "Normal"
    $objForm.SizeGripStyle = "Hide"
    $objForm.ShowInTaskbar = $False
    $objForm.StartPosition = "CenterScreen"
    $objForm.BackColor = "#DF7A1C"
    $Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular)
    $objForm.Font = $Font
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(40,40)
    $objLabel.Size = New-Object System.Drawing.Size(300,300)
    $objLabel.Text = "Hello (Insert first name here)! Please wait while your application is launching.

This box will close automatically."
    $objForm.Controls.Add($objLabel)
    $objForm.Show()| Out-Null
    Start-Sleep -Seconds 5
    $objForm.Close() | Out-Null
}
generate-form
PowershellActive Directory

Avatar of undefined
Last Comment
David Johnson, CD

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
footech

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mase2k

ASKER
You have done it again footech. I really appreciate your assistance. Worked like a charm!
David Johnson, CD

Function Generate-Form {
  Param(
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$true)]
    [String[]]
    $Name
    )

    Add-Type -AssemblyName System.Windows.Forms    
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Session is loading . . . "
    $objForm.Size = New-Object System.Drawing.Size(400,400)
    #$Icon = [system.drawing.icon]::ExtractAssociatedIcon("C:\test.ico") 
    #$objForm.Icon = $Icon
    $objForm.MinimizeBox = $False
    $objForm.MaximizeBox = $False
    $objForm.WindowState = "Normal"
    $objForm.SizeGripStyle = "Hide"
    $objForm.ShowInTaskbar = $False
    $objForm.StartPosition = "CenterScreen"
    $objForm.BackColor = "#DF7A1C"
    $Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular)
    $objForm.Font = $Font
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(40,40)
    $objLabel.Size = New-Object System.Drawing.Size(300,300)
    $objLabel.Text = "Hello " + $Name + "! Please wait while your application is launching.`n`nThis box will close automatically."
    $objForm.Controls.Add($objLabel)
    $objForm.Show()| Out-Null
    Start-Sleep -Seconds 5
    $objForm.Close() | Out-Null
}
$firstame = Get-ADUser -identity $env:username |select-object GivenName
$fn = $firstame.GivenName
generate-form -Name $fn

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes