Returning object from scriptblock

Hi,

I struggle with understanding why I dont get the $infoObject returned from the scriptBlock and written to the CSV file afterwards

Invoke-command -ComputerName $sourceServerName -Credential $CredentialInitializedEarlier -ScriptBlock {
$infoObject = New-Object PSObject

$MacInfo = Get-NetAdapter | Select-Object-Object MacAddress, ifIndex
$IpV4Info = Get-NetIpAddress | Where-Object {($.Interfaceindex -eq $MacInfo.ifIndex ) -and ($.AddressFamily -eq “IPv4”)} | Select-Object-Object IPAddress

Add-Member -inputObject $infoObject -memberType NoteProperty -name “Mac” -value $MacInfo.MacAddress
Add-Member -inputObject $infoObject -memberType NoteProperty -name “IpV4Info” -value $IpV4Info
Add-Member -inputObject $infoObject -memberType NoteProperty -name “ServerName” -value $sourceServerName

$infoObject
} | ConvertFrom-Csv | Export-Csv F:\data\MACAddressBaseline.csv -NoTypeInformation -append

 

Remove ConvertFrom-Csv

$result = 
Invoke-command -ComputerName $sourceServerName -Credential $CredentialInitializedEarlier -ScriptBlock {
    $MacInfo = Get-NetAdapter | Select-Object-Object MacAddress, ifIndex
    $IpV4Info = Get-NetIpAddress | 
    Where-Object {($_.Interfaceindex -eq $MacInfo.ifIndex ) -and ($_.AddressFamily -eq "IPv4")} | 
    Select-Object-Object IPAddress
    
    [PSCustomObject]@{
        Mac = $MacInfo.MacAddress
        IpV4Info = $IpV4Info
        ServerName = $sourceServerName
    }
} 
$result | Export-Csv F:\data\MACAddressBaseline.csv -NoTypeInformation -append

Well, you gotta a number of errors:
1- Lines 3 and 4 show ‘Select-Object-Object’ instead of ‘Select’ or ‘Select-Object’
2- $IpV4Info line 4 assumes incorrectly that there’s a single $MacInfo, where the code must consider that $MacInfo is a collection of objects that you need to iterate through as in:

    $IpV4Info = foreach ($Mac in $MacInfo) {
        (Get-NetIpAddress | Where-Object {($_.Interfaceindex -eq $Mac.ifIndex ) -and ($_.AddressFamily -eq "IPv4")}).IPAddress
    }

3- Line 4 should return the (string) value of the the IPAddress, not an object that has a property called IPAddress. Using Select-Object at the end of line 4 returns an object like @{IPAddress=192.168.124.121} which would not even show up in CSV - See how line 4 is corrected in the final code below.
4- Lines 5 and 6 should account for having multiple values for MAC and IPAddress, especially when outputting to CSV. The corrected code below joins the values using the -join operator
5- In the last line #9, ConvertFrom-Csv is not required
6- In the last line #9, it’s recommended to use

select ServerName,IpV4Info,Mac

to exclude the scriptblock automatic variables such as RunSpaceId, PSComputerName, …
7- Line 7 must use

 $Using:sourceServerName

to grab the desired value inside the script block which was assigned in the parent scope

Suggested code:

Invoke-command -ComputerName $sourceServerName -Credential $CredentialInitializedEarlier -ScriptBlock {
    $infoObject = New-Object PSObject
    $MacInfo = Get-NetAdapter | Select-Object MacAddress, ifIndex
    $IpV4Info = foreach ($Mac in $MacInfo) {
        (Get-NetIpAddress | Where-Object {($_.Interfaceindex -eq $Mac.ifIndex ) -and ($_.AddressFamily -eq "IPv4")}).IPAddress
    }
    Add-Member -inputObject $infoObject -memberType NoteProperty -name "Mac" -value ($MacInfo.MacAddress  -join ', ')
    Add-Member -inputObject $infoObject -memberType NoteProperty -name "IpV4Info" -value ($IpV4Info -join ', ')
    Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $Using:sourceServerName
    $infoObject
} | select ServerName,IpV4Info,Mac | Export-Csv F:\data\MACAddressBaseline.csv -NoTypeInformation -append

Alternatively use the Get-PCInfo function of the AZSBTools PS module as in:

Install-Module AZSBTools -Force -AllowClobber -Scope CurrentUser -SkipPublisherCheck

Get-PCInfo -ComputerName $sourceServerName -Cred $CredentialInitializedEarlier |
    Select ComputerName,IPAddress,MacAddress |
        Export-Csv F:\data\MACAddressBaseline.csv -NoTypeInformation -append