Ways to execute PowerShell (PS1) scripts in remote computers
Below are ways to execute PowerShell (PS1) scripts in remote computers:
Example 1:
invoke-command -computerName MySrv1 -filepath .\test.ps1
Example 2:
Get the version of the PowerShell host running on a remote computer:
invoke-command -computername server64 -scriptblock {(get-host).version}
Example 1:
invoke-command -computerName MySrv1 -filepath .\test.ps1
Example 2:
Get the version of the PowerShell host running on a remote computer:
invoke-command -computername server64 -scriptblock {(get-host).version}
Example 3:
Get the version of the PowerShell host running on a list of remote computers (computers.txt):
PS C:\> $version = invoke-command -computername (get-content computers.txt) -scriptblock {(get-host).version}
PS C:\> $version
Example 4:
Run the Sample.ps1 script on all of the computers listed in the Servers.txt file. Using the -FilePath parameter to specify the script file has the effect that the content of the script is automatically copied into a script block and then passed to and run on each of the remote computers:
PS C:\> invoke-command -comp (get-content servers.txt) -filepath c:\scripts\sample.ps1 -argumentlist Process, Service
Alternatives method:
Invoke-Command uses WSMAN to run commands on remote machines.
An alternative to this is using the WMI method Win32_Process Create() which can be run locally or remotely over RPC
Open a notepad process on a remote computer:
Invoke-WMIMethod -Class Win32_Process -Name Create -Computername workstation64 -ArgumentList Notepad.exe
Example 4:
Run the Sample.ps1 script on all of the computers listed in the Servers.txt file. Using the -FilePath parameter to specify the script file has the effect that the content of the script is automatically copied into a script block and then passed to and run on each of the remote computers:
PS C:\> invoke-command -comp (get-content servers.txt) -filepath c:\scripts\sample.ps1 -argumentlist Process, Service
Alternatives method:
Invoke-Command uses WSMAN to run commands on remote machines.
An alternative to this is using the WMI method Win32_Process Create() which can be run locally or remotely over RPC
Open a notepad process on a remote computer:
Invoke-WMIMethod -Class Win32_Process -Name Create -Computername workstation64 -ArgumentList Notepad.exe
Comments
Post a Comment