Query for first name and add to powershell forms string

Mase2k
Mase2k used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
Here you go.  This uses ADSI to query AD, finding a match to the logged in user.  Some extra handling would have to be introduced to make it work with local users.
Function Generate-Form {

    $adsi = ([ADSISearcher]"(&(objectCategory=person)(objectClass=user)(samaccountname=$env:username))")
    $adsi.PropertiesToLoad.Add("samaccountname")
    $adsi.PropertiesToLoad.Add("givenname")
    $name = ($adsi.FindOne()).properties.givenname

    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.

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

Open in new window

Author

Commented:
You have done it again footech. I really appreciate your assistance. Worked like a charm!
Top Expert 2016

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial