mercredi 26 mai 2021

Powershell: if statement, pick up null in an array

im trying to script up a way to find webjob errors in array.

Im having troubles picking up null values in an array. this is what i have so far.

When i run az webapp webjob triggered list --name webapp --resource-group resource-group

i get this and i want my script to pick up the error: null so i work with it.

{
"error": null,
"extraInfoUrl": "............",
"historyUrl": "......................",
"id": ".................",
"kind": null,
"latestRun": null,
"location": "Australia Southeast",
"name": "webjobname",
"resourceGroup": "resource-group",
"runCommand": "Webjob.exe",
"schedulerLogsUrl": null,
"settings": {},
"systemData": null,
"type": "Microsoft.Web/sites/triggeredwebjobs",
"url": "https://........./api/triggeredwebjobs/Webjob",
"usingSdk": false,
"webJobType": null
}

  write-host -ForegroundColor Yellow $webApp.Name        
        $webjobname = (az webapp webjob triggered list --name $webApp.Name --resource-group $group.ResourceGroupName --query '[].name' -o tsv)
        $jobname = ($webjobname -replace ".*/")
        $webjoberror = (az webapp webjob triggered list --name $webApp.Name --resource-group $group.ResourceGroupName --query '[].error' -o tsv)

{
            #Count through each of the webjob array
            for ($i = 0; $i -lt $webjobname.Count; $i++) 
            {
                #if any of the webjob has a status of stopped    

                if ($null -eq $webjoberror[$i]) 
                {  
                      Write-Host -ForegroundColor Green $jobname[$i]
                        Write-Host "No errors with" $jobname[$i] "it looks fine" $webApp.Name $group.ResourceGroupName
                    #Start the Stopped Webjob
               }
                    else
                {      
                       Write-host "There is an error"
                        Write-Host -ForegroundColor Red $webjobname[$i]
                    }
                }
}

The error i am getting is Cannot index into a null array.
At line:86 char:21
+                 if ($null -eq $webjoberror[$i]) #latest is null
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
 
Cannot index into a null array.
At line:86 char:21
+                 if ($null -eq $webjoberror[$i]) #latest is null
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray


I have tried 
if (!$webjoberror[$i])
if ($webjoberror.count -gt 0)
if ($webjoberror -eq $null)
and different other ones i cant remember. any suggestions?

Aucun commentaire:

Enregistrer un commentaire