?
Solved

Converting AD object to a string with Powershell

Posted on 2011-04-25
4
Medium Priority
?
2,606 Views
Last Modified: 2012-05-11
I am trying to retrieve a list of operating systems from all the machines in a particular OU and there is a space after each OS name and i wanted to use TrimEnd to remove it. I don't want to use Replace in case OS name contains a space in the middle.

$ou = [ADSI]"LDAP://OU=My OU,OU=My Group,DC=MyDomain,DC=com"
$alComputers=new-object Collections.ArrayList

foreach ($item in $alComputers){
	#Write-Host $item.name
	$os = $item.operatingsystem.TrimEnd(" ")

}

Open in new window


I keep getting an error in this line:

$os = $item.operatingsystem.TrimEnd(" ")

What am I doing wrong? I assume $item.operatingsystem is not a string but an object, so what can I do to convert it to a string?
0
Comment
Question by:YZlat
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35461989
The code is not correct, here is a working one.
But I do not see any extra space at the end of the name of the operating systems.

But in case you have, replace line 6 to:
$item.operatingsystem.tostring().trimend()
$ou = [ADSI]"LDAP://OU=My OU,OU=My Group,DC=MyDomain,DC=com"  
$alComputers=$ou.psbase.children
  
foreach ($item in $alComputers){  
        # Write-Host $item.name  
        $item.operatingsystem  
}

Open in new window

0
 
LVL 35

Author Comment

by:YZlat
ID: 35462283
$os=$item.operatingsystem.tostring().trimend()

worked for me, but also

$os = $item.operatingsystem[0].trimend()

What is the difference between  $item.operatingsystem and $item.operatingsystem[0]?

0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 35462509
$item.operatingsystem is a System.DirectoryServices.PropertyValueCollection type of object. PowerShell and the underlying ADSI com interface assumes a multivalued property for each AD attribute, so it gives this general datatype for AD attributes. That is why trimend method does not exist. First we have to convert this datatype to string.
Actually both solutions are OK, if it is a real multivalued property then the indexing is the better option, but here, as operatingsystem is a singlevalued property the tostring method is also perfect.
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 35466422
Thank you!
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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 anti-spam), the admin…
Loops Section Overview

755 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