WMI Healthscript

by bfleming2 at 2012-12-27 05:37:06

Hi All, I was wondering if someone could help me figure out whats the best approach here…I need a script that can run daily against a list of servers (~400)(2003 and 2008r2) then it creates an html file fo the machines that failed a WMI check (checking root\cimv2 and root\cimv2\sms) if they failed they go in this html file listed by server’s name and OS with a total count. This html file is then emailed to a couple of people.

I have looked at a ton of scripts, have an idea what to do…But would like to hear some opinions on the best way to "Test" WMI as to how it is working. I currently use the SCCM Client Actions Tool to look for these failures, but need to automate this to run daily…

by Klaas at 2012-12-27 06:39:16
Start by collecting the servers, will you use Get-ADComputer or a .csv file or something else? Before querying for WMI always test if the server is up, because a time out in WMI will cost you a minute per server.
Ideally you would enable remoting, most of it can be done with GPO. On the 2003 servers you need to install the necessary components first, I believe WinRM2 and .NET framework 3.5.
With remoting enabled you can send Invoke-command to all servers at once to collect what you want.
by RichardSiddaway at 2013-01-03 10:13:17
How are you checking that WMI is working?
Are you trying to run a WMI command against one of the classes or just seeing if the namespace is present (which won’t tell you if WMI is corrupted).

if you want to run against ~400 servers consider making this a PowerShell Job. It can run in the background and let you continue working. With PowerShell v3 you could consider making it a workflow which gives you better parallelism for performing the work.

I’m not sure remoting helps here because you want to perform a couple of checks per machine & WMI can connect remotely itself though you do need to ensure that DCOM is enabled.
Something like this would work for you. I’ve had to use different namespaces
$servers = Get-Content -Path C:\scripts\servers.txt
$data = @()
foreach ($server in $servers){
$compdata = New-Object -TypeName PSObject -Property @{
Computer = $server
Contactable = $false
LastBootTime = ""
AllowTSConnections = $false

if (Test-Connection -ComputerName $server -Quiet -Count 1){
$compdata.Contactable = $true

$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server
$compdata.LastBootTime = $os.ConvertToDateTime($os.LastBootUpTime)

$ts = Get-WmiObject -Namespace root\cimv2\terminalservices -Class Win32_TerminalServiceSetting -ComputerName $server -Authentication PacketPrivacy
if ($ts.AllowTSConnections -eq 1){
$compdata.AllowTSConnections = $true

$data += $compdata
$data | ConvertTo-Html | Out-File -FilePath c:\scripts\report.html
Invoke-Item -Path c]

Please let me know if you need anything else