header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Append SamAccountName
Last Post 10 Sep 2008 03:14 PM by bigh1t. 12 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
bigh1tUser is Offline
New Member
New Member
Posts:9

--
22 Aug 2008 08:29 PM  

We are trying to isolate some suspected dead ID's and want to append a '-old' to the samaccountname variable. I can get to the distiguished name with the below line but I can't find a way to get to the SAM value. Any ideas?

get-content c:\Usertest.txt | Get-QADUser | % { Rename-QADObject -Identity $_.DN -NewName $( $_.Name + "-old") -WhatIf}

ShayUser is Offline
Basic Member
Basic Member
Posts:228

--
23 Aug 2008 03:04 PM  

Try:

get-content c:\Usertest.txt | Get-QADUser | foreach {
   Set-QADUser -SamAccountName ($_.SamAccountName+'-old')
}

bigh1tUser is Offline
New Member
New Member
Posts:9

--
23 Aug 2008 03:30 PM  

Thank you Shay, I appreciate the help. I am new to scripting and very new to Powershell and put the group rename together from other scripts. Could I have used the Set-QADUser function and made it simpler? Also, as always happens at work, is it easy to reverse the rename if I am required to do so?

bigh1tUser is Offline
New Member
New Member
Posts:9

--
23 Aug 2008 03:45 PM  

Getting this response when I run this:

 

cmdlet Set-QADUser at command pipeline position 1
Supply values for the following parameters:
Identity:

ShayUser is Offline
Basic Member
Basic Member
Posts:228

--
23 Aug 2008 03:54 PM  

You're welcome :)

I forgot to add the Identity parameter, so the code should look like:

get-content c:\Usertest.txt | Get-QADUser | foreach {
   Set-QADUser -identity $_ -SamAccountName ($_.SamAccountName+'-old')
}

 

Once you know what name you want to give, renaming is not a problem. For instance, to remove the '-old' suffix you can do this:

Get-QADUser -sizeLimit 0 -ldap '(samAccountName=*-old)' | foreach {
   Set-QADUser -identity $_ -SamAccountName ($_.SamAccountName.substring($_.SamAccountName.lastIndexOf("-"))
}

It retrieves all users that their SamAccountName attribute is ending with '-old', and then it is using the lastIndexOf() method to find all characetrs that occure before the  "-" char.

HTH 

 

bigh1tUser is Offline
New Member
New Member
Posts:9

--
23 Aug 2008 04:18 PM  
Another update.... When I plug in the userid at the Identity prompt it does make the change. The user name gets piped through Get-QADUser but doesn't seem to pipe to Set-QADUser.
bigh1tUser is Offline
New Member
New Member
Posts:9

--
23 Aug 2008 04:22 PM  
Should have known you'd have the answer by the time I tested. The IDENTITY parameter did the trick. Thank you Shay
bigh1tUser is Offline
New Member
New Member
Posts:9

--
09 Sep 2008 08:36 PM  

Good afternoon Shay,

   I just got around to testing the rename script you wrote. I get the following error:

You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:176
+ Get-QADUser -sizeLimit 0 -ldap '(samAccountName=*-old)' | foreach { Set-QADUser -identity $_ -SamAccountName ($_.SamAccountName.substring($_.SamAccountName.lastIndexOf("-")) -w <<<< hatif}

 

I ran::  Get-QADUser -sizeLimit 0 -ldap '(samAccountName=*-old)' | foreach { Set-QADUser -identity $_ -SamAccountName ($_.SamAccountName.substring($_.SamAccountName.lastIndexOf("-")) -whatif}

ShayUser is Offline
Basic Member
Basic Member
Posts:228

--
10 Sep 2008 07:51 AM  
Hi bigh1t

It worked for me the way I posted it. Anyway, a set of parenthesis can't do no harm :)
Glad you could resolve it.
bigh1tUser is Offline
New Member
New Member
Posts:9

--
10 Sep 2008 02:47 PM  

It's not resolved. And I amy be wrong adding the parathesis but that was the error that PS gave me.

    I just re-ran it this morning on my test system and it changed the 1st group name to -old. It doesn't seem to be carrying the samaccountname forward as desired.

ShayUser is Offline
Basic Member
Basic Member
Posts:228

--
10 Sep 2008 03:00 PM  
Can you try this instead, there was a missing closing parenthesis + I've changed the substring() value:

Get-QADUser -sizeLimit 0 -ldap '(samAccountName=*-old)' | foreach {
Set-QADUser -identity $_ -samAccountName ($_.samAccountName.substring(0,$_.samAccountName.lastIndexOf("-"))) -whatif
}
bigh1tUser is Offline
New Member
New Member
Posts:9

--
10 Sep 2008 03:11 PM  
Both with the -whatif and without it pops right back to the prompt with nothing happening.
bigh1tUser is Offline
New Member
New Member
Posts:9

--
10 Sep 2008 03:14 PM  

Never mind the previous reply. It works when you get the trailing '} ' copied in. :)

Thank you very much

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer