I'm writing a deplorable script designed to basically eradicate any trace of Windows 10 from Windows 7, 8, and 8.1. Pretty much a script version of this program. The script is almost finished (I think), but it seems that the logic in some of my if
statements executes even if the variable isn't true.
Here is the portion in mind, with the line that creates the $Caption
variable earlier on in the script added to the top for clarity:
$Caption = (Get-WmiObject win32_operatingsystem).caption
if ($Caption = 'Windows 8.1 Professional') {
if (Get-Hotfix -id KB3044374) {
Wusa /uninstall /kb:3044374 /quiet
Start-Sleep -s 30
Hide-WUUpdate -KBArticleID KB3044374 -Force
Start-Sleep -s 20
}
}
if ($Caption = 'Microsoft Windows 7 Professional') {
if (Get-Hotfix -id KB2952664) {
wusa /uninstall /kb:2952664 /quiet
Start-Sleep -s 30
Hide-WUUpdate -KBArticleID KB2952664 -Force
Start-Sleep -s 20
}
if (Get-Hotfix -id KB2990214) {
wusa /uninstall /kb:2990214 /quiet
Start-Sleep -s 30
Hide-WUUpdate -KBArticleID KB2990214 -Force
Start-Sleep -s 20
}
}
It's not a complete deal breaker, as the code still executes giving simple error messages when the updates intended for another version of Windows aren't found, but I'd like to fix this bug in order to maintain good programming principals.
The Get-Hotfix
cmdlet does not return a boolean value. It returns an object containing information if it exists on the target system. If, Else operators expect a boolean (logical 1 or 0) input to work correctly.
What you want to do is use the measure ( Measure-Object
) cmdlet like so:
$m = get-hotfix -id KB3124200 | measure
if ($m.Count -gt 0) { //execute code if hotfix is present // }
This will give you an accurate output to base your If statement on.
EDIT: This line:
If ($Caption = 'Microsoft Windows 7 Professional') {
should be:
If ($Caption -eq 'Microsoft Windows 7 Professional')
This link contains a good resource for PowerShell comparison operators.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.