Solved

Retrieve a shortened output from a field in PowerShell

Posted on 2011-09-10
6
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 70

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 70

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell help for creating accounts 283 87
Check if user is member of specific AD group 3 38
Health check of winows and webservces 5 48
Calling a function 4 22
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…

738 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