Solved

Retrieve a shortened output from a field in PowerShell

Posted on 2011-09-10
6
587 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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 27

Expert Comment

by:KenMcF
Comment Utility
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 68

Expert Comment

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

Author Closing Comment

by:LindyS
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now