Executing GUI Form on a remote Computer


Please, I tried executing a custom GUI Form script on a remote desktop. But when ever I tried doing that i got the error message below. What could be best way to approach this.

C:\Powershell\scripts> Invoke-Command -ComputerName NGMXL9293LFM -ScriptBlock {Get-UCForm.ps1}

The term 'Get-UCForm.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
+ CategoryInfo : ObjectNotFound: (Get-UCForm.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : NGMXL1793LMM

<#---------------------------------------------------- code line Below ---------------------------------------------------------------------#>

function Get-UCform {
# init powershell GUI
Add-Type -AssemblyName System.windows.forms

#Create a new form
$UCInformationform = New-Object System.Windows.Forms.Form #Creating an object of the form.
$UCInformationform.ClientSize = '500, 300' #The form size height and widht
$UCInformationform.Text = 'LazyAdmin - Powershell GUI Example' #Main text at the top of the Form
$UCInformationform.BackColor = '#ffffff' #Defining the background color
<#------------- Content of the form Goes Below ----------------------#>

$title = New-Object System.Windows.Forms.Label #Creating an object of my title
$title.Text = 'Technical User Support'
$title.AutoSize = $true

#Defining the Min and Max with of the title form
$title.Width = 25
$title.Height = 10

#Position of the Element
$title.location = New-object system.Drawing.Point(50,50)

#Define the font type and size
$title.font = 'Microsoft Sans Serif, 13'

#Other elements
$Description = New-Object System.Windows.Forms.Label
$Description.Text = "Add a New construction site Printer"
$Description.Width = 450
$Description.Height = 50
$Description.AutoSize = $false
$Description.Location = New-Object System.Drawing.Point(50,50)
$Description.Font = 'Microsoft sans serif, 10'

#Using a Dropdown List
$Printertype = New-Object System.Windows.Forms.ComboBox
$Printertype.text = ""
$Printertype.Width = 170
$Printertype.AutoSize = $true

#add the items in the dropdown list
@('Canon','Hp') | ForEach-Object {[void] $Printertype.items.Add($_)}

#Select the Default Value
$Printertype.selectedIndex = 0
$Printertype.Location = New-Object System.Drawing.Point(20,210)
$Printertype.Font = 'Microsoft Sans Serif, 10'

#add the Element to the form
$UCInformationform.Controls.AddRange(@($title, $Description,$Printertype))

#Display The form



Instead of trying to send your script in the -ScriptBlock {} parameter, what you want to do is send it with -FilePath, like this:

Invoke-Command -ComputerName NGMXL9293LFM -FilePath C:\path_to_your_script\Get-UCForm.ps1

Replace “path_to_your_script” with whatever the correct path to the script is on your local machine, and Invoke-Command will send it to the specified computer and execute it. Here is some further reading: Use PowerShell Invoke-Command to run scripts on remote computers.

While trying the suggestion above, i got the below error message.


C:\Powershell\scripts> Invoke-Command -ComputerName MX623693LFM -FilePath .\Get-UCForm.ps1

Exception calling “ShowDialog” with “0” argument(s): “Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the
ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.”

  • CategoryInfo : NotSpecified: (:slight_smile: [], MethodInvocationException
  • FullyQualifiedErrorId : InvalidOperationException

If you’re trying to open a GUI on a remote session, there are certain security settings that restrict or prevent that. You for sure need to execute it as the user that has the active session. An alternate approach to just using Invoke-Command is to use the task scheduler to run as the user. I’ve personally just targeted a group like “Users” to target without knowing which user.

The other part of what you’re trying you need to consider is the remote system has to know/have access to the script or function. You specify just a filename to the remote computer. Depending on where that profile loads by default, there would need to exist this file. In your C:\Powershell\Scripts folder this file works fine. If this file exists on the remote PC, then simply change to the full path

Invoke-Command -ComputerName NGMXL9293LFM -ScriptBlock {c:\Powershell\Scripts\Get-UCForm.ps1}

If it doesn’t exist and you don’t intend to put a copy there, then you need to provide that definition to the remote runspace in some form or fashion. One way is to dot source the script but if you’re already invoking a remote command, then reaching out to a third host is subject to the double hop issue. You could simply provide the definition in the scriptblock

Invoke-Command -ComputerName NGMXL9293LFM -ScriptBlock {
    function Test-Function {
        write-host Hello from $env:COMPUTERNAME -ForegroundColor Cyan