我的 powershell 脚本顶部有一个基本的 XML 数组。 future 的计划要求我将其作为一个单独的文件来调用,但目前这是有效的:
[xml]$xml = @"
<?xml version="1.0"?>
<PublicSubnets>
<Subnet>15.4.211.0/16</Subnet>
<Subnet>15.4.211.0/24</Subnet>
<Subnet>15.4.211.0/24</Subnet>
<Subnet>15.4.211.0/24</Subnet>
<Subnet>15.4.211.0/24</Subnet>
</PublicSubnets>
"@
我试图调用它,并经历一个循环,其中有一个“优先级”和“计数器”,每次循环完成时都会上升。
例如,我希望有以下内容
15.4.211.0/16 counter=1 priority=250
15.4.211.0/24 counter=2 priority=251
15.4.211.0/24 counter=3 priority=252
我的想法是,我将基于这些在 Azure 中创建 NSG 规则,如果我可以获得正确的计数器和优先级,我可以自动添加 NSG 规则(计数器是规则编号,优先级是是规则优先级)。
目前这是我所拥有的(记住我在 powershell 脚本的顶部有 XML 数组)
$priority = 250
$counter = 1
foreach ($subnet in $xml.PublicSubnets)
{
$subnet.Subnet
Write-output "Counter is $counter and Priority is $priority"
$counter++
$priority++
}
我得到的不是我希望的输出(见上文),而是:
15.4.211.0/16
15.4.211.0/24
15.4.211.0/24
15.4.211.0/24
15.4.211.0/24
Counter is 1 and Priority is 250
如果我再次运行它,计数器和优先级都会增加 1,但这对我没有帮助:(
我的处理方式正确吗?我不是程序员,而且是 powershell 新手。我已经观看并阅读了教程,但这里确实需要一些帮助。
提前致谢
最佳答案
这应该有效
$priority = 250
$counter = 1
foreach ($subnet in $xml.PublicSubnets.Subnet)
{
$subnet.Subnet
Write-output "Counter is $counter and Priority is $priority"
$counter+=1
$priority+=1
}
关于xml - 通过 powershell 中的 XML 进行计数器(for 循环?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52956817/