Reputation: 645
I am pulling in a text file that was previously created that simply lists the KB numbers for all currently installed Windows Updates. I am trying to pull this file in and call wusa.exe
and pass it the KB number to perform an uninstallation.
$a = Get-Content c:\hotfixid.txt
foreach ($kb in $a) {
$command = 'cmd /c wusa.exe /uninstall /kb:' + $kb.Trim().Substring(2) + ' /quiet /norestart'
#Write-Host $command
Write-Host "Currently uninstalling $kb"
Invoke-Expression -Command:$command
}
If I use
Write-Host $command
and copy that directly to a run dialog box in Windows, it completes successfully.
What happens when I run it in a PowerShell script is that it only outputs the Write-Host portions one after the other in about 2 seconds. I do not see any command windows opening and I don't see it actually DOING anything. I am running the PowerShell script 'As Administrator' with an unrestricted execution policy. I have also tried adding 'runas' to the $command
to call the CMD window each time with administrative privileges and it made no difference. Calling it via Invoke-Command
as well makes no difference.
Upvotes: 0
Views: 3230
Reputation: 8019
PowerShell can run most commands directly w/o too much trouble.
Using Invoke-Expression
just complicates matters, as does Invoke-Command
or Start-Process
, because you need to get the quoting right, and pass the arguments in a slightly unnatural way.
You can even skip running cmd.exe
most of the time.
Try the following:
wusa.exe /uninstall "/kb:$($kb.Trim().Substring(2))" /quiet /norestart
Upvotes: 1