Advanced Windows PowerShell Scripting Video Training

Friday, April 17, 2015

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-CredentialMCTExpert\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.

Friday, April 10, 2015

OK, so I should not be working right now. It is Friday. I just finished teaching a 5 day Windows Server 2012 R2 class. It is sunny and 86 degrees. I should be outside or something. Like the nerd I am, this Navy Chief is hard at work. Albeit, in shorts and no shoes or socks since I am at home. Anyway, I took a quick look at the forms at PowerShell.com and noticed the stream about having bold face type in the ISE. I like the PowerShell ISE. It is fast and simple. That also means that it is light on the bells and whistles.

I also use Sapien’s PowerShell Studio 2015 so I decided to publish out one of my personal settings. Take a look at the Font and Color settings below.

The item is Unknown Command it is also the only item with a background color other that the editors background, which for me is black. I normally use TAB completion heavily, but I also do some weird stuff where TAB completion is not the best idea. So, every once and a while, I make a typo. Having the Unknown Command highlighted so dramatically really helps to alert me when I make that rare typo (insert rolling of the eyes). Anyway, as for that question on PowerShell.com, take another look at the image. You can select to have different items in the editor as bold face. See below for my highlight.

Yesterday on PowerShell.com, I had the opportunity to help an IT Pro pull the last logged on user from every one of their client machines. What we discovered is that there are two places in the registry that hold this information.

To do this one at a time would have been to time consuming. Also, what about the clients that were not online? How are you going to record them? Sitting in my Windows Server 2012 R2 class right now is an IT Pro with over 70,000 clients. This would have been a nightmare to perform manually. I am estimating that it would have taken about a minute per client to remote in, grab this information, and then move on. That means a total of 48 days of continuous work or 145 standard working days. I went ahead and enclosed this code inside of one of my tool templates to solve some issues and make this a bit of a more robust experience. Check out the help file.