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.
Yeah, okay. So, not having a header is indeed where line 3 will be coming unstuck.
Basically, as you can see in your PowerShell output - and as you've already observed yourself, it's using the first row as the header, meaning your references to the $IP variable on lines 4 and 6 are essentially producing a [PSCustomObject] rather than a string, which won't work.
Simplest fix given your file format is to change Import-Csv to Get-Content on line 3. Or, just add a header to the CSV and then update either lines 4 or 6 (there's multiple options here) to reference that header as the attribute name.
That's probably all you need to do.
As for the New-Object thing: fair enough. Under Windows PowerShell (otherwise known as 5.1), having the `1 throw an exception but perhaps you're using PowerShell (i.e. 7.x) instead?
Anyway, so long as $ipRanges is producing the correct type (you can see the full type name jammed into the example below), you're golden.
Cheers,
Lain