Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How to regex a table output

$
0
0

Hey guys,

So I need a script that runs on a client machine to get the person's username and then, based on that, his OU in AD. This is what I have thus far:

$username=$env:USERNAME

$pathOutUser="C:\Scripts\UsAer_Logger\Output\userTemp\$username.txt"

$userProperties=Get-ADUser $username

Add-Content $pathOutUser $userProperties

$userPath = Resolve-Path "C:\Scripts\UsAer_Logger\Output\userTemp\$username.txt"
if (Test-Path $userPath)
{
  $txt=[IO.File]::ReadAllText($userPath)
  $userParse = [regex]::Match($txt,'OU=(.*)').Groups[1].Value
}

The output of Get-AdUser $username looks like this:

DistinguishedName : CN=Alexander Bazzi,OU=IT,OU=Coral Gables,OU=US,DC=corp,DC=aersale,DC=com
Enabled           : True
GivenName         : Alexander
Name              : Alexander Bazzi
ObjectClass       : user
ObjectGUID        : 96b6b7a2-03d1-4275-b6dc-9239a2774275
SamAccountName    : abazzi
SID               : S-1-5-21-3137774707-1406588056-219970087-3909
Surname           : Bazzi
UserPrincipalName : abazzi@corp.aersale.com

What I need is a way to parse through this text and get the OU of the user (IT, in this case). Am I in the right track? How would you do it? Is there a way to do it in the pipe, without having to save the table to a text file and then read it?

Many thanks in advance,

Alex


Viewing all articles
Browse latest Browse all 15028

Trending Articles



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