Author Posts

November 6, 2014 at 12:35 pm

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

November 6, 2014 at 12:41 pm

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.

November 6, 2014 at 12:52 pm

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.

November 6, 2014 at 12:52 pm

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 🙂

November 6, 2014 at 12:53 pm

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