I'd like to use ForEach-Object -Parallel to whitelist IPs for Azure Web Apps. This is normally done like this:
Add-AzWebAppAccessRestrictionRule -ResourceGroupName "ResourceGroup1" -WebAppName "WebApp1" -Name "IP-100" -Priority "100" -Action Allow -IpAddress "10.0.0.0/24"
The approach that I had in mind, with changing count, as in changing IP-100 to IP-101, Priority from 100 to 101 doesn't work, as ForEach-Object -Parallel
is using isolated runspaces:
Each runspace must load whatever module is needed and have any variable be explicitly passed in from the calling script. The only variable that automatically appears in the parallel script block is the piped in object.
How can I run such script in parallel, something like this?
$Count = 100
Get-Content UniqueIPs.txt | ForEach-Object -Parallel {
Add-AzWebAppAccessRestrictionRule -ResourceGroupName "ResourceGroup1" -WebAppName "WepApp1" -Name "IP-$using:Count" -Priority "$using:Count" -Action Allow -IpAddress "$IP/24" -WhatIf
$Count ++
} -ThrottleLimit 5
Without -Parallel I usually did it like this:
foreach($IP in $IPs1)
{
Add-AzWebAppAccessRestrictionRule -ResourceGroupName "ResourceGroup1" -WebAppName "WebApp1" -Name "IP-$name" -Priority "$priority" -Action Allow -IpAddress "$IP/24"
$name ++
$priority ++
}