PowerShell scripts may be executed regionally or remotely. On this article I’ll present how scripts may be executed remotely. It’s important whether or not the script is on the supply pc or on the goal pc. Each situations are potential. Let’s dive in.
PowerShell Script is situated on the Distant Pc
If the script is situated on the distant pc, we should use the Scriptblock parameter.
Right here is an instance:
Invoke-Command -ComputerName mb01 -ScriptBlock {C:tempscript.ps1}
This command will name the script script.ps1 which is situated on the Distant-Pc.
PowerShell Script is situated on the Native Pc
If the script is situated on the distant pc, we should use the FilePath parameter.
Invoke-Command -ComputerName mb01 -FilePath 'C:tempScript.ps1'
This command will name the script script.ps1 which is situated on the Native Pc.
Various: Set up reference to Enter-PS session
If the script is saved on the distant pc we are able to additionally use Enter-PSSession to hook up with the distant pc and at last execute the script.
Right here is an instance:
Enter-PSSession -ComputerName mb01 # subsequent, navigate to the script folder
.script.ps1 # run the script
Exit-PSSession
Hope this was useful.
Printed by