?
Solved

Retrieve a shortened output from a field in PowerShell

Posted on 2011-09-10
6
Medium Priority
?
619 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

765 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