Forum Discussion
Create Named Location list using PowerShell
- Jul 13, 2022
Here's a quick-n-dirty re-working if your original script demonstrating the Get-Content approach.
I've included a screenshot showing the output from $ipRanges but I had to keep the subtle change regarding "`1", otherwise, it throws an error for me under Windows PowerShell.
Connect-AzureAD $ipRanges = New-Object -TypeName System.Collections.Generic.List`1[Microsoft.Open.MSGraph.Model.IpRange] Get-Content -Path C:\IPs.csv | ForEach-Object { $ipRanges.Add($_) } New-AzureADMSNamedLocationPolicy -OdataType "#microsoft.graph.ipNamedLocation" -DisplayName "IP named location policy" -IsTrusted $false -IpRanges $ipRanges
$ipRanges output (purely as confirmation it's correctly producing the IpRange data type):
Cheers,
Lain
Hello LainRobertson,
Thank you for your reply.
The csv file is very simple, it doesn't have any headers:
But when I try to get what I have in the array, I get the following result:
Probably PowerShell thinks that the first line is the header.
Regarding the "`1" on line 2, I removed it and have the following error: Cannot find an overload for "Add" and the argument count: "1". I used the old-school format.
Here's a quick-n-dirty re-working if your original script demonstrating the Get-Content approach.
I've included a screenshot showing the output from $ipRanges but I had to keep the subtle change regarding "`1", otherwise, it throws an error for me under Windows PowerShell.
Connect-AzureAD
$ipRanges = New-Object -TypeName System.Collections.Generic.List`1[Microsoft.Open.MSGraph.Model.IpRange]
Get-Content -Path C:\IPs.csv |
ForEach-Object {
$ipRanges.Add($_)
}
New-AzureADMSNamedLocationPolicy -OdataType "#microsoft.graph.ipNamedLocation" -DisplayName "IP named location policy" -IsTrusted $false -IpRanges $ipRanges
$ipRanges output (purely as confirmation it's correctly producing the IpRange data type):
Cheers,
Lain
- mikhailfJul 13, 2022Iron Contributor
Thank you, I removed "`1" from the script and it worked.
By the way, I use Powershell 5.1.
Appreciate your help.