Invoke-Command Help

Hey Everyone,

Running into an issue I can’t really figure out and have tried a few different ways.
See Code below…

Problem: You cannot call a method on a null-valued expression.
So its something to do in my Foreach when creating the dc2Data it throws that error when I step through it with a debugger. But when I run the code below in its own window, not within UD, it populates the variable and creates the chart… I am assuming this is because the Variable is living on the computer I invoked it on and not my host machine?

New-UDGrid -Title "DC2" -Headers @("HostName","IP Address", "DC1 Future Address", "Fail To DC1") -Properties @("HostName","IP Address", "DC1 Address", "Fail To DC1") -PageSize 5 -Endpoint {
                $dc2 = @()
                $dc2Data = Invoke-Command -ComputerName ns01.local -ScriptBlock {Get-DnsServerResourceRecord -ZoneName "ZoneName" -ComputerName "ComputerName.Local" | Where-Object {$_.RecordData.IPV4Address -like '10.150.*'}}
                $dc2Data | ForEach-Object { `
                    $dc2 += [PSCustomObject]@{
                        'HostName' = $_.HostName;
                        'IP Address' = $_.RecordData.IPV4Address.IPAddressToString;
                        'DC1 Address' = $($_.RecordData.IPV4Address.IPAddressToString).Replace('.150.','.250.')
                        'Fail To DC1' = (New-UDButton -Text "Failover!" -Icon hand_point_left -IconAlignment left -BackgroundColor '#bf0000' -FontColor 'White' -OnClick {Show-UDToast -Message "Clicked Failover!"});
                    }
                }
            $dc2 | Out-UDGridData 
            }

Ideas? Tips on how to improve my code as well would be awesome :slight_smile:

Thanks all
-Obie

1 Like

Hi, you code looks fine fir me.
I can imagine that the property “DC1 Address” is null and you are trying to execute “.Replace()” on that.
Try that code without the replacement, If the error is resolved there is a null value. Just out of my mind a -replace can help (but not sure if you are expecting a null value).
Kr

1 Like

Hey Augustin,

Good idea. I just commented it out and then my first row in the Grid populated every hostname into 1 row. Seems like its not breaking it apart when i loop through it with the foreach… Will have to figure out another way to separate it into the PSCustom Object

Hi @Obie
Try this as the endpoint:

	$dc2Data | ForEach-Object { 
		[PSCustomObject]@{
			'HostName' = $_.HostName;
			'IP Address' = $_.RecordData.IPV4Address.IPAddressToString;
			'DC1 Address' = $($_.RecordData.IPV4Address.IPAddressToString).Replace('.150.','.250.')
			'Fail To DC1' = (New-UDButton -Text "Failover!" -Icon hand_point_left -IconAlignment left -BackgroundColor '#bf0000' -FontColor 'White' -OnClick {Show-UDToast -Message "Clicked Failover!"});
		}
	} | Out-UDGridData

Should work?
I’m questioning wether or not UDGrid supports spaces in property names? If not, try to shorten the names, and add em to the properties param on the grid to match.

Happy hunting

Somewhat works haha. It manages to split each line into the Grid with an individual hostname this time but for some reason the “$_.RecordData.IPV4Address.IPAddressToString” still returns a NULL value… But if i run it manually I get a response with a value. which causes the Grid to fail to load.

Not sure why it returns null for the Dashboard but not my individual user running the exact same commands.

Remove the $ at the start of it. No need to escape :thinking:
Give it a go?

@BoSen29 Didn’t help haha…

But I figured it out a workaround i guess… When retrieving the objects I just did a Select-Object and set an Alias for each property… Something with the RecordData.IPV4Address.IPAddressToString; it did not like when trying to parse it or something… Not it just returns as $_.IPAddress and works fine…

Strange…

But thank you @BoSen29 and @augustin.ziegler for the help and ideas! Always nice to get another pair of eyes on a problem to make sure I am not derp-in’ out on something.

-Obie

1 Like

Good to hear man!

Anytime bro :slight_smile: