################################################################### ## Description: Remotely enable WinRM on a remote machine in a very reliable and ## efficient manner. Because everything is done from the CMD window and all on one line ## the worry of breaking your connection tot he PC when the firewall goes down is gone. ## This script tells the remote PC to do everything. ## ## This script is provided as is and may be freely used and distributed so long as proper ## credit is maintained. ## ## Written by: ## Date Modified: 01-06-14 ################################################################### $pc = Read-Host "Enter hostname" $itworked = $true # Lets make sure the PC is online before we try anything $ping = new-object System.Net.NetworkInformation.Ping $Reply = $ping.send($pc) If ($Reply.Status -eq "Success") { # Ping was good so lets build the command line code that will be executed. Thanks to the command lines # && operator we can string everything together and not have to worry as much about failures or timing # things just right. Each one only executes if and after the one before it was successful. $cmd = "cmd /c powershell enable-psremoting -force -skipnetworkprofilecheck && net stop WinRM && net start WinRM && net stop MpsSvc && net start MpsSvc" Write-Host -foregroundcolor yellow "`n[Invoking WMI Command]" Try { # We'll invoke WMI to create a process and pass it the command line string we created. # I change it's error action to stop so that I can catch it later, by default it's not a terminating error $invoke = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName $pc -ErrorAction Stop -ErrorVariable $errors } Catch { Write-Host -foregroundcolor red "Unable to execute remote WMI function. Make sure Remote WMI is enabled in the remote PC's firewall." $itworked = $false } If ($itworked) { Write-Host -foregroundcolor yellow "[WMI Successfuly Invoked]" Write-Host "Process may take up to 60 seconds to complete after function ends" } } Else { Write-Host -foregroundcolor red "$pc is offline." } "`n"