I would like to publish PowerShell with RequiredCommandLine set to run a script.
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
$username = $env:username
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
$display = Get-ADUser $username -Properties Name, PasswordLastSet, PasswordExpired, Lockedout, LastLogonDate |
Select Name, PasswordLastSet, PasswordExpired, Lockedout, LastLogonDate, @{n='PasswordWillExpiry';e={$_.PasswordLastSet.AddDays($maxPasswordAge)}} |
Out-String
$GroupMem = Get-ADPrincipalGroupMembership -Identity $env:username | Select-Object SamAccountName | Format-Table -hidetableheaders | Out-String
$longest = 1
$display.Split("`n") | % {
if ($_.Length -gt $longest) {
$longest = $_.Length
}
}
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Account info for user: $username"
$Form.Width = $longest * 8
$Form.Height = 500
$Form.AutoSizeMode = 'GrowAndShrink'
$Form.StartPosition = 'CenterScreen'
# Build Label
$Label = New-Object System.Windows.Forms.Label
$Label.Text = " The account details for $username : $display The current group membership of $username : `r`n $GroupMem"
$Label.Font = New-Object System.Drawing.Font('Consolas', 9)
$Label.AutoSize = $true
$label.Left = 10
$label.Top = 50
# Build OK Button
$button = New-Object System.Windows.Forms.Button
$button.Top = 10
$button.Left = 10
#$button.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
#$button.Top = 400
$button.width = 100
$button.Text = 'OK'
$button.Add_Click({$form.Close()})
# Use Enter/Esc key
$Form.KeyPreview = $True
$Form.Add_KeyDown({
if ($_.KeyCode -eq 'Enter') {
$Form.Close()
}
})
$Form.Add_KeyDown({
if ($_.KeyCode -eq 'Escape') {
$Form.Close()
}
})
# Add Controls to Form
$Form.Controls.Add($button)
$form.controls.Add($label)
[void]$Form.ShowDialog()
This script will display a Form (messagebox) displaying the current user's account details. The script functions as expected then run on the server. However, when run as a published app a PowerShell window opens briefly and then disappears. No form/messagebox appears.
What is it about how this kind of script runs on RDS that causes the Form/Messagebox not to appear?
And is there any way to get it to work as desired?
Thanks in advance for your help.