Saturday, September 17, 2011

Enabling Powershell Remoting on Remote Computers

Powershell remoting is a powerful tool that allows you to run a powershell session on a remote computer. Enabling remoting on a single computer is simple:

Start a powershell.exe prompt (Run as administrator after XP/2003)
Type: Enable-PsRemoting -F

If you have hundreds of computers you want to enable it on it is more difficult. Psexec will hang when executing the command which prevents the standard administrator method. There are some complex scripts floating around the internet. I found the following method to be very simple:

schtasks /create /s servername /rl highest /ru SYSTEM /sc once /st 18:00 /TN EnableRemoting /TR "powershell -noprofile -command Enable-PsRemoting -F" /F /Z /V1

Change the /s servername to the actual server name
Change /st time to a time in the future (be careful if you have servers in different time zones)

After the task runs, the /Z parameter will cause it to be deleted automatically.

If you do not use the /V1 parameter when scheduling a task against a 2003 server you will receive the following error:

ERROR: The task XML contains a value which is incorrectly formatted or out of range.

To execute the schtasks command against a large number of servers, you can use a text file with the server names and a for loop:

for /f %i in (textfile.txt) do schtasks /create /s %i /rl highest /ru SYSTEM /sc once /st 18:00 /TN EnableRemoting /TR "powershell -noprofile -command Enable-PsRemoting -F" /F /Z /V1

To test your work start powershell:
invoke-command -computername remotecomputername -scriptblock {hostname}

1 comment:

  1. Pretty cool entry, I actually wrote a set of functions that do it just using WMI if you're interested.