gpt4 book ai didi

azure-powershell - PowerShell 7. ForEach-Object -Parallel 不会针对 Azure PowerShell 进行身份验证

转载 作者:行者123 更新时间:2023-12-04 11:43:46 24 4
gpt4 key购买 nike

我们编写了一个脚本来并行执行 Azure PowerShell 命令。问题是当我们增加-ThrottleLimit高于 1,一些命令没有被正确执行。脚本是:

# Writing IPs for whitelisting into file.
Add-Content -Path IPs.txt -Value ((Get-AzWebApp -ResourceGroupName "ResourceGroup1" -Name "WebApp1").OutboundIpAddresses).Split(",")
Add-Content -Path IPs.txt -Value ((Get-AzWebApp -ResourceGroupName "ResourceGroup1" -Name "WebApp1").PossibleOutboundIpAddresses).Split(",")
# Writing new file with inique IPs.
Get-Content IPs.txt | Sort-Object -Unique | Set-Content UniqueIPs.txt

# Referencing the file.
$IPsForWhitelisting = Get-Content UniqueIPs.txt

# Assigning priotiry number to each IP
$Count = 100
$List = foreach ($IP in $IPsForWhitelisting) {
$IP|Select @{l='IP';e={$_}},@{l='Count';e={$Count}}
$Count++
}
# Whitelisting all the IPs from the list.
$List | ForEach-Object -Parallel {
$IP = $_.IP
$Priority = $_.Count
$azureApplicationId ="***"
$azureTenantId= "***"
$azureApplicationSecret = "***"
$azureSecurePassword = ConvertTo-SecureString $azureApplicationSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($azureApplicationId , $azureSecurePassword)
Connect-AzAccount -Credential $credential -TenantId $azureTenantId -ServicePrincipal | Out-null
echo "IP-$Priority"
echo "$IP/24"
echo $Priority
Add-AzWebAppAccessRestrictionRule -ResourceGroupName "ResourceGroup1" -WebAppName "WebApp1" -Name "IP-$Priority" -Priority $Priority -Action Allow -IpAddress "$IP/24"
} -ThrottleLimit 1
ThrottleLimit设置为 1 - 8 条规则正在创建,如果 ThrottleLimit设置为 2 - 7 条规则正在创建,3 - 4 条规则,10 - 1 条规则,因此一些规则被跳过。
这种行为的原因是什么?

最佳答案

总之——-Parallel参数不会(可能还)神奇地导入所有落入 For-EachObject 范围内的因变量堵塞。实际上,PWSH 跨越不同的进程,只有循环的数组才会被隐式传递,所有其他变量都需要显式指定。
应该使用 $using:指令(前缀)表示要在并行代码块中导入(使其可见)的变量。
例子:

$avar = [Int]10
$bvar = [Int]20
$list = @('here', 'it', 'eees')

$list | ForEach-Object -Parallel {
Write-Output "(a, b) is here ($($using:avar), $($using:bvar))"
Write-Output "(a, b) missing ($($avar), $($bvar))"
Write-Output "Current element is $_"
}```

*thus - the described behavior is likely due to the fact that config. variables are not imported (at all) and thus the operations silently fail.*

关于azure-powershell - PowerShell 7. ForEach-Object -Parallel 不会针对 Azure PowerShell 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68211301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com