Execute commands on Remote machine using Windows PowerShell
If you are an administrator of several servers, I don’t think you want to execute PowerShell script on each server manually, especially if you have 20 or more servers. Since PowerShell 2.0 you can execute commands on remote computers using PowerShell via WinRM (Windows Remote Management) service.
To use this feature, you should turn the WinRM service on because it’s turned off by default.
You can check whether your service is running:
get-service winrm
If service has status not Running, you should allow WinRM service in PowerShell:
Enable-PSRemoting
After this press Y and press enter.
Now you can execute commands via powershell on remote machine.
For example, example below makes a copy of file on remote machine.
Invoke-Command -ComputerName anotherPCName -ScriptBlock { Copy-Item c:\file1.txt c:\file2.txt }
On remote machine there are two files after this.
Some useful code for SharePoint administrators. This code executes SharePoint 2010 stsadm.exe with keys for adding content database.
Invoke-Command -ComputerName anotherPCName -ScriptBlock { "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\stsadm.exe -o addcontentdb -url -databasename -databaseserver "}
Of course, you can execute any commands with stsadm.