Solved

Retrieve a shortened output from a field in PowerShell

Posted on 2011-09-10
6
615 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

687 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