简体   繁体   English

Powershell对象引用未设置为对象的实例

[英]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.

相关问题 Azure PowerShell-对象引用未设置为对象的实例 - Azure PowerShell - Object reference not set to an instance of an object powershell 位传输 object 引用未设置为 object 的实例 - powershell bitstransfer object reference not set to an instance of an object Powershell Core中的“对象引用未设置为对象的实例” - "Object reference not set to an instance of an object" in Powershell Core Powershell Set-Item 返回未设置为实例对象的对象引用 - Powershell Set-Item returns Object reference not set to an object of an instance PowerShell新项目-对象引用未设置为对象的实例 - PowerShell New-item - Object reference not set to an instance of an object PowerShell 给出错误“对象引用未设置到实例对象” - PowerShell giving an error "Object reference not set to an instance object" Function 从 Powershell 5 移植到 Powershell 7 失败,并显示“对象引用未设置为对象的实例” - Function ported from Powershell 5 to Powershell 7 fails with “object reference not set to an instance of an object” PowerShell导入的dll无法正常工作:使用“ 0”参数调用“ ReadLookupTables”的异常:“对象引用未设置为对象的实例 - PowerShell imported dll not working: Exception calling “ReadLookupTables” with “0” argument(s): "Object reference not set to an instance of an object AzCosmosDBSqlContainer:“对象引用未设置为 object 的实例。” - AzCosmosDBSqlContainer : “Object reference not set to an instance of an object.” Azure 函数 - New-AzContainerGroup:未将对象引用设置为对象的实例 - Azure Functions - New-AzContainerGroup : Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM