I was looking for a way to use csv input for a Powershell script like I use Excel. The way I wanted to use this is the following:
I have a matrix with AD application groups as columns and users as rows. If a user should have accesss to the application there should be a “x” otherwise it is empty. This matrix saved as a csv file, I wanted to know all uses for every application group.
This post of Alex K. Angelopoulos showed me the way to use a filter for a two demensional matrix in a csv file by using the foreach and where cmdlets.
In my case I want all users for every application group. There for I want to loop through all columns:
# This will get the noteproperties and the name of the noteproperties is the header of that column
$header = (Get-Member -InputObject $csv[0] -MemberType NoteProperty)
# This will loop through all columns of which the name is not username
# This way I only have all columns which represents an application group
foreach ( $attr in $header ) {
If ($attr.Name -ne "Username") {
# this should hold the code to select only the selected users
# Then you can do the action you want on every user by doing a ForEach ($user in $username) for example
}
}
Then I want to select only that records that have an x in that column.
$csv | Where-Object {$_.($attr.Name) -eq "x"}
If I then want not the records but the actual usernames that holds an x in that column I need to add a ForEach and it will look like this.
$usernames = $csv | Where-Object {$_.($attr.Name) -eq "x"} | ForEach {$_.Username}


