If the server is not accessible, you can test the session like I did here.
cls
$computer = "xxxxxxxxxxxxxxx"
$User = ".\admin"
$pswd = "admin"
$PWord = ConvertTo-SecureString -String $pswd -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
$sess = New-CimSession -ComputerName $computer -Credential $Credential
if ($sess) {
$stask = Get-ScheduledTask -CimSession $sess
$staskfinal = $stask | Select-Object TaskPath, TaskName, @{ Name = "Username";
Expression = { $_.Principal.UserID } }, @{ Name = "LogonType";
Expression = { $_.Principal.LogonType } } | Where {$_.username -like 'admin*'} # note user
$staskfinal # append to csv here
} else {
"Server is not accessible."
}
Or you could add a try/catch block.
cls
$computer = "xxxxxxxxxxxxxxx"
$User = ".\admin"
$pswd = "admin"
$PWord = ConvertTo-SecureString -String $pswd -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
$ErrorActionPreference = 'Stop' # if we get an error, stop processing
try {
$sess = New-CimSession -ComputerName $computer -Credential $Credential
$stask = Get-ScheduledTask -CimSession $sess
$staskfinal = $stask | Select-Object TaskPath, TaskName, @{ Name = "Username";
Expression = { $_.Principal.UserID } }, @{ Name = "LogonType";
Expression = { $_.Principal.LogonType } } | Where {$_.username -like 'admin*'} # note user
$staskfinal # append to csv here
} catch {
"Server is not accessible."
}
Beyond that you will need to provide more details about this "WMI query delay".