title: Function execution error: Value cannot be null
body: <p>I have created a PowerShell script that is triggered via Task Scheduler when a windows event occur. </p>
  
  <p>The script is expected to receive <code>eventID</code>, <code>eventRecordId</code> and <code>channel</code> and gets event detail from <code>Get-WinEvent</code> and display the detail via a pop up.</p>
  
  <pre><code>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 "&lt;QueryList&gt;&lt;Query Path='System'&gt;&lt;Select &gt;*[System[(EventRecordID=8478)]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;"
  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
  </code></pre>
  
  <p>The line </p>
  
  <pre><code>$events = Get-WinEvent -LogName 'System' -FilterXPath "&lt;QueryList&gt;&lt;Query Path='System'&gt;&lt;Select &gt;*[System[(EventRecordID=8478)]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;"
  </code></pre>
  
  <p>works fine but when I replace constants with variables </p>
  
  <pre><code>$events = Get-WinEvent -LogName $channel -FilterXPath "&lt;QueryList&gt;&lt;Query Path='$channel'&gt;&lt;Select &gt;*[System[(EventRecordID=$eventRecordId)]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;"
  </code></pre>
  
  <p>I get the following error.</p>
  
  <pre class="lang-none prettyprint-override"><code>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 "&lt;QueryList&gt;&lt;Query Path='S ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Get-WinEvent], ArgumentNullException
      + FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.PowerShell.Commands.GetWinEventCommand
  </code></pre>
  
  <p>I am not sure what I am doing wrong. I can see the values of <code>$eventID</code>, <code>$eventRecordId</code> and <code>$channel</code> getting written to the data file but why the Get-WinEvent is giving null exception</p>
  
  <p>I appreciate if anyone point me to the right direction</p>