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
Jason YuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
winrm may not be enabled or running on the destination computer.
0
Jason YuAuthor Commented:
Then How to resolve this?

What is winrm? Could I install them remotely?

thanks.
0
David Johnson, CD, MVPOwnerCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
Get-Process should use DCOM to communicate with the machine specified by the -computername parameter.  But then I don't know why it would encounter an error when the previous WMI commands (line 8 and 15) run without error.

I would test running Get-Process commands to the remote machine outside of that script and see if you ever get the error.  You may want to try using a Get-WmiObject command with the Win32_Process class instead of Get-Process.
0
Jason YuAuthor Commented:
good answers, thank you all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.