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



im正在尝试编写一种方法来查找数组中的webjob错误。

我在数组中查找空值时遇到问题。这就是我到目前为止所拥有的。

当我跑步时az webapp webjob triggered list --name webapp --resource-group resource-group

我得到了这个,我想让我的脚本接收错误:null,所以我使用它。

{
"错误"澳大利亚东南部",
"名称":"网络作业名称",
"resourceGroup":"资源组">
"runCommand":"Webjob.exe">
"schedulerLogsUrl":null,
"设置":{},
"systemData":null,
"类型":"Microsoft.Web/sites/trigeredwebjobs">
"url":"https://........./api/triggeredwebjobs/Webjob">
"使用Sdk":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?

假设az webapp webjob triggered list --name webapp --resource-group resource-group返回的正是您在回答中发布的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       : 

相关内容

  • 没有找到相关文章

最新更新