Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Friday, April 17, 2015

Renaming Remote Clients

So I am finally getting around to posting some of the code from this weeks PowerShell class.  As always, I’m listening to my class and I am not afraid to deviate from the lessons when someone comes up with a good idea.  While teaching about PowerShell remoting, I was asked if it is possible to rename remote clients.  Sure it is!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

$Cred = Get-Credential -Credential MCTExpert\AdminAccount

$OldName = "CL1"

$NewName = "Client1"

 

Invoke-Command -ComputerName $OldName -ScriptBlock {

    Param($C, $NN)

    $Name = HostName

    $Splat = @{

        "NewName" = $NN

        "Restart" = $True

        "Force" = $True

        "PassThru" = $True

        "DomainCredential" = $C

    }

        Rename-Computer @Splat

 

} -ArgumentList $Cred, $NewName

Line Details
1 Ask for the domain credentials of someone who has the rights to rename the computer object in Active Directory
2 and 3 The new and old computer name. 
5 Creates a remote session with the client using the old name.
6 This is used to pass information from your client, to the remote client.  The first variable $C maps to the $Cred being passing in line 17.  The variable $NN maps to $NewName, also being passed in line 17.
7 Gets the host machine name for the Rename-Computer cmdlet.
8 This is where we create a splat for all the parameters that we will feed Rename-Computer.  If you go through the help file for Rename-Computer, you will see what each one does.
15 This is where we apply the splat and actually rename the computer.
17 This is where we pass information from your client, to the remote one.  The information is passed to the Param block on line 6.

This is very basic.  In practice, I would consider a CSV file containing one column with the old computer name, and another with the new name.  I would then pipe this CSV into a more advanced version of this code so I can make things happen a little faster and less manually.

No comments: