I have created a PowerShell script that is triggered via Task Scheduler when a windows event occur.
The script is expected to receive eventID
, eventRecordId
and channel
and gets event detail from Get-WinEvent
and display the detail via a pop up.
Param( $eventID, $eventRecordId, $channel)
#$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\temp\output.txt -append
function PopUPEventInfo ( $eventID, $eventRecordId, $channel)
{
Add-Content -Value $eventID -Path C:\users\CFNLocalAdmin\data.txt
Add-Content -Value $eventRecordId -Path C:\users\CFNLocalAdmin\data.txt
Add-Content -Value $channel -Path C:\users\CFNLocalAdmin\data.txt
if($eventID -eq 7036)
{
$events = Get-WinEvent -LogName 'System' -FilterXPath "<QueryList><Query Path='System'><Select >*[System[(EventRecordID=8478)]]</Select></Query></QueryList>"
foreach($event in $events)
{
$eventParams=$event.ToXml()
$eventXml = [xml]$event.ToXml()
#Write-Host "Attempting parsing xml event"
$SysTime=$eventXml.Event.System.TimeCreated.SystemTime
#Write-Host $SysTime
$ProviderName=$eventXml.Event.System.Provider.Name
#Write-Host $ProviderName
$ServiceName=""
$ServiceStatus=""
$ServiceName= $eventXml.Event.EventData.Data[0].'#text'
$ServiceStatus=$eventXml.Event.EventData.Data[1].'#text'
$popipObj = New-Object -ComObject wscript.shell
$popipObj.popup("RecordID: "+$eventRecordId +", Channel :"+$channel+"Event Timestamp: "+$ServiceName +": "+$ServiceStatus)
}
}
}
PopUPEventInfo $eventID, $eventRecordId, $channel
The line
$events = Get-WinEvent -LogName 'System' -FilterXPath "<QueryList><Query Path='System'><Select >*[System[(EventRecordID=8478)]]</Select></Query></QueryList>"
works fine but when I replace constants with variables
$events = Get-WinEvent -LogName $channel -FilterXPath "<QueryList><Query Path='$channel'><Select >*[System[(EventRecordID=$eventRecordId)]]</Select></Query></QueryList>"
I get the following error.
TerminatingError(Get-WinEvent): "Value cannot be null.
Parameter name: collection"
Get-WinEvent : Value cannot be null.
Parameter name: collection
At C:\Users\CFNLocalAdmin\test.ps1:26 char:11
+ $events = Get-WinEvent -LogName $channel -FilterXPath "<QueryList><Query Path='S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-WinEvent], ArgumentNullException
+ FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.PowerShell.Commands.GetWinEventCommand
I am not sure what I am doing wrong. I can see the values of $eventID
, $eventRecordId
and $channel
getting written to the data file but why the Get-WinEvent is giving null exception
I appreciate if anyone point me to the right direction