2012-03-09 12 views
2

私はWindowsがカウントを返すのと同じ方法で利用可能なWindowsアップデートの数を返すPowershellスクリプトを書くのに疲れています。私が抱えている問題は、Window Updateが返すカウントに一致するように自分のカウントを取得できないということです。

例えば私のスクリプトを返すことがあります。
クリティカル回数:0
重要数:1
オプションの数:30

しかし、Windows Updateがあると言うだろう:
クリティカル数:1
重要なカウント:1
任意の数:29

Windows Updateでカウントを表示するためにWindowsが使用する基準を知っている人はいますか?
更新カウントを表示するためにWindowsが使用する基準は何ですか?

# ----- Get All Assigned updates -------- 
$UpdateSession = New-Object -ComObject "Microsoft.Update.Session" 
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher() 
$SearchResult = $UpdateSearcher.Search('IsInstalled=0') 

# ----- Matrix Results for type of updates that are needed -------- 
$critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" } 
$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" } 
$optional = $SearchResult.updates | where { ($_.MsrcSeverity -ne "Critical") -and ($_.MsrcSeverity -ne "Important") } 


答えて

3

は、わからないそれはあなたの問題を解決したりしません場合は、これを実行してみてください:
は、ここに私のコードのサンプルです。私はアクセス可能なパワーシェルを持っていません。

#Get All Assigned updates in $SearchResult 
$UpdateSession = New-Object -ComObject Microsoft.Update.Session 
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher() 
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0") 


#Matrix Results for type of updates that are needed 
$Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" } 
$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" } 
$other = $SearchResult.updates | where { $_.MsrcSeverity -eq $null } 

#Write Results 
Write-Host "total=$($SearchResult.updates.count)" 
Write-Host "critical=$($Critical.count)" 
Write-Host "important=$($Important.count)" 
Write-Host "other=$($other.count)" 
+0

マイケル - 私はあなたのコードをそのまま使用しましたが、私はクリティカル、重要、その他の理由でリターンを得ることができませんでした。検索基準と "$ _。MsrcSeverity -eq $ null"を使用してコードで使用しましたが、結果はWindowsが返したものと一致しませんでした。私のコード:C = 0、I = 0、O = 31 WindowsはC = 0、I = 1、O = 30を返します。私のコードとWindowsで異なると報告されている1つの更新プログラムはKB2600217という更新プログラムであり、重要なセクションでは表示されていますがチェックされていません。他のアイデア? – Eskimo

+0

@エスキモー申し訳ありませんが、私はこのテーマについてたくさん知りません。参考になるリンクがいくつかあります。 http://blogs.technet.com/b/jamesone/archive/2009/01/27/managing-windows-update-with-powershell.aspx http://richardspowershellblog.wordpress.com/2011/06/12/windows-updates-4-tidy-up-get-update http://blogs.technet.com/b/heyscriptingguy/archive/2012/01/20/get-windows-update-status-information- by-using-powershell.aspx –

1

これは古い投稿ですが、この問題を解決する際に私が見つけたものを他の人が役に立つと思うように共有します。

この現象は、WHERE句から1つのオブジェクトが返されたときに、PowerShellが配列/コレクションを返さなかったために発生します。下の私のスクリーンショットでは、同様のコードを使ってテストを行い、MsrcSeverityレベルごとにテストを打ちました。未分類の重大度には単一の更新が含まれていましたが、カウントを取得しようとするとPowerShellはnull値を返しました。

$ uncategorizedupdates変数を呼び出すと、コレクションではなく実際の更新が表示されます。

Screenshot of Output

、この問題を解決するために、私たちがしなければならないすべては、明示的に[配列]として私達の変数を定義して、それが唯一の単一のオブジェクトが存在する場合であっても、オブジェクトのコレクションを返します。下図のようにちょうどあなたのコードを変更....

[array]$critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" } 
[array]$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" } 
[array]$optional = $SearchResult.updates | where { ($_.MsrcSeverity -ne "Critical") -and ($_.MsrcSeverity -ne "Important") } 

私はそれもPowerShellの5ホープまだ問題であるので、これは便利であるだけでなくWindowsの10でこれを再現しております。

関連する問題