[英]Powershell Object reference not set to an instance of an object
The below loop forms part of a Powershell script that loops through Windows Tasks Schedules and then send an email with the statue and duration etc. However, when it executes the below loop I then receive the erorr: 下面的循环是Powershell脚本的一部分,该脚本循环遍历Windows Tasks Schedules,然后发送包含状态和持续时间等的电子邮件。但是,当它执行下面的循环时,我会收到erorr:
New-TimeSpan : Cannot bind parameter 'Start' to the target. Exception setting "Start": "Object reference not set to an instance of an object."
This is the loop: 这是循环:
foreach ($Task in $Tasks){
switch -Regex ($Task){
{$DailyTasks -contains $Task}{
$TaskRunTime = (Get-ScheduledTaskInfo "$Task").LastRunTime
$Difference = (New-TimeSpan -Start $TaskRunTime).TotalHours
$IntervalCheck = 12
switch -Regex ($Difference){
{($Difference -gt "$IntervalCheck")}{
$Status = "BAD"
$EmailTemp = @"
<tr>
<td class="colorm">$Task</td>
<td class="colorr">$Status</td>
</tr>
"@
}
{$Difference -lt "$IntervalCheck"}{
$Status = "OK"
$EmailTemp = @"
<tr>
<td class="colorm">$Task</td>
<td>$Status</td>
</tr>
What am I missing? 我想念什么?
it's possible your task has never run before, so $TaskRunTime is $null? 您的任务可能从未执行过,所以$ TaskRunTime是$ null吗? In which case you need to handle a possible null value.
在这种情况下,您需要处理可能的空值。
I'm not sure if your $Task is a task object or just a string.... if it's just a string you will have to run to obtain last run time: 我不确定您的$ Task是任务对象还是只是字符串...。如果只是字符串,则必须运行以获得上次运行时间:
$TaskRunTime = (Get-ScheduledTask $Task | Get-ScheduledTaskInfo).LastRunTime
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.