简体   繁体   English

如何在某些“ if”语句中使用Windows版本标题,以确定要检查的Windows更新?

[英]How I use the Windows version caption in some 'if' statements in order to determine which Windows updates to check?

I'm writing a deplorable script designed to basically eradicate any trace of Windows 10 from Windows 7, 8, and 8.1. 我正在编写一个可悲的脚本,旨在从Windows 7、8和8.1根除Windows 10的任何痕迹。 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. 脚本几乎完成了(我认为),但是似乎我的某些if语句中的逻辑即使变量不是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变量的行已添加到顶部:

$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. 这不是一个彻底的交易突破,因为当找不到用于其他Windows版本的更新时,代码仍会执行并给出简单的错误消息,但我想修复此错误,以保持良好的编程原则。

The Get-Hotfix cmdlet does not return a boolean value. Get-Hotfix cmdlet不返回布尔值。 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. 如果是,则Else运算符期望布尔(逻辑1或0)输入正常工作。

What you want to do is use the measure ( Measure-Object ) cmdlet like so: 您想要做的是像这样使用measure( Measure-Object )cmdlet:

$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. 这将为您提供准确的输出,以作为If语句的基础。

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. 该链接为PowerShell比较运算符提供了很好的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM