YZlat
asked on
Converting AD object to a string with Powershell
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.
I keep getting an error in this line:
$os = $item.operatingsystem.Trim End(" ")
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?
$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(" ")
}
I keep getting an error in this line:
$os = $item.operatingsystem.Trim
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?
ASKER
$os=$item.operatingsystem. tostring() .trimend()
worked for me, but also
$os = $item.operatingsystem[0].t rimend()
What is the difference between $item.operatingsystem and $item.operatingsystem[0]?
worked for me, but also
$os = $item.operatingsystem[0].t
What is the difference between $item.operatingsystem and $item.operatingsystem[0]?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
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.tost
Open in new window