Returning object from scriptblock

Welcome Forums General PowerShell Q&A Returning object from scriptblock

Viewing 2 reply threads
  • Author
    Posts
    • #214947
      Participant
      Topics: 2
      Replies: 3
      Points: 32
      Rank: Member

      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

       

    • #214962
      Participant
      Topics: 5
      Replies: 322
      Points: 446
      Helping Hand
      Rank: Contributor

      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
      
    • #214965
      Participant
      Topics: 12
      Replies: 524
      Points: 1,220
      Helping Hand
      Rank: Community Hero

      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
      
Viewing 2 reply threads
  • You must be logged in to reply to this topic.