函数执行错误:值不能为null

时间:2016-02-18 00:32:17

标签: powershell

我创建了一个PowerShell脚本,当Windows事件发生时,该脚本通过Task Scheduler触发。

该脚本应该会收到eventIDeventRecordIdchannel,并从Get-WinEvent获取事件详细信息,并通过弹出窗口显示详细信息。

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

该行

$events = Get-WinEvent -LogName 'System' -FilterXPath "<QueryList><Query Path='System'><Select >*[System[(EventRecordID=8478)]]</Select></Query></QueryList>"

工作正常但我用变量替换常量

$events = Get-WinEvent -LogName $channel -FilterXPath "<QueryList><Query Path='$channel'><Select >*[System[(EventRecordID=$eventRecordId)]]</Select></Query></QueryList>"

我收到以下错误。

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

我不确定我做错了什么。我可以看到$eventID$eventRecordId$channel的值被写入数据文件但是为什么Get-WinEvent会给出null异常

如果有人指出我正确的方向,我感激不尽

2 个答案:

答案 0 :(得分:0)

错误的最可能原因是$channel为空。你说你可以看到它不是空的,因为它被写入文件,但我持怀疑态度。也许你在以前的运行中看到了价值观。

如果您有任何机会使用西里尔语键盘,或者您从西里尔语源复制脚本,请确保$channel中的c,a和e不是西里尔语,а和е。 Powershell可以愉快地忍受它们,但$channel$сhаnnеl是两个不同的变量,即使你不能看到

$channel = 'Hello'
$сhаnnеl = 'Again'
Write-Host $channel    
Write-Host $сhаnnеl

给出这个输出:

PS C:\WINDOWS\system32> $channel = 'Hello'
PS C:\WINDOWS\system32> $сhаnnеl = 'Again'
PS C:\WINDOWS\system32> Write-Host $channel
Hello
PS C:\WINDOWS\system32> Write-Host $сhаnnеl
Again
PS C:\WINDOWS\system32>

答案 1 :(得分:0)

您遇到的问题是您正在错误地调用该函数。调用它时,在参数之间放置逗号,使它们成为一个数组,这样就可以有效地将所有三个变量传递给第一个参数。

PopUPEventInfo  $eventID, $eventRecordId, $channel

实际上被视为:

PopUPEventInfo  -EventId @($eventID, $eventRecordId, $channel) -EventRecordId $null -Channel $null

如果您只是删除逗号,命令应该按预期工作。

PopUPEventInfo  $eventID $eventRecordId $channel

或完全:

PopUPEventInfo  -EventId $eventID -EventRecordId $eventRecordId -Channel $channel