header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Remove Spaces In Various Attributes
Last Post 21 Jan 2010 01:48 AM by James. 15 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
JamesUser is Offline
Basic Member
Basic Member
Posts:398
Avatar

--
09 Jan 2010 05:35 AM

    Hello,

    I am looking for a way to search by the title of student in AD and then from there find any accounts which have a space in the samaccountname, proxyaddresses, cn and then just remove the space I would like to do this for the entire domain.

    Does anyone know how to do this?

    I have tried the following:



    #Add-PSSnapin Quest.ActiveRoles.ADManagement $AllOUs = Get-QADObject -Type organizationalUnit -SearchRoot "stockport.sch.uk/Schools" -SearchScope OneLevel foreach ($ou in $AllOUs) { $Users = Get-QADUser -sizelimit 0 -SearchRoot $searchroot | where {$_.title -match "student" -and $_.samaccountname -match "\s"} } If ($users -eq $null) {Write-Warning "No match found...!"} foreach ($user in $users) { Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s") Set-QADUser -id $user -cn ($user.cn -replace "\s") Set-QADUser -id $user -proxyaddresses ($user.proxyaddresses -replace "\s") Set-QADUser -id $user -targetaddress ($user.targetaddress -replace "\s") }



    However must be going through the students or something as I get lots of errors like:

    Get-QADUser : The argument cannot be null or empty. At C:\removespaces.ps1:6 char:46 + $Users = Get-QADUser -sizelimit 0 -SearchRoot <<<< $searchroot | where {$_.t itle -match "student" -and $_.samaccountname -match "\s"} Get-QADUser : The argument cannot be null or empty. At C:\removespaces.ps1:6 char:46 + $Users = Get-QADUser -sizelimit 0 -SearchRoot <<<< $searchroot | where {$_.t itle -match "student" -and $_.samaccountname -match "\s"}

    Then eventually I get:

    WARNING: No match found...!

    Followed by some more of the above errors and then it just stops.

    Can anyone explain?

    Many Thanks

    James

    Vishal RamnaniUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    11 Jan 2010 03:56 AM
    Hey,

    This was throwing error because you were using a variable which you didn't define. but again that may give you an error because few parameters used by you are not in Set-QADUser cmdlet.

    Try below...

    #Add-PSSnapin Quest.ActiveRoles.ADManagement $searchroot = "stockport.sch.uk/Schools" $Users = Get-QADUser -sizelimit 0 -SearchRoot $searchroot | where {$_.title -match "student" -and $_.samaccountname -match "\s"} If ($users -eq $null) {Write-Warning "No match found...!"} foreach ($user in $users) { Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s") }

    Can you please explain what attributes you are trying to set with these parameters?
    -cn
    -proxyaddress
    -targetaddress

    Thanks.
    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    11 Jan 2010 04:06 AM
    Hello,

    We setup the targetaddress, cn and proxyaddress attributes for the user in AD for part of our whole setup to do with Microsoft and how they want us to set things up.

    We would need the attributes to have no spaces in them so that we can get the automated process from out AD to Microsoft working. Since the provision before hnd is beinging in spaces on students and such we need to remove them before it will go of to microsoft.

    Does this make any more sense?

    Many Thanks

    James
    Vishal RamnaniUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    11 Jan 2010 09:55 PM
    removing space is fine but as far as the these are attribute are concerned, i dont thing you will be able to set it up by Set-QADuser cmdlet.

    Just type "Get-Help Set-QADUser -detail" and you will be given a list of parameters you can use to set AD attributes. take that parameter which you want to use, provide value and set it. Also you don't need to use multiple Set-QADUser cmdlets here. just provide all parameter to single cmdlet and you should be fine.

    Thanks.

    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    12 Jan 2010 12:21 AM
    Oh right ok.

    We have set some attributes before to the target and proxy addresses before.

    I have just found the script I used (from some help in here) :

    Set-QADUser -Identity $_.Name -ObjectAttributes @{TargetAddress=$TargetAddress}

    So would something like:

    Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s")
    Set-QADUser -Id $user -ObjectAttributes @{cn = -replace "\s"}
    Set-QADUser -id $user -ObjectAttributes @{proxyaddresses = -replace "\s")
    Set-QADUser -id $user -ObjectAttributes @{targetaddress = -replace "\s")

    If so.. I am unsure how to combine the 3 objectattributes together into 1 line.

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    12 Jan 2010 04:22 AM
    Or is it like this? (After a google search eventually finding it)

    Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s")
    Set-QADUser -Id $user -ObjectAttributes @{cn = -replace "\s"; proxyaddresses = -replace "\s"; targetaddress = -replace "\s"}

    Many Thanks

    James
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:352
    Avatar

    --
    12 Jan 2010 05:17 AM
    Hi James,

    Would this do it for you? In the replace statements I use the single-quote character. ' ' '' is

    'single-quote space single-quote'

    -and-

    'single-quote single-quote'

    get-qaduser -IncludedProperties samaccountname,proxyaddresses,cn -LdapFilter '(description=student)' | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()}
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    12 Jan 2010 05:24 AM
    Hello,

    I have included targetaddress to... and the AD Attribute which is set to student is title...

    Would this now be correct?

    Get-QADUser -IncludedProperties samaccountname,proxyaddresses,targetaddress,cn -LdapFilter '(title=student)' | %{$_.samaccountname = $_.samaccountname.replace(' ','').trim();$_.cn = $_.cn.replace(' ','').trim();$_.targetaddress = $_.targetaddress.replace(' ','').trim()$_.proxyaddresses = $_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','').trim()});$_.commitchanges()}
    }

    Also could I test it on just 1 user to make sure its working as I expect before doing the entire domain?

    Many Thanks

    James
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:352
    Avatar

    --
    12 Jan 2010 05:33 AM
    Hi James,

    I edited my post because there were a couple of problems. First I had $_.proxyaddresses = $_.proxyaddresses = $(...

    I corrected that.

    I also realized that you don't need the addtional .trim() if all you are worried about is spaces the replace will catch all spaces.

    To use it on just one user get rid of the -ldapfilter paramater and do this:

    get-qaduser -includeproperties samaccountname,proxyaddresses,targetaddresses,cn | ...

    see if it works then put the ldap filter back in without the user name.

    Here is the command with targetaddress you might need to add the -sizelimit parameter to this as well.

    get-qaduser -IncludedProperties samaccountname,proxyaddresses,targetaddress,cn -LdapFilter '(description=student)' | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()}

    Here is the command without the ldapfilter (replace with actual user).

    get-qaduser -IncludedProperties samaccountname,proxyaddresses,targetaddress,cn | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()}

    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:352
    Avatar

    --
    12 Jan 2010 05:38 AM
    I put username inside of greater and less than brackets and the page interpreted it as a tag. Here is is again using quotes instead for single user (just replace the username with an actual name):

    get-qaduser 'username' -IncludedProperties samaccountname,proxyaddresses,targetaddress,cn | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()}
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:352
    Avatar

    --
    12 Jan 2010 05:40 AM
    Ah I just caught the -LdapFilter error I made here is the search again:

    get-qaduser -IncludedProperties samaccountname,proxyaddresses,targetaddress,cn -LdapFilter '(title=student)' | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()}
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:352
    Avatar

    --
    12 Jan 2010 05:54 AM
    One other thing and you probably already know it...you can speed up the query by adding a -searchroot and specifying the OU to search in and if you have more than 1000 students then you will need to specify the -sizelimit parameter to get all of the users.
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    14 Jan 2010 12:41 AM
    Hello,

    Thanks you I have give it a go and its changed the samaccount name but this is all.

    Its runs fine but does not change the CN, Proxyaddresses, and targetaddress. I have also found it will need changing in the mail attribule as well.

    I hope this makes sense.

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    16 Jan 2010 05:24 AM
    Hello,

    I have tried all the following:


    #Add-PSSnapin Quest.ActiveRoles.ADManagement #$AllOUs = Get-QADObject -Type organizationalUnit -SearchRoot "domain/ou" -SearchScope OneLevel #foreach ($ou in $AllOUs) #{ #$Users = Get-QADUser -sizelimit 0 -SearchRoot $searchroot | where {$_.title -match "student" -and $_.samaccountname -match "\s"} #} #If ($users -eq $null) {Write-Warning "No match found...!"} #foreach ($user in $users) { #$user = Get-QADUser -Identity "4039butcher - pol" #Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s") #Set-QADUser -Id $user -ObjectAttributes @{cn = $_.cn.Replace(' ','')} #Set-QADUser -id $user -ObjectAttributes @{proxyaddresses = $_.proxyaddresses.Replace(' ','')} #Set-QADUser -id $user -ObjectAttributes @{targetaddress = $_.targetaddress -replace(' ','')} #Get-QADUser '4039butcher - pol' -IncludedProperties samaccountname,proxyaddresses,targetaddress,cn | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()} $user = Get-QADUser '4039butcher - pol' # Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s") # Set-QADUser -id $user -ObjectAttributes @{targetaddress = $_.targetaddress -replace " ",""} # Set-QADUser -id $user -ObjectAttributes @{proxyaddresses = $_.proxyaddresses -replace " ","" } # Set-QADUser -id $user -ObjectAttributes @{cn = $user.cn -replace "\s"} Set-QADUser -id $user -IncludedProperties samaccountname,targetaddress,cn,proxyaddresses |%{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()} Write-Host "Complete" #if($user.cn -contains " "){$user.cn -replace " ", ""} #}


    I Can get it to change the samaccount name but nothing else.

    It will blank out the target and proxyaddresses attributes and it just errors when trying the CN...

    Can anyone assist with this?

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    18 Jan 2010 11:36 PM
    Hello,

    Can anyone assist on this matter? I am at a loss and dont know where its going wrong.

    All help is greatly appreciated.

    Many Thanks

    James
    JamesUser is Offline
    Basic Member
    Basic Member
    Posts:398
    Avatar

    --
    21 Jan 2010 01:48 AM
    Can anyone shed any light on this? as its not working and I dont know any other way of doing it.

    my code is:

    #Add-PSSnapin Quest.ActiveRoles.ADManagement #$AllOUs = Get-QADObject -Type organizationalUnit -SearchRoot "domain/ou" -SearchScope OneLevel #foreach ($ou in $AllOUs) #{ #$Users = Get-QADUser -sizelimit 0 -SearchRoot $searchroot | where {$_.title -match "student" -and $_.samaccountname -match "\s"} #} #If ($users -eq $null) {Write-Warning "No match found...!"} #foreach ($user in $users) { #$user = Get-QADUser -Identity 'user.name - here' #Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s") #Set-QADUser -Id $user -ObjectAttributes @{cn = $_.cn.Replace(' ','')} #Set-QADUser -id $user -ObjectAttributes @{proxyaddresses = $_.proxyaddresses.Replace(' ','')} #Set-QADUser -id $user -ObjectAttributes @{targetaddress = $_.targetaddress -replace(' ','')} #Get-QADUser 'user.name - here' -IncludedProperties samaccountname,proxyaddresses,targetaddress,cn | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.cn = $_.cn.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()} #$user = Get-QADUser 'user.name - here' #Write-Host $user.targetAddress #$user.targetAddress -match "\s" #Get-QADUser 'user.name - here' -IncludedProperties samaccountname | %{$_.samaccountname = $_.samaccountname.replace(' ','');$_.commitchanges()} Get-QADUser 'user.name - here' -IncludedProperties targetaddress | %{$_.TargetAddress = $_.TargetAddress.replace('\s','')| %{$_.replace('\s','')};$_.commitchanges()} ##if($user.targetAddress -match "\s") ##{$_.TargetAddress = $_.targetAddress.replace("\s",'')} #Set-QADUser -Identity $user -ObjectAttributes @{TargetAddress=TargetAddress -replace(' ','')} #Get-QADUser 'user.name - here' -IncludedProperties proxyaddresses | %{$_.proxyaddresses = @($_.proxyaddresses -replace(' ',''));$_.commitchanges()} # Set-QADUser -id $user -samaccountname ($user.samaccountname -replace "\s") # Set-QADUser -id $user -ObjectAttributes @{targetaddress = $_.targetaddress -replace "\s"} # Set-QADUser -id $user -ObjectAttributes @{proxyaddresses = $_.proxyaddresses -replace "\s"} # Set-QADUser -id $user -ObjectAttributes @{cn = $user.cn -replace "\s"} #Set-QADUser -id $user -IncludedProperties samaccountname,targetaddress,proxyaddresses |%{$_.samaccountname = $_.samaccountname.replace(' ','');$_.targetaddress = $_.targetaddress.replace(' ','');$_.proxyaddresses = @($_.proxyaddresses | %{$_.replace(' ','')});$_.commitchanges()} Write-Host "Complete" #if($user.cn -contains " "){$user.cn -replace " ", ""} #}

    It comes up with no errors however its not replacing the target address.

    All the hashed out things are various attempts I have had.

    Does anyone know why this is?

    Many Thanks

    James
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer