Use Get-Content Based On Data Type

Viewed 43

I have a PowerShell script that whitelists IPs to Azure Web Apps based on IPs in a file:

$ResourceGroupName = 'RG'
$WebAppName = 'WebApp'
$WhitelistFilePath = 'C:\IPs.txt'


$IPs = Get-Content $WhitelistFilePath
$name = 600
$priority = 600

foreach($IP in $IPs)
{
    Add-AzWebAppAccessRestrictionRule -ResourceGroupName $ResourceGroupName -WebAppName $WebAppName -Name "IP-$name" -Priority "$priority" -Action Allow -IpAddress "$IP/24"
    $name ++
    $priority ++
}

The script works fine with simple list of IPs. C:\IPs.txt:

10.0.0.0
100.0.0.0

Getting:

Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name IP-600 -Priority 600 -Action Allow -IpAddress 10.0.0.0/24
Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name IP-601 -Priority 601 -Action Allow -IpAddress 100.0.0.0/24

However, I'd also like to add names for those IPs, so C:\IPs.txt looks like this:

Ben's IP
10.0.0.0
John's IP
100.0.0.0

How can I edit my script so I receive:

Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name "Ben's IP" -Priority 600 -Action Allow -IpAddress 10.0.0.0/24
Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name "John's IP" -Priority 601 -Action Allow -IpAddress 100.0.0.0/24

Instead of what I'm getting now:

Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name IP-600 -Priority 600 -Action Allow -IpAddress Ben's IP/24
Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name IP-601 -Priority 601 -Action Allow -IpAddress 10.0.0.0/24
Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name IP-602 -Priority 602 -Action Allow -IpAddress John's IP/24
Add-AzWebAppAccessRestrictionRule -ResourceGroupName RG -WebAppName WebApp -Name IP-603 -Priority 603 -Action Allow -IpAddress 100.0.0.0/24
2 Answers

Bassie's helpful answer shows a viable solution with a for loop.

A perhaps simpler approach is to use Get-Content's -ReadCount parameter to read the lines of the input file in batches, namely 2 at a time in this case:

# Create a sample 'IPs.txt' file.
@'
Ben's IP
10.0.0.0
John's IP
100.0.0.0
'@ > IPs.txt

$priority = 600
Get-Content -ReadCount 2 IPs.txt | ForEach-Object {

  # Split the 2-element array of lines into its constituent lines.
  $name, $ip = $_

  # Call Add-AzWebAppAccessRestrictionRule with arguments
  # based on the variables.
  # -WhatIf previews the command; remove it to actually run the command.
  Add-AzWebAppAccessRestrictionRule -WhatIf `
    -ResourceGroupName RG -WebAppName WebApp `
    -Name $name -Priority ($priority++) -Action Allow -IpAddress $ip/24
}

You could use a for loop instead:

for ($i = 0; $i -lt $IPs.Length; $i = $i + 2) {
    $name = $IPs[$i]
    $ip = $IPs[$i +1]
    Add-AzWebAppAccessRestrictionRule -ResourceGroupName $ResourceGroupName -WebAppName $WebAppName -Name "IP-$name" -Priority "$priority" -Action Allow -IpAddress "$ip/24"
    $priority++
}

However, this could be unreliable depending on how certain you are of the format of the input file

Related