Thursday, September 29, 2011

How to Enable Powershell Remoting using VMware invoke-vmscript

Enabling Powershell remoting using scripting can be tricky due to permissions issues. One work-around is to use the task scheduler to invoke the script locally. Another method I have used is to enable remoting using VMware’s PowerCLI—obviously this will only work for Windows guests running on VMware with functioning tools.
The script is very simple:
$guestcred=get-credential
$hostcred=get-credential
$script = 'Enable-PsRemoting –F'
$vms = get-vm | where {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -match "Microsoft Windows*" } | Sort Name
foreach($vm in $vms){invoke-vmscript -scripttext $script -vm $vm  -guestcredential $guestcred -hostcredential $hostcred}
The first two lines get the guest and host credentials that will be used to launch the script. The host credentials are usually the actual root account on the host—not the credentials for vCenter. The script that needs to be executed in each guest is stored in the $script variable.
The VMs to run the script on are then collected. I am limiting it to only Windows guests that are in a powered on state.
Finally I run the script inside each guest using a foreach loop. It is possible to pass in an array of VMs, but I have found that if you have a heavy consolidation ratio it can cause dramatic CPU spikes.
You can then test your connections using enter-pssession or invoke-command.

No comments:

Post a Comment