adamknight
(ITcrackerjack)
1
Description
I wrote this script to enable the WinRM service so I could execute processes remotely using Powershell. This script uses PSexec (a part of the PSTools suite) to run the winrm qc command.
This script can be called like this from another script or the PS CLI.
.\remotely_enable_winrm.ps1 -computerName computer name
It returns an error code that can be tested for like this.
if ($LastExitCode -ne 0)
Let me know if you have any questions or suggestions!!
Source Code
Param([string]$computerName)
Function enableWinRM {
$result = winrm id -r:$global:compName 2>$null
Write-Host
if ($LastExitCode -eq 0) {
Write-Host "WinRM already enabled on" $global:compName "..." -ForegroundColor green
} else {
Write-Host "Enabling WinRM on" $global:compName "..." -ForegroundColor red
.\pstools\psexec.exe \\$global:compName -s C:\Windows\system32\winrm.cmd qc -quiet
if ($LastExitCode -eq 0) {
.\pstools\psservice.exe \\$global:compName restart WinRM
$result = winrm id -r:$global:compName 2>$null
if ($LastExitCode -eq 0) {Write-Host 'WinRM successfully enabled!' -ForegroundColor green}
else {exit 1}
}
else {exit 1}
}
}
$global:compName = $computerName
enableWinRM
exit 0
3 Spice ups
shuey
(Shuey)
2
Thanks man, this was exactly what I needed and a very quick solution for me! I’d like to figure out how to modify the code to feed it a txt file list of machines, since I need to do this to about 20 computers.
Awesome! This worked great. I just had to adjust the path for psexec.exe, but other than that it ran great.
how do I change computer Name for a remote system?
Hello Dwaynemcdonald, Try the below that changes the current name to a $newname (make sure you populate the newname variable with the new computer name earlier prior to executing the below) : (Get-WMIObject Win32_ComputerSystem -Computer ($env:COMPUTERNAME)).Rename($newname) Restart-Computer ($env:COMPUTERNAME) # lets to restart the computer to get the new name into effect. Cheers VC
cywebit
(cywebit)
6
Brilliant! Thanks very much! Works like a charm. Much appreciated!