credog
asked on
Powershell Format Output to Screen
I need to gather some data in AD for a monthly report. I have the following code that could be better, but has worked for me. I've tried to expand it to output a comma separated list of users in addition to the formatted data. Heres the snippet I'm trying to expand and what I have so far:
*********** Users - OU Container ***********
Name: Joe Blow
Description: Admin User
Distinguished Name: CN=Joe Blow,OU=Administrators,OU= example,DC =EX,DC=ETC ,DC=ETC
UserAccountControl: 512
Name: Joe smith
Description: Regular User
Distinguished Name: CN=Joe Smith,OU=Users,OU=example, DC=EX,DC=E TC,DC=ETC
UserAccountControl: 512
blowsmith
============== Total for OU is 2 ===============
What I am trying to get to is to format the blowsmith portion to look like the following:
blow (admin), smith (user)
So, if the DN contains the word Administrators then append (admin) and if not just append (user). All separated by a comma. I've tried various ways and get close, but can't seem to put it all together.
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) | Out-Null}
$OU = $ou.ToUpper()
Write-Output "`n*********** Users - $OU Container ***********`n"
# Go search and and return the requested attrributes
$colResults = $objSearcher.FindAll()
# Loop through each record and grab the returned attributes,
# assign them to a variable and echo out values
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties
$counter++
"Name: " + $objItem.name
"Description: " + $objItem.description
"Distinguished Name: " + $objItem.distinguishedname
"UserAccountControl: " + $objItem.useraccountcontrol
Write-Output ""
#### New part trying to add. Basically get the last part of the name in the distinguishedname (i.e CN=Joe Blow, OU=example .... returns Blow) seems to work
$names1 += $objItem.name.Split(' ')[-1] -join "'"
}
#### Convert to lower
$names1.tolower()
#### Reset
$names1 = $null
Write-Output ""
Write-Output "============== Total for $OU is $counter ==============="
$total+=$counter
}
The output currently is this:*********** Users - OU Container ***********
Name: Joe Blow
Description: Admin User
Distinguished Name: CN=Joe Blow,OU=Administrators,OU=
UserAccountControl: 512
Name: Joe smith
Description: Regular User
Distinguished Name: CN=Joe Smith,OU=Users,OU=example,
UserAccountControl: 512
blowsmith
============== Total for OU is 2 ===============
What I am trying to get to is to format the blowsmith portion to look like the following:
blow (admin), smith (user)
So, if the DN contains the word Administrators then append (admin) and if not just append (user). All separated by a comma. I've tried various ways and get close, but can't seem to put it all together.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Apologies for not including all the script, was trying to save be concise. Next time I'll paste the whole thing.
footech, what you provided works great. One change would be good. The examples I provided have the Name in the following format Joe Blow. What you provided works great on that, however I didn't didn't notice that some of the names will be in the j.smith format.
Can the spit command be modified to get the last name in both formats, using both the space and . (dot) as the delimiter.
footech, what you provided works great. One change would be good. The examples I provided have the Name in the following format Joe Blow. What you provided works great on that, however I didn't didn't notice that some of the names will be in the j.smith format.
Can the spit command be modified to get the last name in both formats, using both the space and . (dot) as the delimiter.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
I'm not sure what are you getting the ADOBject instead of using the Get-ADUsers if they're just users and get what you need to export the way it should be...
What if you post the whole script and remove the sensitive information so we can help you with the re-creation of the script?