We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Converting AD object to a string with Powershell

YZlat
YZlat asked
on
Medium Priority
3,360 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?
Comment
Watch Question

Commented:
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

CERTIFIED EXPERT

Author

Commented:
$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]?

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
Thank you!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.