Skip to content

Instantly share code, notes, and snippets.

@heyjoeway
Created October 28, 2021 15:29
Show Gist options
  • Save heyjoeway/00309c47fdb02ab47afb831442d8fa51 to your computer and use it in GitHub Desktop.
Save heyjoeway/00309c47fdb02ab47afb831442d8fa51 to your computer and use it in GitHub Desktop.
Remotely run commands in a GUI PowerShell window
$ComputerName = "PC001", "PC002", "PC003" # ...
Invoke-GUICommand.ps1 -ComputerName $ComputerName -ScriptBlock {
echo "Hello World!"
pause
}
param(
[string]$ScriptBlock="",
[string]$Credential="",
$ComputerName=""
)
$scriptBlockStr = $ScriptBlock.ToString()
Invoke-Command -Credential $Credential -ComputerName $ComputerName -ArgumentList $scriptBlockStr -ScriptBlock {
param($scriptBlockStr)
$taskName = "tmp"
$A = New-ScheduledTaskAction -Id $taskName -Execute "Powershell.exe" -Argument "Invoke-Command -ScriptBlock { $scriptBlockStr }";
$T= New-ScheduledTaskTrigger -Once -At (Get-Date);
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false;
Register-ScheduledTask -TaskName $taskName -Action $A -Trigger $T;
Start-ScheduledTask -TaskName $taskName;
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment