Powershell:if语句,在数组中获取null

Powershell: if statement, pick up null in an array

我正在尝试编写一种在数组中查找网络作业错误的方法。

我在获取数组中的空值时遇到问题。 这是我目前所拥有的。

当我运行 az webapp webjob triggered list --name webapp --resource-group resource-group

我明白了,我希望我的脚本能够发现错误:null,所以我使用它。

{
“错误”:空,
"extraInfoUrl": "......",
"historyUrl": "......................",
"id": ".............",
“种类”:空,
“最新运行”:空,
"location": "澳大利亚东南部",
“名称”:“网络作业名称”,
“资源组”:“资源组”,
"运行命令": "Webjob.exe",
“schedulerLogsUrl”:空,
“设置”:{},
“系统数据”:空,
“类型”:“微软。Web/sites/triggeredwebjobs”,
"url": "https://........./api/triggeredwebjobs/Webjob",
“usingSdk”:错误,
“webJobType”:空
}

  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?

假设 az webapp webjob triggered list --name webapp --resource-group resource-group returns 与您在答案中发布的 JSON 完全相同,您可以这样做:

$json = az webapp webjob triggered list --name webapp --resource-group resource-group | ConvertFrom-Json

或者这将同样有效:

$resGroup = az webapp webjob triggered list --name webapp --resource-group resource-group
$json = ConvertFrom-Json $resGroup

现在你有了一个很容易操纵的对象。因此,例如,如果您想检查 error 是否为 null:

$json = @'
{
"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
}
'@ | ConvertFrom-Json

PS C:\> if(-not $json.error){'Value is Null'}
Value is Null

PS C:\> if($json.error -eq $null){'Value is Null'}
Value is Null

您从 ConvertFrom-Json 获得的对象应该如下所示:

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