Author Posts

October 31, 2016 at 7:27 pm

Hi,

I'm fairly new to Powershell. What I have is a text file that lists users, locations, and what they have access to at that location. Here is an example of I'm working with:

IEA0A,AA,.21131711.......115111917.
IEH6F,AC10,....1..1.........1..1..1..

Each number (or period) represents an access right to a part of the program. What I need is to separate each access into an object and remove the columns that I do not need (Lets say that all I need is the 1st, 5th, and 12th.

Here is what I have so far. Not much, but it strips away all the extra stuff in the report and leaves just the data.

$raw = Get-Content C:\file.txt | Where-Object {$_ -like ' I*'} | ForEach-Object {$_.trim()}
$raw -replace "\s+",","

Thanks,

October 31, 2016 at 8:06 pm

i think this is good starting point to work with objects

PS C:\Users\alex\Desktop> $txt = Import-Csv .\test.txt -Delimiter "," -Header User,Location,Access
PS C:\Users\alex\Desktop> $txt

User  Location Access
----  -------- ------
IEA0A AA       .21131711.......115111917.
IEH6F AC10     ....1..1.........1..1..1..


PS C:\Users\alex\Desktop> $txt[0]

User  Location Access
----  -------- ------
IEA0A AA       .21131711.......115111917.

October 31, 2016 at 9:00 pm

So, how would I go about getting the following result?

User Location Access1 Access2 Access3
—- ——– —— —— ——
IEA0A AA . 2 1 <–and so on

October 31, 2016 at 9:09 pm

by the way , each line is an array of properties for the csv object , and each property is an array of characters
that means you can access each line's "User" with it's key , indexed by zero and/or each access character indexed by zero , like so

$txt.User[0] or $txt.access[0][0] , which means you can loop through each line/access string

November 1, 2016 at 12:51 pm

You could do something like this:

$data = Import-CSV C:\Users\Rob\Desktop\Archive\test.csv -Header User,Location,AccessFull

$data | 
Select User,
       Location,
       @{Name="Access1";Expression={$_.AccessFull[0]}},
       @{Name="Access2";Expression={$_.AccessFull[1]}},
       @{Name="Access3";Expression={$_.AccessFull[2]}},
       @{Name="Access4";Expression={$_.AccessFull[3]}},
       @{Name="Access5";Expression={$_.AccessFull[4]}},
       @{Name="Access6";Expression={$_.AccessFull[5]}}

November 1, 2016 at 4:07 pm

This is exactly what I needed. Thank you Rob.