Created
October 28, 2021 15:29
-
-
Save heyjoeway/00309c47fdb02ab47afb831442d8fa51 to your computer and use it in GitHub Desktop.
Remotely run commands in a GUI PowerShell window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ComputerName = "PC001", "PC002", "PC003" # ... | |
Invoke-GUICommand.ps1 -ComputerName $ComputerName -ScriptBlock { | |
echo "Hello World!" | |
pause | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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