我有一组自定义对象,并且需要对每个对象执行以下步骤:
对象看起来像这样:
id : 1
groupId : 1
name : Customer1 Dashboard
fullName : Customer Dashboards/Team2 Group/Customer1 Group/Customer1 Dashboard
groupName : Customer1 Group
groupFullPath : Customer Dashboards/Team2 Group/Customer1 Group
widgetTokens : { @{type = owned; name = defaultResourceGroup; value = Client Teams/Team1 Group/Customers/Customer1*; inheritList = System.Object[] }, @{type = owned; name = defaultWebsiteGroup; value = Client Teams/Team1 Group/Customers/Customer1; inheritList = System.Object[]}}
这是我到目前为止所拥有的:
Foreach ($dashboard in $allDashboards) {
$dashboardProperties = @{ }
$dashboard.psobject.properties | ForEach-Object { $dashboardProperties[$_.Name] = $_.Value }
@($dashboardProperties.GetEnumerator()) | Where-Object { $_.Name -eq 'widgetTokens' } | Select-Object -ExpandProperty Value | Where-Object { $_.Value -match "Team1 Group" } | ForEach-Object { $dashboardProperties[$_.Key] = @($_.value.replace('Team1 Group', 'Team2 Group')) }
$dashboardProperties
#Code to interact with an API
}
因为有两个项目与“ @($ dashboardProperties.GetEnumerator())| Where-Object {$ _。Name -eq'widgetTokens'} ...”行匹配(至少,我认为这是原因) ,出现以下错误:
Index operation failed; the array index evaluated to null.
At line:1 char:200
+ ... ch-Object { $dashboardProperties[$_.Key] = @($_.value.replace('Team1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
Index operation failed; the array index evaluated to null.
At line:1 char:200
+ ... ch-Object { $dashboardProperties[$_.Key] = @($_.value.replace('Team1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
运行此命令时,我看到了两行,如预期的那样:
@($dashboardProperties.GetEnumerator()) | Where-Object { $_.Name -eq 'widgetTokens' } | Select-Object -ExpandProperty Value | Where-Object { $_.Value -match "Team1 Group" }
有什么想法,我不确定该如何克服?
谢谢
答案 0 :(得分:0)
根据Reddit的反馈,看起来应该可以:
$DashboardProperties.WidgetTokens |
Where-Object { $_['Name'] -eq 'widgetTokens' -and $_['Value'] -match 'Team1 Group' } |
ForEach-Object { $_['Value'] = $_['Value'] -replace '1','2' }