[英]Looping through properties inside a PSObject in PowerShell (from json)
在 PowerShell 中打開 .json 文件后,我得到了一個名為 $json_sitesandsnets 的新 PSObject。 最終我想要做的是遍歷每個站點(奧蘭多、丹佛),並遍歷它們的每個子網,然后做一些事情。 最初,我需要使用 New-ADReplicationSubnet cmdlet 為 Active Directory 中的相應站點創建這些子網。 我已經使用它來創建單個子網,但我有一個更大的站點和子網列表,我將使用它。 這個例子只是一個小的精簡版本,在這里發布。 一旦我弄清楚如何訪問對象值中的這些子網,我可能只需將每個子網列表提供給 New-ADReplicationSubnet。 但我真的希望能夠循環每個子網用於其他目的。 我在想,一旦你知道如何去做,這可能並不難。
這是原始json:
{
"Orlando": [
"10.10.10.0/24",
"10.10.20.0/24",
"10.10.30.0/24",
"10.10.40.0/24"
],
"Denver": [
"10.0.70.0/24",
"10.0.80.0/24",
"10.0.90.0/24",
"10.0.100.0/24",
"10.0.110.0/24",
"10.0.120.0/24"
]
}
我得到了 PSObject 的屬性,如下所示:
$json_sitesandsnets.psobject.properties
當我在 VSCode 中運行 Debug 時,我可以看到 PSObject 和子網列表(數組):在 Debug 中看到這個
這是一些代碼,它獲取名稱沒問題(Write-Host $site.Name)。 正如您所看到的,我嘗試了幾種不同的方法來訪問這些子網,這是每個站點的價值。 我就是想不通。 我已經閱讀了很多關於循環 PSObject 的文章和論壇主題,但似乎無法找到訪問和循環作為 PSObject 值的數組的最佳答案。 我覺得我真的很親近。
foreach ($site in $json_sitesandsnets.PSObject.Properties) {
Write-Host $site.Name
$site| get-member -type NoteProperty | foreach-object {
$subnet = $site."$($_.Value)"
Write-Host $subnet
Write-Host $_.Value
}
}
您可以通過使用站點名稱進行索引來做到這一點。 $json_sitesandsnets.($site.Name)
我還會在foreach
使用$json_sitesandsnets.PSObject.Properties.Name
而不是$json_sitesandsnets.PSObject.Properties
來簡化事情:
foreach ($site in $json_sitesandsnets.PSObject.Properties.Name) {
Write-Host $site
foreach ($subnet in $json_sitesandsnets.$site){
Write-Host "logic for subnet $subnet"
}
# alternatively
$json_sitesandsnets.$site | ForEach-Object {
Write-Host "logic for subnet $_ (foreach-object)"
}
}
編輯:您的方法不起作用,因為子網是一個字符串數組,並且子網未命名為屬性/注釋屬性。 您可以通過執行以下操作來看到這一點。
# This tells you it's an array of "objects" with properties Length, Rank etc
Get-Member -InputObject $json_sitesandsnets.Denver
# This pipes the array elements, telling you they are strings with Length property
# In powershell strings are an object that only have properties Chars/Length
$json_sitesandsnets.Denver | Get-Member
# To only return the types
$json_sitesandsnets.Denver.GetType()
$json_sitesandsnets.Denver[0].GetType()
在我看來,你想要的是這個。
foreach ($site in $json_sitesandsnets.PSObject.Properties) {
Write-Host $site.Name -ForegroundColor Cyan
$site.Value | Out-String | Write-Host
}
輸出
對於子網值,not 屬性是Orlando和Denver 。 你做對了那部分,但是你的$site| get-member
$site| get-member
行不應該在那里。 相反,您可以直接通過$site.value
訪問該值。 如果你需要做更多的事情,你可以循環這些值,像這樣:
$index = 0
Foreach ($subnet in $site.Value) {
Write-Host "$($index): $subnet"
$index+=1
}
輸出
我不認為 json 可能是這樣的? 在 powershell 中生成的對象數組會更容易使用。 它幾乎可以是一個csv。
[
{
"site": "Orlando",
"subnets": [
"10.10.10.0/24",
"10.10.20.0/24",
"10.10.30.0/24",
"10.10.40.0/24"
]
},
{
"site": "Denver",
"subnets": [
"10.0.70.0/24",
"10.0.80.0/24",
"10.0.90.0/24",
"10.0.100.0/24"
]
}
]
cat sites.json | convertfrom-json
site subnets
---- -------
Orlando {10.10.10.0/24, 10.10.20.0/24, 10.10.30.0/24, 10.10.40.0/24}
Denver {10.0.70.0/24, 10.0.80.0/24, 10.0.90.0/24, 10.0.100.0/24}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.