I'm trying to make a script that would let me add phonenumber 2factor for multiple azure users at once from an array/list.
I thought that maybe a 2d array would be a nice solution. and then loop through the array and add each persons number as it goes down the list.
however powersehell is very much not my thing. so i cant rly get it going.
So if anyone could help me with how to make the 2 array, and how to then grab each nested array within the aray.
below is a "sketch" of what i would like to work.
$name = [
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"],
["[email protected]"," 1 12345678"]
]
bellow is the command that i would use to add the number that belongs to the email.
foreach ($i in $name) {
New-MgUserAuthenticationPhoneMethod -UserId $navn[$i][0] -phoneType "mobile" -phoneNumber $navn[$i][1]
}
CodePudding user response:
The foreach
iterator doesn't work quite like that. Its idea is that you'll have access to each element on the collection, one by one. That's not the same as an index to the item. Since the $name
is an array containing arrays, you can use indexer to access inner member's elements.
Like so,
# Use parenthesis (, not brackets [
# $contacts would be better a name since it's a collection of mail and phone pairs
$name = (
("[email protected]"," 1 12345678"),
...
("[email protected]"," 1 12345678"))
# Print each element's first - index zero - member:
foreach($i in $name) { $i[0] }
name-01@email.com
...
name-10@email.com
# Or second - index one - member:
foreach($i in $name) { $i[1] }
1 12345678
...
1 12345678
So the final version should be akin to
foreach ($i in $name) {
New-MgUserAuthenticationPhoneMethod -UserId $i[0] -phoneType "mobile" -phoneNumber $i[1]
}
CodePudding user response:
what i ended up with is super messy.
$user = @(
("[email protected]"," 1 12345678"),
("[email protected]"," 1 22345678"),
("[email protected]"," 1 32345678"),
("[email protected]"," 1 42345678"),
("[email protected]"," 1 52345678"),
("[email protected]"," 1 62345678"),
("[email protected]"," 1 72345678")
)
$position = 0
foreach ($i in $user) {
New-MgUserAuthenticationPhoneMethod -UserId $user[$position][0] -phoneType "mobile" -phoneNumber $user[$position][1]
$position
}
EDIT: use vonPryz solution! its WAY cleaner and how you should do this.