Link to home
Start Free TrialLog in
Avatar of Jason Yu
Jason YuFlag for United States of America

asked on

why the script shows "Couldn't connect to remote machine"

I have a script to remove a specific patch I installed several days ago from a list of computers. The code is as below:

function Uninstall-Hotfix {
[cmdletbinding()]
param(
$computername = $env:computername,
[string] $HotfixID
)            

$hotfixes = Get-WmiObject -ComputerName $computername -Class Win32_QuickFixEngineering | select hotfixid            

if($hotfixes -match $hotfixID) {
    $hotfixID = $HotfixID.Replace("KB","")
    Write-host "Found the hotfix KB" + $HotfixID
    Write-Host "Uninstalling the hotfix"
    $UninstallString = "cmd.exe /c wusa.exe /uninstall /KB:$hotfixID /quiet /norestart"
    ([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($UninstallString) | out-null            

    while (@(Get-Process wusa -computername $computername -ErrorAction SilentlyContinue).Count -ne 0) {
        Start-Sleep 3
        Write-Host "Waiting for update removal to finish ..."
    }
write-host "Completed the uninstallation of $hotfixID"
}
else {            

write-host "Given hotfix($hotfixID) not found"
return
}            

}



$myComputers = Get-Content "c:\PowerShell Scripts\computers_needremove.txt"
foreach ($computer in $myComputers) {
 Uninstall-HotFix -ComputerName $computer -HotfixID KB3097877
}

Open in new window


When I ran this script, it uninstalled the hotfix but in the progress of running it, it gave me error message like below, could you please help me with this:

PS C:\PowerShell Scripts> .\patch_remove.ps1
Given hotfix(KB3097877) not found
Given hotfix(KB3097877) not found
Found the hotfix KB + 3097877
Uninstalling the hotfix
Get-Process : Couldn't connect to remote machine.
At C:\PowerShell Scripts\patch_remove.ps1:18 char:14
+     while (@(Get-Process wusa -computername $computername -ErrorAction SilentlyC ...
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Process], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand

Completed the uninstallation of 3097877
Found the hotfix KB + 3097877
Uninstalling the hotfix
Get-Process : Couldn't connect to remote machine.
At C:\PowerShell Scripts\patch_remove.ps1:18 char:14
+     while (@(Get-Process wusa -computername $computername -ErrorAction SilentlyC ...
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Process], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand

Completed the uninstallation of 3097877
Found the hotfix KB + 3097877
Uninstalling the hotfix
Get-Process : Couldn't connect to remote machine.
At C:\PowerShell Scripts\patch_remove.ps1:18 char:14
+     while (@(Get-Process wusa -computername $computername -ErrorAction SilentlyC ...
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Process], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

winrm may not be enabled or running on the destination computer.
Avatar of Jason Yu

ASKER

Then How to resolve this?

What is winrm? Could I install them remotely?

thanks.
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
good answers, thank you all.