I’m working with Microsoft Teams to build a complete list of our DDI ranges, the current method we are using is a throw back to the early 2010’s. So what I’m trying to do is get a complete listing of all our DDI’s in use. This I can do pretty easily. What’s proving to be an issue is adding a constant to the output. The code below:<\/p>\n
$NumberRanges = @(\"+4412xx\",\"+4414xx\",\"+4416xx\")\n$Users = $null\n\nforeach ($Range in $NumberRanges)\n{\n\tWrite-Output \"Working with $($Range) Range...\"\n\t[string]$RangeString = $Range\n\t$Users += Get-CsPhoneNumberAssignment | `\n\tWhere-Object{ (($_.TelephoneNumber -like $Range) -and ($_.AssignedPstnTargetId -ne $null)) } | `\n\tSelect-Object @{ Label = \"Number\"; Expr = { $_.TelephoneNumber.Replace($Range, \"\") } },`\n\t\t\t\t @{ Label = \"DisplayName\"; Expr = { (Get-MgUser -UserId $_.AssignedPstnTargetId).DisplayName } },`\n\t\t\t\t @{ Label = \"System\"; Expr = { \"Teams\" },`\n\t\t\t\t @{ Label = \"Range\"; Expr = { $RangeString }} }\n\n}\n<\/code><\/pre>\n
Advertisement
The issue is with the last Label statement. Whenever I run this I get the following error message:<\/p>\n
Select-Object : The \"Expr\" key has a type, System.Object[], that is not valid; expected types are {System.String,\nSystem.Management.Automation.ScriptBlock}.\nAt line:7 char:1\n+ Select-Object @{ Label = \"Number\"; Expr = { $_.TelephoneNumber.Replac ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException\n + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand\n<\/code><\/pre>\n
Advertisement
Help Please !?<\/p>\n
Cheers,<\/p>\n
Mike<\/p>","upvoteCount":4,"answerCount":6,"datePublished":"2025-06-11T15:12:48.183Z","author":{"@type":"Person","name":"mikelaing","url":"https://community.spiceworks.com/u/mikelaing"},"acceptedAnswer":{"@type":"Answer","text":"
Yes, the issue here is the closing curly braces. There is no way $RangeString can be a collection based on this code.<\/p>\n
So adding the missing } before the comma on the second to last label line. And then removing the extra } on the final label line.<\/p>\n
I fixed the += inefficiency.<\/p>\n
$NumberRanges = @(\"+4412xx\",\"+4414xx\",\"+4416xx\")\n$Users = [System.Collections.Generic.List[pscustomobject]]@()\n\nforeach ($Range in $NumberRanges)\n{\n\tWrite-Output \"Working with $($Range) Range...\"\n\t[string]$RangeString = $Range\n\t$User = Get-CsPhoneNumberAssignment |\n Where-Object{ (($_.TelephoneNumber -like $Range) -and ($_.AssignedPstnTargetId -ne $null)) } |\n Select-Object @{ Label = \"Number\"; Expr = { $_.TelephoneNumber.Replace($Range, \"\") } },\n\t\t @{ Label = \"DisplayName\"; Expr = { (Get-MgUser -UserId $_.AssignedPstnTargetId).DisplayName } },\n\t\t @{ Label = \"System\"; Expr = { \"Teams\" }},\n\t\t @{ Label = \"Range\"; Expr = { $RangeString }}\n $Users.Add($User)\n\n}\n# output \n$Users\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2025-06-11T15:21:04.294Z","url":"https://community.spiceworks.com/t/listing-ddi-numbers-from-teams-in-powershell/1214325/5","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},"suggestedAnswer":[{"@type":"Answer","text":"I’m working with Microsoft Teams to build a complete list of our DDI ranges, the current method we are using is a throw back to the early 2010’s. So what I’m trying to do is get a complete listing of all our DDI’s in use. This I can do pretty easily. What’s proving to be an issue is adding a constant to the output. The code below:<\/p>\n
$NumberRanges = @(\"+4412xx\",\"+4414xx\",\"+4416xx\")\n$Users = $null\n\nforeach ($Range in $NumberRanges)\n{\n\tWrite-Output \"Working with $($Range) Range...\"\n\t[string]$RangeString = $Range\n\t$Users += Get-CsPhoneNumberAssignment | `\n\tWhere-Object{ (($_.TelephoneNumber -like $Range) -and ($_.AssignedPstnTargetId -ne $null)) } | `\n\tSelect-Object @{ Label = \"Number\"; Expr = { $_.TelephoneNumber.Replace($Range, \"\") } },`\n\t\t\t\t @{ Label = \"DisplayName\"; Expr = { (Get-MgUser -UserId $_.AssignedPstnTargetId).DisplayName } },`\n\t\t\t\t @{ Label = \"System\"; Expr = { \"Teams\" },`\n\t\t\t\t @{ Label = \"Range\"; Expr = { $RangeString }} }\n\n}\n<\/code><\/pre>\nThe issue is with the last Label statement. Whenever I run this I get the following error message:<\/p>\n
Select-Object : The \"Expr\" key has a type, System.Object[], that is not valid; expected types are {System.String,\nSystem.Management.Automation.ScriptBlock}.\nAt line:7 char:1\n+ Select-Object @{ Label = \"Number\"; Expr = { $_.TelephoneNumber.Replac ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException\n + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand\n<\/code><\/pre>\nHelp Please !?<\/p>\n
Cheers,<\/p>\n
Mike<\/p>","upvoteCount":4,"datePublished":"2025-06-11T15:12:48.251Z","url":"https://community.spiceworks.com/t/listing-ddi-numbers-from-teams-in-powershell/1214325/1","author":{"@type":"Person","name":"mikelaing","url":"https://community.spiceworks.com/u/mikelaing"}},{"@type":"Answer","text":"
Your last label has an extra } on the line. I’d remove that.<\/p>","upvoteCount":3,"datePublished":"2025-06-11T15:16:19.577Z","url":"https://community.spiceworks.com/t/listing-ddi-numbers-from-teams-in-powershell/1214325/2","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"
If I read the error message correctly, it’s assuming the label “Number”; Expr is wrong and should be Expr.object I’d start there.<\/p>","upvoteCount":0,"datePublished":"2025-06-11T15:17:01.894Z","url":"https://community.spiceworks.com/t/listing-ddi-numbers-from-teams-in-powershell/1214325/3","author":{"@type":"Person","name":"Jay-Updegrove","url":"https://community.spiceworks.com/u/Jay-Updegrove"}},{"@type":"Answer","text":"
In the second to last label, there are two open, one closed, that is then closed on the last label so the number is correct but might be on the wrong line?<\/p>","upvoteCount":0,"datePublished":"2025-06-11T15:18:42.097Z","url":"https://community.spiceworks.com/t/listing-ddi-numbers-from-teams-in-powershell/1214325/4","author":{"@type":"Person","name":"Jay-Updegrove","url":"https://community.spiceworks.com/u/Jay-Updegrove"}},{"@type":"Answer","text":"
Loos much better, good catch!<\/p>","upvoteCount":0,"datePublished":"2025-06-11T15:30:19.730Z","url":"https://community.spiceworks.com/t/listing-ddi-numbers-from-teams-in-powershell/1214325/6","author":{"@type":"Person","name":"Jay-Updegrove","url":"https://community.spiceworks.com/u/Jay-Updegrove"}}]}}