#1 01 Dec 2011 22:07

carltonjacobson
Member
Registered: 22 Nov 2011
Posts: 7

Select-String with -context to parse user data in a text file to vars

I have a text file filled with user data that I need pull from with data like:

dn:CN=Jacobson\, Carlton
>sn: Jacobson
>givenName: Carlton
>displayName: Jacobson, Carlton
>userAccountControl: ###
>employeeID: ###
>objectSid: ###
>sAMAccountName: ###
>mail: Carlton.Jacobson@###.com

I used the following code to get some values:

$patternName = "Jacobson, Carlton"

$userInfo = Select-String $adfindPath -pattern $patternName -context 3, 5
$userInfo = $userInfo.Context.DisplayPreContext + $userInfo.Line + $userInfo.Context.DisplayPostContext
$userID = getADFINDValue $userInfo ">sAMAccountName: "
$mail = getADFINDValue $userInfo ">mail: "
$firstname = getADFINDValue $userinfo ">givenName: "
$surname = getADFINDValue $userinfo ">sn: "

But what if there was variable amount of information and I didn't want to pull information from another person's account?  The problem cannot be solved very simply because I do not know what the dn: will be, so I cannot just find that then find the next instance of mail: and get everything in between.  I think you could do it using IndexOf but I'm not going to go there, yet.  Also, I think my parsing into variables is a bit crude.

Offline

#2 01 Dec 2011 22:25

carltonjacobson
Member
Registered: 22 Nov 2011
Posts: 7

Re: Select-String with -context to parse user data in a text file to vars

Including my simple function to strip out the data:

Function getADFINDValue ($ADinput, $ADvalue)
{ 
   $output = $ADinput | select-string -Pattern $ADvalue
   return $output.Line.Replace($ADvalue, "")

}

Offline

Board footer

Powered by FluxBB