Solved

Retrieve a shortened output from a field in PowerShell

Posted on 2011-09-10
6
605 Views
Last Modified: 2012-05-12
I use a powershell command to retrieve information for all computers in my domain.
One of the fields is Parent container, that I labeled as location.

@{label=”Location”;expression={$_.ParentContainer}}

It returns something like

Domain/OU/OU/Computers

Is it possible to have it replace "Domain/OU/OU/Computers" with "Computers"?
0
Comment
Question by:LindyS
  • 2
  • 2
  • 2
6 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36516020
Of course. One way is to split at "/", and only take the last part:
@{label=”Location”;expression={$_.ParentContainer.Split("/")[-1]}}

Open in new window

0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36516078
Another option you could use get-qadobject and get the name of the OU

@{label=”Location”;expression={ (get-qadobject $($_.ParentContainerDN)).name}}
0
 
LVL 3

Author Comment

by:LindyS
ID: 36516083
That almost works. The problem is that some of the computer OUs have sub OUs.
Is there a way to remove everything before the name of one of the parent OUs?

Like this example
Domain/OU/OU/Computers. Would it be possible to just strip the portion containing Domain?

I played with a few variations of what you gave me but didn't have any luck.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:KenMcF
ID: 36516097
In that senerio I think what Qlemo posted would be best. Dependning on how your OU strucrture is you may need a few if statements to make sure you are getting the correct name and just filter out.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 36516109
Do you want to get  OU/OU/Computers  for the above example?
0
 
LVL 3

Author Closing Comment

by:LindyS
ID: 36516119
This is the best solution.
I had to change it to "@{label=”Location”;expression={$_.ParentContainer.Split("/")[-2]}} ", but for the most part it gets me what I need.

There are only a hand full of computers that are outside of the normal structure.

Thanks for the help!
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question