Publish Powershell running script does not display output Form/messagebox.

Guinch 141 Reputation points
2021-06-16T17:01:39.563+00:00

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.

Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,505 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,509 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 33,376 Reputation points
    2021-06-16T21:38:29.19+00:00

    Add the -NoExit switch to the Powershell.exe arguments. That should prevent the window from just going away and then you can see any errors.

    If that doesn't work, then publish cmd.exe to call PS and redirect stdout and stderr into a log file.

    cmd.exe /c powershell.exe  -file c:\scripts\xxxxx.ps1 1>c:\temp\ps.log 2>&1
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.