get-hotfix无法在Powershell v1上运行....需要替代方案

时间:2011-11-11 16:57:04

标签: powershell

我有以下代码执行各种操作并检查修补程序信息。我试过Windows Server 2008 SP2服务器,但没有安装Powershell v2。因此,我现在不需要通过我的遗产并修复此问题(有超过3000台2008 SP2服务器) - 我需要更改我的代码。

我知道Win32_quickfixengineering可以做同样的事情但不太确定我的代码应该改变什么 - 请帮助:

$computerdel = gc env:computername

$t = "C:\buildlog\$(gc env:computername).log"
#$e = Test-Path $t
rm $t -ErrorAction SilentlyContinue

#if ( $e -eq $true ) { rm $t }
#else { Write-Host "Shortcut does not exist." }


$Logfile = "C:\buildlog\$(gc env:computername).log"

Function LogWrite
{
   Param ([string]$logstring)

   Add-content $Logfile -value $logstring
}

LogWrite -----------------------------------------------------------------------------------------------------------------------------------------------

$computer = gc env:computername
$Date = Get-Date
$ExecutionpolicySET = Set-ExecutionPolicy Unrestricted
$OSVersion = Get-WmiObject Win32_operatingsystem

LogWrite "Script has been run on $Date - This is Servers Local Time"
LogWrite $computer
LogWrite "Server type: "
LogWrite $OSVersion.name

$onetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMajorPart).tostring()
$twotcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMinorPart).tostring() 
$threetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductBuildPart).tostring() 
$fourtcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductPrivatePart).tostring() 

$onedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMajorPart).tostring() 
$twodfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMinorPart).tostring() 
$threedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductBuildPart).tostring() 
$fourdfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductPrivatePart).tostring()

$hotfix1 = Get-HotFix -Id KB2450944 -ErrorAction SilentlyContinue 
$hotfix2 = Get-HotFix -Id KB2582284 -ErrorAction SilentlyContinue
$hotfix3 = Get-HotFix -Id KB979808 -ErrorAction SilentlyContinue
$hotfix4 = Get-HotFix -Id KB2121690 -ErrorAction SilentlyContinue
$hotfix5 = Get-HotFix -Id KB973776 -ErrorAction SilentlyContinue
$hotfix6 = Get-HotFix -Id KB953325 -ErrorAction SilentlyContinue
$hotfix7 = Get-HotFix -Id KB977357 -ErrorAction SilentlyContinue
$hotfix8 = Get-HotFix -Id KB2505348 -ErrorAction SilentlyContinue
$hotfix9 = Get-HotFix -Id KB2285835 -ErrorAction SilentlyContinue
$hotfix10 = Get-HotFix -Id KB976655 -ErrorAction SilentlyContinue
$hotfix11 = Get-HotFix -Id KB979564 -ErrorAction SilentlyContinue
$hotfix12 = Get-HotFix -Id KB962969 -ErrorAction SilentlyContinue

LogWrite

    If ($hotfix1) {
        LogWrite "Hotfix KB2450944 is installed - This is DFSRS.exe Upgrade Hotfix" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB2450944 is NOT installed - Please ensure you install this hotfix - This is DFSRS.exe Upgrade Hotfix" -ForegroundColor "red"
    }
    LogWrite "DFSRS.exe Version on $computer is: ""$onedfsr.$twodfsr.$threedfsr.$fourdfsr"" "

LogWrite

    If ($hotfix4) {
        LogWrite "Hotfix KB2121690 is installed - This is 20 CPU Patch" -BackgroundColor Green -ForegroundColor Black
    }
    else {
    LogWrite "Hotfix KB2121690 is NOT installed - This is 20 CPU Patch" -ForegroundColor "red"
}

LogWrite

If ($OSVersion.version -eq "6.0.6002") 
{
    If ($hotfix5) {
        LogWrite "Hotfix KB973776 is installed - This is Robocopy Upgrade Hotfix" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB973776 is NOT installed - This is Robocopy Upgrade Hotfix" -ForegroundColor "red"
    }

    If ($hotfix6) {
        LogWrite "Hotfix KB953325 is installed - Resolves Paged Pool Exhusion Issue" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB953325 is NOT installed - Resolves Paged Pool Exhusion Issue" -ForegroundColor "red"
    }
    If ($hotfix12) {
        LogWrite "Hotfix KB962969 is installed - Resolves Command Line error on DFSRadmin.exe" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB962969 is NOT installed - Resolves Command Line error on DFSRadmin.exe" -ForegroundColor "red"
    }

}

ElseIf ($OSVersion.version -eq "6.1.7600")
{

    If ($hotfix2) {
        LogWrite "Hotfix KB2582284 is installed - This is TCPIP.sys Upgrade Hotfix" -BackgroundColor Green -ForegroundColor Black
        }
    else {
        LogWrite "Hotfix KB2582284 is NOT installed - Please ensure you install this hotfix - This is DFSRS.exe Upgrade Hotfix" -ForegroundColor "red"
        }

    LogWrite "TCPIP.sys Version on $computer is: ""$onetcp.$twotcp.$threetcp.$fourtcp"" "

LogWrite

    If ($hotfix3) {
        LogWrite "Hotfix KB979808 is installed - This is Robocopy R2 Pre-Seeding Upgrade" -BackgroundColor Green -ForegroundColor Black
        }
    else {
        LogWrite "Hotfix KB979808 is NOT installed - This is Robocopy R2 Pre-Seeding Upgrade" -ForegroundColor "red"
    }
LogWrite
    If ($hotfix7) {
        LogWrite "Hotfix KB977357 is installed - Resolves Memory Leak on WMI" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB977357 is NOT installed - Resolves Memory Leak on WMI" -ForegroundColor "red"
    }
LogWrite
    If ($hotfix8) {
        LogWrite "Hotfix KB2505348 is installed - Resolves Performance issues on WMI" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB2505348 is NOT installed - Resolves Performance issues on WMI" -ForegroundColor "red"
    }
LogWrite
If ($hotfix10) {
        LogWrite "Hotfix KB976655 is installed - Update for dfsro.dll" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB976655 is NOT installed - Update for dfsro.dll" -ForegroundColor "red"
    }
LogWrite
If ($hotfix11) {
        LogWrite "Hotfix KB979564 is installed - Dfsrclus.dll update" -BackgroundColor Green -ForegroundColor Black
    }
    else {
        LogWrite "Hotfix KB979564 is NOT installed - Dfsrclus.dll update" -ForegroundColor "red"
    }


LogWrite -----------------------------------------------------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:2)

使用-Id参数调用时,Get-Hotfix cmdlet为该参数中的每个id运行wmi查询,并在wmi查询的where子句中使用该id。

当找不到符合条件的修补程序时,Get-Hotfix cmdlet将引发错误。要取消显示错误消息,您可以添加-ErrorAction SilentlyContinue

使用Get-WmiObject -query运行wmi查询时,如果找不到符合查询条件的对象,则不会引发任何错误。

这意味着以下两个语句是等效的(第二个语句也适用于Powershell v1):

Get-HotFix -Id KB2450944 -ErrorAction SilentlyContinue
Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB2450944'"

在您的代码中,您应该替换此代码块:

$hotfix1 = Get-HotFix -Id KB2450944 -ErrorAction SilentlyContinue 
$hotfix2 = Get-HotFix -Id KB2582284 -ErrorAction SilentlyContinue
$hotfix3 = Get-HotFix -Id KB979808 -ErrorAction SilentlyContinue
$hotfix4 = Get-HotFix -Id KB2121690 -ErrorAction SilentlyContinue
$hotfix5 = Get-HotFix -Id KB973776 -ErrorAction SilentlyContinue
$hotfix6 = Get-HotFix -Id KB953325 -ErrorAction SilentlyContinue
$hotfix7 = Get-HotFix -Id KB977357 -ErrorAction SilentlyContinue
$hotfix8 = Get-HotFix -Id KB2505348 -ErrorAction SilentlyContinue
$hotfix9 = Get-HotFix -Id KB2285835 -ErrorAction SilentlyContinue
$hotfix10 = Get-HotFix -Id KB976655 -ErrorAction SilentlyContinue
$hotfix11 = Get-HotFix -Id KB979564 -ErrorAction SilentlyContinue
$hotfix12 = Get-HotFix -Id KB962969 -ErrorAction SilentlyContinue

使用此代码块:

$hotfix1 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB2450944'" 
$hotfix2 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB2582284'"
$hotfix3 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB979808'"
$hotfix4 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB2121690'"
$hotfix5 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB973776'"
$hotfix6 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB953325'"
$hotfix7 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB977357'"
$hotfix8 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB2505348'"
$hotfix9 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB2285835'"
$hotfix10 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB976655'"
$hotfix11 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB979564'"
$hotfix12 = Get-WmiObject -Query "Select * from Win32_QuickFixEngineering where HotFixID= 'KB962969'"