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:

$NumberRanges = @("+4412xx","+4414xx","+4416xx")
$Users = $null

foreach ($Range in $NumberRanges)
{
	Write-Output "Working with $($Range) Range..."
	[string]$RangeString = $Range
	$Users += Get-CsPhoneNumberAssignment | `
	Where-Object{ (($_.TelephoneNumber -like $Range) -and ($_.AssignedPstnTargetId -ne $null)) } | `
	Select-Object @{ Label = "Number"; Expr = { $_.TelephoneNumber.Replace($Range, "") } },`
				  @{ Label = "DisplayName"; Expr = { (Get-MgUser -UserId $_.AssignedPstnTargetId).DisplayName } },`
				  @{ Label = "System"; Expr = { "Teams" },`
				  @{ Label = "Range"; Expr = { $RangeString }} }

}

The issue is with the last Label statement. Whenever I run this I get the following error message:

Select-Object : The "Expr" key has a type, System.Object[], that is not valid; expected types are {System.String,
System.Management.Automation.ScriptBlock}.
At line:7 char:1
+ Select-Object @{ Label = "Number"; Expr = { $_.TelephoneNumber.Replac ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException
    + FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand

Help Please !?

Cheers,

Mike

4 Spice ups

Your last label has an extra } on the line. I’d remove that.

3 Spice ups

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.

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?

Yes, the issue here is the closing curly braces. There is no way $RangeString can be a collection based on this code.

So adding the missing } before the comma on the second to last label line. And then removing the extra } on the final label line.

I fixed the += inefficiency.

$NumberRanges = @("+4412xx","+4414xx","+4416xx")
$Users = [System.Collections.Generic.List[pscustomobject]]@()

foreach ($Range in $NumberRanges)
{
	Write-Output "Working with $($Range) Range..."
	[string]$RangeString = $Range
	$User = Get-CsPhoneNumberAssignment |
                Where-Object{ (($_.TelephoneNumber -like $Range) -and ($_.AssignedPstnTargetId -ne $null)) } |
                    Select-Object @{ Label = "Number"; Expr = { $_.TelephoneNumber.Replace($Range, "") } },
		                @{ Label = "DisplayName"; Expr = { (Get-MgUser -UserId $_.AssignedPstnTargetId).DisplayName } },
		                @{ Label = "System"; Expr = { "Teams" }},
		                @{ Label = "Range"; Expr = { $RangeString }}
    $Users.Add($User)

}
# output 
$Users
1 Spice up

Loos much better, good catch!