WMI Query Problem

by Sum4uandSum4u at 2012-09-08 20:04:24

I cannot figure out what the problem is here. If I run just the $.StartMode -eq "Auto", it filters fine. If try to run only the $.Started -eq "False" it still comes back with the below response.
Name : wuauserv
Started : True
StartMode : Auto

Runnng the code below, I want it to pipe all services that are set to AUTO and then show me the AUTO services that are not running. Thanks in advance.
Get-WmiObject -Query ‘select Name,StartMode,Started from Win32_Service’ -Namespace ‘Root\CIMV2’ |where { $.StartMode -eq "Auto" -and $.Started -eq "False" }
by surveyor at 2012-09-09 04:35:10
Try it with $false instead of the text "False"

Get-WmiObject -Query 'select Name,StartMode,Started from Win32_Service' -Namespace 'Root\CIMV2' |where { $.StartMode -eq "Auto" -and $.Started -eq $false }
by surveyor at 2012-09-09 04:37:16
Or a little bit faster with:
Get-WmiObject -Query "select Name,StartMode,Started from Win32_Service where StartMode = 'Auto' and Started = False" -Namespace 'Root\CIMV2'
by Sum4uandSum4u at 2012-09-11 17:49:16
Works like a charm … Thanks Jens!
by surveyor at 2012-09-11 23:43:26
Maybe you can use the following script. It’s very special for my needs, but shows how to read eventlogs from many servers in a few seconds and export to an Excel-file for filtering…
(Seems that syntaxhighlighting has a few problems…)
<#
.SYNOPSIS
Skript zur Überprüfung der Serverereignisprotokoll

.DESCRIPTION
Dieses Skript ruft von allen Servern alle Fehler und Warnungen
aus den Ereignisprotokollen SYSTEM und APPLICATIOn ab, die seit
dem letzten Abruf hinzu gekommen sind.

.EXAMPLE
& .\Get-ServerEventlogErrors.ps1

.NOTES
Freie und Hansestadt Hamburg
Landesbetrieb Geoinformation und Vermessung
LGV G 4 / IT-Betrieb
Sachsenkamp 4
20097 Hamburg

Dipl.-Ing. Jens Kalski

Tel : 040 / 428.26-5611
Fax : 040 / 428.26-5964
E-Mail : jens.kalski@gv.hamburg.de
#>

#region Funktionen
Function Remove-ComObject {
<#
.Synopsis
Releases all <__ComObject> objects in the caller scope.

.Description
Releases all <__ComObject> objects in the caller scope, except for those that are Read-Only or Constant.

.Example
Remove-ComObject -Verbose
Description
===========
Releases <__ComObject> objects in the caller scope and displays the released COM objects' variable names.

.Inputs
None

.Outputs
None

.Notes
Name: Remove-ComObject
Author: Robert Robelo
LastEdit: 01/13/2010 19:14
#>
[CmdletBinding()]
Param()
End {
Start-Sleep -Milliseconds 500
[Management.Automation.ScopedItemOptions]$scopedOpt = 'ReadOnly, Constant'
Get-Variable -Scope 1 | Where-Object {
$.Value.pstypenames -contains 'System.__ComObject' -and -not ($scopedOpt -band $.Options)
} | Remove-Variable -Scope 1 -Verbose]$PSBoundParameters['Verbose'].IsPresent)
[GC]::Collect()
}
}
#endregion Funktionen

#region Admin-Test
if (
-Not (
[Security.Principal.WindowsPrincipal] <br> [Security.Principal.WindowsIdentity]::GetCurrent&#40;&#41;<br> &#41;.IsInRole&#40;<br> [Security.Principal.WindowsBuiltInRole]&quot;Administrator&quot;<br> &#41;<br>&#41; {<br> Write-Host &quot; &quot; -BackgroundColor Red<br> Write-Host &quot; Dieses Skript benötigt Administrator-Berechtigungen! &quot; -BackgroundColor Red -ForegroundColor White<br> Write-Host &quot; &quot; -BackgroundColor Red<br> Exit<br>}<br>#endregion Admin-Test<br><br>#region Vorbelegungen<br>$DatumUhrzeit = Get-Date<br>$Ergebnisdatei = Join-Path -Path $ScriptDirectory -ChildPath &#39;Data\Get-ServerEventlogErrors.csv&#39;<br>$ExcelDatei = Join-Path -Path $ScriptDirectory -ChildPath &quot;Data\Get-ServerEventlogErrors_$NowForFiles.xlsx&quot;<br>$DateiMitLetztemZugriff = Join-Path -Path $ScriptDirectory -ChildPath &#39;Data\Get-ServerEventlogErrors_LetzterZugriff.xml&#39;<br>#endregion Vorbelegungen<br><br>#region Datum des letzten Abrufs<br>if &#40;Test-Path -LiteralPath $DateiMitLetztemZugriff&#41; {<br> [DateTime]$LetzterZugriff = Import-Clixml -Path $DateiMitLetztemZugriff<br>} else {<br> [DateTime]$LetzterZugriff = $DatumUhrzeit.AddDays&#40;-1&#41;<br>}<br>Export-Clixml -Path $DateiMitLetztemZugriff -InputObject $DatumUhrzeit<br>$LetzterZugriffWMI = [Management.ManagementDateTimeConverter]::ToDmtfDateTime&#40;$LetzterZugriff&#41;<br>#endregion Datum des letzten Abrufs<br><br>#region Server-Eventlogeinträge seit letztem Abruf abrufen<br># Liste der Servernamen mit Test auf Erreichbarkeit<br>$Serverliste = New-Object System.Collections.ArrayList<br>Get-LGVServerNames | ForEach-Object { $Serverliste.Add&#40;$_&#41; | Out-Null }<br>if &#40;$Serverliste.Count&#41; {<br> $i = 0<br> do {<br> if &#40;Test-Connection -ComputerName $Serverliste[$i] -Count 1 -Quiet&#41; {<br> $i = $i + 1<br> } else {<br> Write-Host &#40;&quot;Server {0} ist nicht erreichbar und wird aus der Serverliste entfernt.&quot; -f $Serverliste[$i]&#41;<br> $Serverliste.RemoveAt&#40;$i&#41;<br> }<br> } while &#40;$i -lt $Serverliste.Count&#41;<br>}<br><br># Ereignisprotokolleinträge von allen Servern abrufen<br>$Job = Get-WmiObject -ComputerName $Serverliste -AsJob -Query @&quot;<br>SELECT<br> ComputerName,Logfile,SourceName,Type,EventCode,TimeGenerated,User,Message<br>FROM<br> Win32_NTLogEvent<br>WHERE<br> &#40;<br> &#40;Logfile = &#39;Application&#39;&#41;<br> or<br> &#40;Logfile = &#39;System&#39;&#41;<br> &#41;<br>and<br> &#40;<br> &#40;Type = &#39;Error&#39;&#41;<br> or<br> &#40;Type = &#39;Warning&#39;&#41;<br> &#41;<br>and<br> TimeGenerated &gt; &#39;$LetzterZugriffWMI&#39;<br>and<br> not &#40; &#40;Type = &#39;Error&#39;&#41; and &#40;SourceName = &#39;Ntfs&#39;&#41; and &#40;EventCode = 137&#41; &#41;<br>and<br> not &#40; &#40;Type = &#39;Warning&#39;&#41; and &#40;SourceName = &#39;Ntfs&#39;&#41; and &#40;EventCode = 57&#41; &#41;<br>and<br> not &#40; &#40;Type = &#39;Warning&#39;&#41; and &#40;SourceName = &#39;McLogEvent&#39;&#41; and &#40;EventCode = 258&#41; &#41;<br>and<br> not &#40; &#40;Logfile = &#39;Application&#39;&#41; and &#40;Type = &#39;Warning&#39;&#41; and &#40;SourceName = &#39;Microsoft-Windows-User Profiles Service&#39;&#41; and &#40;EventCode = 1530&#41; &#41;<br>&quot;@<br>Wait-Job -Job $Job | Out-Null<br>$Ergebnis = Receive-Job -Job $Job<br>Remove-Job -Job $Job<br>#endregion Server-Eventlogeinträge seit letztem Abruf abrufen<br><br>#region Wenn es Daten gibt ---&gt; Umwandlung in Excel-Arbeitsmappe ---&gt; Aufruf der Excel-Arbeitsmappe<br>if &#40;$Ergebnis&#41; {<br> #region Umwandlung der Ergebnisdaten in CSV-Datei zum schnelleren Import in Excel<br> $Ergebnis = $Ergebnis |<br> Sort-Object -Property TimeGenerated -Descending |<br> Select-Object
-Property <br> ComputerName,<br> Logfile,<br> SourceName,<br> Type,<br> EventCode,<br> @{Label=&#39;Zeit&#39;;Expression={[Management.ManagementDateTimeConverter]::ToDateTime&#40;$_.TimeGenerated&#41;}},<br> User,<br> @{Label=&#39;Ereignismeldung&#39;;Expression={&#40;$_.Message -replace &quot;n",'/n') -replace "/n/n","/n"}} # Umwandlung von Zeilenumbrüchen für CSV-Export
Write-Host ("Insgesamt wurden in den Ereignisprotokollen seit {0:dd.MM.yyyy HH:mm:ss} auf {1} Servern {2} Fehler und/oder Warnungen gefunden." -f $LetzterZugriff,$Serverliste.Count,$Ergebnis.Count)
$Ergebnis | Export-Csv -Path $Ergebnisdatei -Encoding UTF8 -NoTypeInformation -UseCulture
#endregion Umwandlung der Ergebnisdaten in CSV-Datei zum schnelleren Import in Excel

#region Umwandlung der CSV-Datei in eine Excel-Arbeitsmappe
# Excel öffnen
$Excel = New-Object -ComObject Excel.Application
# Excel anzeigen
$Excel.Visible = $true
# Mögliche Fehlermeldungen und Frage-Popupfenster unterdrücken
$Excel.DisplayAlerts = $false
# CSV-Datei in Excel öffnen
$Arbeitsmappe = $Excel.Workbooks.Open($Ergebnisdatei)
If ($Arbeitsmappe) {
If ($Arbeitsmappe.ReadOnly) {
Write-Host "$Ergebnisdatei ist schreibgeschützt." -ForegroundColor Red
Exit
} else {
# Importiertes Tabellenblatt auswähen
$Tabellenblatt = $Arbeitsmappe.WorkSheets.Item("Get-ServerEventlogErrors")
# Gesamte importierte Daten in Tabellenbereich umwandeln
$NeueTabelle = $Tabellenblatt.ListObjects.Add([Microsoft.Office.Interop.Excel.XlListObjectSourceType]::xlSrcRange,$Tabellenblatt.Range($Tabellenblatt.Cells.Item(1,1),$Tabellenblatt.Cells.Item($Ergebnis.Count + 1,8)),$false,[Microsoft.Office.Interop.Excel.XlYesNoGuess]::xlYes,$Tabellenblatt.Cells.Item(1,1))

# Umgewandelte Zeilenumbrüche aus den Originaldaten wiederherstellen
$Tabellenblatt.Columns.Item("H]]::xlByRows,$false,$false,$false) | Out-Null
# Alle Spalten auf optimale Breite einstellen
$Tabellenblatt.Columns.Item("A:H").EntireColumn.AutoFit() | Out-Null
# In der Meldungsspalte den Zeilenumbruch aktivieren, für den Fall dass die Daten noch breiter sind als die maximale Breite von 255 Zeichen
$Tabellenblatt.Columns.Item("H:H").WrapText = $true

# Gesamten Tabellenbereich auswählen
$TabellenAuswahl = $Tabellenblatt.Range($Tabellenblatt.Cells.Item(1,1),$Tabellenblatt.Cells.Item($Ergebnis.Count + 1,8))
$TabellenAuswahl.Select() | Out-Null
# … und die Daten darin OBEN positionieren
$TabellenAuswahl.VerticalAlignment = [Microsoft.Office.Interop.Excel.XlVAlign]::xlVAlignTop
}
}
# Arbeitsmappe als Excel-Arbeitsmappe im Standardformat abspeichern
$Arbeitsmappe.SaveAs($ExcelDatei,[Microsoft.Office.Interop.Excel.XlFileFormat]]]::xlUserResolution,$true,$null,$null,$null) | Out-Null
# Excel beenden und komplett aus dem Speicher entfernen
$Excel.Quit()
Remove-Variable -Name Excel -Force
[GC]::Collect()
Remove-ComObject
#endregion Umwandlung der CSV-Datei in eine Excel-Arbeitsmappe

# CSV-Datei löschen
Remove-Item -LiteralPath "$Ergebnisdatei"
# Excel-Arbeitsmappe aufrufen
Invoke-Item -LiteralPath "$ExcelDatei"
} else {
Write-Host ("Seit {0:dd.MM.yyyy HH:mm:ss} wurden auf {1} Servern keine Fehler oder Warnungen gefunden." -f $LetzterZugriff,$Serverliste.Count)
}
#endregion Wenn es Daten gibt —> Umwandlung in Excel-Arbeitsmappe —> Aufruf der Excel-Arbeitsmappe