Reputation: 77
I have a wrote a powershell script to do some OS validation remotely.But when the remote server in not enabled with winrm I get below message.So how I can force winrm to enable remotely using psexec?
**Connecting to remote server xxxxx.us.oim.com failed with the following error message : The WSMan service could not launch a host process to process the given request. Make sure the WSMan provider host server and proxy are properly registered.**
I have added a command in my powershell script PsExec.exe \$host -s powershell "Enable-PSRemoting -force" (here $host will give the hostname) This is executing and at the same time I am getting below message also.
PsExec.exe : Connecting to xxxxxxxxx.us.oim.com...
At line:72 char:1
+ PsExec.exe \\$fqdn -s powershell "Enable-PSRemoting -force"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Connecting to xxxxxxxxx.us.oim.com...:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Starting PSEXESVC service on xxxxxxxxx.us.oim.com...Connecting with PsExec service on xxxxxxxxx.us.oim.com...Starting powershell on xxxxxxxxx.us.oim.com...
powershell exited on xxxxxxxxx.us.oim.com with error code 0
Is that possible to create a condition only if "WSMan service could not launch a host process to process the given request" then run command to enable winrm ! If possible please let me know how ?
Upvotes: 2
Views: 6435
Reputation: 592
Adding a more detailed version of the code Vladimir posted in the past.
Make sure to copy PsExec.exe and PsService.exe to your relevant path.
Function enableWinRM
{
try
{
# Check if the service is up and running by querying WinRM version:
$result = winrm id -r:$global:compName 2>$null
# If it's already running and nothing was done - exit with code 0:
if ($LastExitCode -eq 0)
{
Write-Host "WinRM service is already enabled on $global:compName" -ForegroundColor Green
}
# If exit code is 1, means service is not running - try to start WinRM service on remote machine using PsExec.exe:
else
{
Write-Host "Enabling WinRM service on $global:compName..." -ForegroundColor Cyan
Start-Process -Filepath ("\\"+$global:compName+"\c$\temp\PsExec.exe") -ArgumentList "\\$global:compName -s /accepteula -nobanner C:\Windows\system32\winrm.cmd qc -quiet" -NoNewWindow -ErrorAction SilentlyContinue
# Check again if the service is up and running by querying WinRM version:
$result = winrm id -r:$global:compName 2>$null
# If exit code is still 1 - means service was failed to start on first attempt - try to restart WinRM service on remote machine using PsService.exe:
if ($LastExitCode -eq 1)
{
Write-Host "WinRM service is still not running on $global:compName. trying to restart WinRM a second time..." -ForegroundColor Yellow
Start-Process -Filepath ("\\"+$global:compName+"\c$\temp\PsService.exe") -ArgumentList "\\$global:compName /accepteula -nobanner restart WinRM" -NoNewWindow -ErrorAction SilentlyContinue
# Check again if the service is up and running by querying WinRM version:
$result = winrm id -r:$global:compName 2>$null
# If the service was successfully started on second attempt and nothing was done at this point - exit with code 0:
if ($LastExitCode -eq 0)
{
Write-Host "WinRM service was successfully started on second attempt on $global:compName!" -ForegroundColor Green
}
# Exit code is still 1 and the service was failed to start after two attempts:
else
{
Write-Host "Error: WinRM service was failed to start on $global:compName" -ForegroundColor Red
}
}
# Exit code turned to 0. the service was started successfully on first attempt:
else
{
Write-Host "WinRM service was started successfully on first attempt on $global:compName!" -ForegroundColor Green
}
}
}
catch{}
}
$list = Get-Content -Path "C:\Temp\Computers List.txt"
foreach ($computer in $list)
{
enableWinRM
}
Upvotes: 0
Reputation: 655
Function enableWinRM {
$computers = Get-Content "C:\temp\computers.txt"
foreach ($computer in $computers) {
$result = winrm id -r:$computer 2> $null
if ($lastExitCode -eq 0) {
Write-Host "WinRM already enabled on" $computer "..." -ForegroundColor green
} else {
Write-Host "Enabling WinRM on" $computer "..." -ForegroundColor red
.\pstools\psexec.exe \\$computer -s C:\Windows\System32\winrm.cmd qc -quiet
if ($LastExitCode -eq 0) {
.\pstools\psservice.exe \\$computer restart WinRM
$result = winrm id -r:$computer 2>$null
if ($LastExitCode -eq 0) {Write-Host "WinRM successfully enabled!" -ForegroundColor green}
else {exit 1}
} #end of if
} #end of else
} #end of foreach
Just call this function in the end of your script and that is it.
Make sure you have pstools in the folder where your script is.
Upvotes: 2