need assistance with a quest ADCMDlets script to rename computers in bulk

we have approx. 500 systems to rename to a standard naming convention.

we located this article and script:
http://deployhappiness.com/powershell-script-to-change-computer-names/

However, the script concerns me as it first removes / deletes the computer object from AD.
is there a way to build some logic into this so that it first tests access to the target machine? for example, if the domain admin account we are using didn't have access to a particular system, the script would delete the computer object, then fail to run on the machine. leaving us in a bad situation.

also any additional logic you think would safeguard us would be helpful.


$Cred= Get-Credential
 
$CSV = Import-Csv "U:\LAB.csv" -Header OldName, NewName
Foreach ($name in $CSV)
 
{
$ComputerName = Get-QADComputer $Name.NewName
$GroupMemberships = Get-QADComputer $Name.NewName | Get-QADMemberOf
$OUMembership = Get-QADComputer $Name.NewName | Select-Object ParentContainer
 
Remove-QADObject -Identity $ComputerName
 
Start-Sleep 10
 
Rename-Computer -ComputerName $name.OldName -NewName $name.NewName -DomainCredential $Cred -Force -Restart
 
Start-Sleep 5
 
foreach ($GroupMembership in $GroupMemberships)
{Add-QADMemberOf -Group $GroupMembership -Identity $ComputerName }
 
Move-QADObject -Identity $ComputerName -NewParentContainer $OUMembership.ParentContainer
}

Open in new window

siber1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
Here is a sample code.. I presume $name.OldName is the computer which you want to check the permissions.. Basically the script will check if the Domain admins are member of The local administrators group, and if yes. Then it will process the rename.. Check and see if it work as expected..

$Cred= Get-Credential
 
$CSV = Import-Csv "U:\LAB.csv" -Header OldName, NewName
Foreach ($name in $CSV)
{
$Group = $null
$Server = $name.OldName
If (Test-Connection $Server -Quiet) {
$Group = ([ADSI]"WinNT://$Server/Administrators,group").psbase.Invoke("Members") | % {
		$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
	} | ? {$_ -eq "Domain Admins"}
}

If ($Group -eq "Domain Admins"){

	$ComputerName = Get-QADComputer $Name.NewName
	$GroupMemberships = Get-QADComputer $Name.NewName | Get-QADMemberOf
	$OUMembership = Get-QADComputer $Name.NewName | Select-Object ParentContainer
	 
	Remove-QADObject -Identity $ComputerName
	 
	Start-Sleep 10
	 
	Rename-Computer -ComputerName $name.OldName -NewName $name.NewName -DomainCredential $Cred -Force -Restart
	 
	Start-Sleep 5
	 
	foreach ($GroupMembership in $GroupMemberships)
	{Add-QADMemberOf -Group $GroupMembership -Identity $ComputerName }
	 
	Move-QADObject -Identity $ComputerName -NewParentContainer $OUMembership.ParentContainer
 }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
siber1Author Commented:
thank you Subsun. I will test that in the lab today.
0
siber1Author Commented:
Hi Subsun,

we encountered a problem running the script.

here is the error that we are seeing:

[PS] C:\drop>.\computer-rename.ps1

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Exception calling "Invoke" with "2" argument(s): "The network path was not found.
"
At C:\drop\computer-rename.ps1:9 char:70
+ $Group = ([ADSI]"WinNT://$Server/Administrators,group").psbase.Invoke <<<< ("Members") | % {
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Open in new window


please advise how we can correct this?


thx - S.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

SubsunCommented:
Probably the computer does not have he group Administrators or the Administrators group is empty.. Does this error terminate the script?
0
siber1Author Commented:
Hi Subsun,

I just verified that the "Administrators" group exists and the Domain admins is a member.

I'm running this from the Quest AD Cmdlets shell, do I need to import any modules into it first? for example, "rename-computer" is not recognized.

here are the errors:

[PS] C:\drop>.\computer-rename.ps1

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Remove-QADObject : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Supply an argument
that is not null or empty and then try the command again.
At C:\drop\computer-rename.ps1:20 char:28
+     Remove-QADObject -Identity <<<<  $ComputerName
    + CategoryInfo          : InvalidData: (:) [Remove-QADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Commands.RemoveOb
   jectCmdlet

The term 'Rename-Computer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
 the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\drop\computer-rename.ps1:24 char:17
+     Rename-Computer <<<<  -ComputerName $name.OldName -NewName $name.NewName -DomainCredential $Cred -Force -Restart
    + CategoryInfo          : ObjectNotFound: (Rename-Computer:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Add-QADMemberOf : Cannot validate argument on parameter 'Group'. The argument is null or empty. Supply an argument that
 is not null or empty and then try the command again.
At C:\drop\computer-rename.ps1:29 char:25
+     {Add-QADMemberOf -Group <<<<  $GroupMembership -Identity $ComputerName }
    + CategoryInfo          : InvalidData: (:) [Add-QADMemberOf], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Commands.AddMembe
   rOfCmdlet

Move-QADObject : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Supply an argument th
at is not null or empty and then try the command again.
At C:\drop\computer-rename.ps1:31 char:26
+     Move-QADObject -Identity <<<<  $ComputerName -NewParentContainer $OUMembership.ParentContainer
    + CategoryInfo          : InvalidData: (:) [Move-QADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Commands.MoveObje
   ctCmdlet

Open in new window

0
siber1Author Commented:
hi Subsun, I found another way to do it:
http://social.technet.microsoft.com/wiki/contents/articles/2243.how-to-rename-computers-using-powershell-and-a-csv-file.aspx

works perfectly.

appreciate your assistance. have a great day.
0
SubsunCommented:
For Rename-Computer you need PowerShell 3.0
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.