Working with PSObject

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Don Jones Don Jones 2 years ago.

  • Author
    Posts
  • #20421
    Profile photo of rambog cnonline.net
    rambog cnonline.net
    Participant

    I have a number of CSV files that are dumped to a directory (e.g., c:\admindump). Each file is named after a building ie., Baker.csv. I need to be able to go through each file and extract the name of the workstation (under the column heading of Name), the IP address (under column "Client IP Address), and the building it is located. The building is not contained in the CSV files but are gathered from the name of the file i.e., Baker.csv is Baker.

    Using the code below, I am able to create my master list with the name of the computer and IP address but how do I get the building name into this master list?

    $ListOFiles=Get-ChildItem -Path "c:\admindump" -File *.csv
    $BigList=$ListOFiles|%{Import-CSV -Path "c:\admindump\$_"}
    Function Get-Info {
    Begin {}
    Process {
    $MachineInfo=new-object PSObject
    $MachineInfo|Add-Member -type Noteproperty -Name WKSName -value $_.name
    $MachineInfo|Add-Member -type Noteproperty -Name IP -value $_."Client IP Address"
    Write-Output $MachineInfo
    }
    End {}
    } #end function get-info

    $MasterList=$BigList|Get-Info

  • #20422
    Profile photo of Don Jones
    Don Jones
    Keymaster

    It's because you're separating some steps. Happens a lot when you try to use ForEach-Object – the nesting gets hard to track.

    function Combinator {
      $List = Get-ChildItem -Path "c:\admindump" -File *.csv
      ForEach ($file in $list) {
        $contents = Import-CSV -Path "c:\admindump\$_"
        foreach ($content in $contents) {
          $content | Add-Member -type NoteProperty -Name Building -value $file -PassThru
        }
      }
    }
    
    Combinator | Export-CSV masterlist.csv
    
    

    Something like that. Point being, you go through each file one at a time, rather than reading them in all at once, so that you'll have each file's filename available as you're reading it's data.

  • #20423
    Profile photo of Micah Battin
    Micah Battin
    Participant
    Function Get-Info {
        Param (
            [String[]]$Path
        )
        Process {
            ForEach ($P in $Path) {
                $CSV = Import-CSV $P
                ForEach ($Line in $CSV){
                    $MachineInfo = new-object PSObject
                    $MachineInfo | Add-Member -type Noteproperty -Name WKSName -value "$($Line.name)"
                    $MachineInfo | Add-Member -type Noteproperty -Name IP -value "$($Line.'Client IP Address')"
                    $MachineInfo | Add-Member -type Noteproperty -Name Building -value "$((Get-Item $P).BaseName)"
                    Write-Output $MachineInfo
                }
            }
        }
    }
    
    Get-Info -Path (Get-ChildItem -Path "c:\admindump" -File *.csv)
    

    Give that a try.

  • #20424
    Profile photo of Greg Altman
    Greg Altman
    Member

    Hmm I could get that to work like this:

    function Combinator {
      $List = Get-ChildItem -Path "c:\admindump\" -File *.csv
      ForEach ($file in $list) {
        $contents = Import-CSV -Path "c:\admindump\$file"
        foreach ($content in $contents) {
          $content | Add-Member -type NoteProperty -Name Building -value $file -PassThru
        }
      }
    }
     
    Combinator | Export-CSV c:\masterlist.csv
    

    The difference being in line 4 and changing the $_ to $file.

    Not trying to nitpick, just following along with the class 🙂

  • #20425
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yup sorry, I'd pasted your code and didn't change that.

You must be logged in to reply to this topic.