Need help in fixing the error

I have script below to check the deadline of the updates available in software center```

$TargetedUpdates= Get-WmiObject -ComputerName $system -Namespace root\CCM\ClientSDK -Class CCM_SoftwareUpdate -Filter ComplianceState=0


 Foreach ($Update in $TargetedUpdates){
$Object = New-Object PSObject -Property ([ordered]@{ 
Deadline          = [datetime]::ParseExact("$($Update.Deadline.Substring(6,2))/$($Update.Deadline.Substring(4,2))/$($Update.Deadline.Substring(0,4))", "dd/MM/yyyy", $null)
}
$Object
}

I am getting error for some updates as below, though for some updates Its going smooth

You cannot call a method on a null-valued expression.
At line:99 char:71
+ ...   = [datetime]::ParseExact("$($Update.Deadline.Substring(6,2))/$($Upd ...
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 

Please help

You should not use Get-WmiObject anymore.

$ComputerName = 'RemoteComputer'

$GetCimInstanceParams = @{
    ComputerName = $ComputerName
    Namespace    = 'root\CCM\ClientSDK'
    ClassName    = 'CCM_SoftwareUpdate'
    Filter       = 'ComplianceState=0'
}

$TargetedUpdates = Get-CimInstance @GetCimInstanceParams

Foreach ($Update in $TargetedUpdates) {
    [PSCustomObject]@{
        ComputerName = $ComputerName
        Name         = $Update.Name
        ArticleID    = $Update.ArticleID
        Deadline     = $Update.Deadline
    }
}
2 Likes