Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell file manipulation

$
0
0

Yet another one of these things we do .VBS without a hitch, but I (and others in my team) can't wrap our heads around how to do it in Powershell, so instead of wasting countless man-hours on this, I figured getting some extra help can't hurt.

This should be incredibly easy to do... we have a massive input file (only will give small example) that we need reformated to a single line of data per group.

File looks like this:

GroupDN,ManagedBy,IdentityReference
NA-ManagedBy2010,Test.user1,domain\someone1
NA-ManagedBy2010,Test.user1,domain\someone2
NA-ManagedBy2003,Test.user3,domain\user23
NA-ManagedBy2003,Test.user3,domain\user32
NA-ManagedBy2003,Test.user3,domain\guy40

What we're looking for is really darn straightforward: don't duplicate the group, don't duplicate the manager, but concatenate at the end of the line every user, so that it would look like:

GroupDN,ManagedBy,IdentityReference
NA-ManagedBy2010,Test.user1,domain\someone1;domain\someone2
NA-ManagedBy2003,Test.user3,domain\user23;domain\user32;domain\guy40

Here's what we have, which technically gives us what we want buried in the output

$file = Import-Csv d:\outfile.csv
$groupcomp = $null
foreach ($line in $file)
	{
	if ($groupcomp -eq $line.groupdn)
 		{
 		$write = $write + ";" + $line.identityreference
 		}
 	else
 		{
 		$write = $line.groupdn + "," + $line.managedby + "," + $line.identityreference
 		$groupcomp = $line.groupdn
  		}  	 
	$write
	}

Execpt that we haven't figured out how to spit out JUST the last line of the whole concatenating process before going to the next one.  Already have it running in .VBS in about 25 lines, but I've never seen a powershell script take nearly as much or more than .VBS, so I'm thinking we're not going about this in an efficient way at ALL.

Really, really hard to get my brain to get out of VBS thinking patterns >_<

Thanks


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>