A Holiday Special: Rusty the Red-Eyed Scripter, Part 3

Doctor Scripto

Summary: Rusty the Red-Eyed Scripter learns how to unblock files and tweak event logs with Windows PowerShell 5.0.

  Note   This is a five-part series that includes the following posts:

Our story continues as we observe Rusty and getting his first real taste of Windows PowerShell 5.0 and discovering the power and ease-of-use it brings.

Rusty and Thomas have been watching the PowerShell Saturday speaker get deeper into PowerShell—until a quick pizza break is called.

Everybody ambles to the back of the room to scoop up some pizza and sodas, and the chatting begins. Rusty is chatting with Thomas, “I’d love to get five minutes with the speaker and thank him. This is going to make my daily tasks far easier. I’m starting to get my hands on some larger 200+ user sites!”

Thomas looks and gestures down the hall, “Come on. I’ll introduce you. He’s a fellow MVP.”

Rusty walks with Thomas to meet the speaker. From out of the corner he can hear the fellow with the green tilley hat shouting, “Remember Sean, no coffee and no sugar!”

The fellow being spoken to was none other than the speaker. Thomas introduce them.

“Sean, this is my good friend Rusty. He’s just getting into PowerShell 5, and he wanted to say ‘Hi!’”

Sean reached out to shake Rusty’s hand. His voice was almost as excited as his on stage mannerisms, “Hey, Rusty! Woohoo! PowerShell rocks!”

Rusty thought to himself, “No wonder that fellow told him no coffee!”

He introduced himself as “The Energized Tech” and a fellow who shared a strong past with Rusty. He too used to work crazy long hours and drove cars held together by bits of string and hope.

“Yes, I vividly remember those days, and then I encountered PowerShell. I finished my work in 15 minutes, billed the two-hour minimum, and wrote a song about it—and I barely knew the technology!”

Rusty’s eyeballs popped open! “You wrote THAT song?”

“Shhhh!!!!! Not so loud! Groupies!” Sean gave him a wink. “So if you’ve got a few minutes, there are some cmdlets in PowerShell 5 you’ll really love if you’re working in the field.”

Rusty stared at the screen as the magic began to unfold before him.

“I remember clearly downloading big ZIP files from providers, extracting the content, and then halfway into the installation, finding an error. Do you know what the problem was? The ZIP file was a blocked file like this one:”

Rusty nodded his head in agreement, having also encountered that problem. The solution was to select Unblock, hit Apply, and again extract the archive. “The solution wasn’t difficult, but it meant click, click, click, when all I really wanted was to say ‘UnBlock this!’ Well, now we can by using the new Unblock-File cmdlet like this:”

UnBlock-File C:\Foo\Filename.zip

“Now!” Sean began to rub his hands together like a gerbil, “How would you like an easy way to compare if two files are exactly the same?”

“Hmmm?” Rusty raised his left eyebrow quizzically. Normally such a process would involve a byte-by-byte comparison of the files.

“In PowerShell 5, we can use the Get-FileHash cmdlet, which produces a very quick and high-level SHA-256 hash of any file in question. So I can do this on any two files to see if the content is identical:”

$Hash1=Get-FileHash C:\foo\Filename1.txt

$Hash2=Get-FileHash C:\foo\Filename2.txt

If ($Hash1.Hash –ne $Hash2.Hash) { Write-out ‘Files are different!’ }

Rusty already could see a use for this. He had client with an application that wrote severe crashes to a log. With this cmdlet, he could track only when that log file changed.

“Ooooo! I’ve got one you’re going to love,” continued an excited Sean, “How would you like an easy way to see all the hotfixes on a machine?” He sat down and entered a new PowerShell 5 cmdlet called Get-Hotfix. “This cmdlet will show you all the hotfixes on a local machine.”

Image of command output

“But,” he continued, “You can also target a remote workstation by using the computer name and credential parameters! This example will prompt you for the local Admin credentials on a computer called SomeRemotePC and tell you the hotfixes that are on it:”

Get-Hotfix –computername SomeRemotePC –Credential (Get-credential)

“But wait!” he shot his hand up like a game show host, “There’s more! I can configure event log settings from Windows PowerShell!”

Rusty sat up for this. He knew all too well what it was like to investigate the event logs on a server and find that they were too small or overwriting too quickly. He was forever tweaking and bumping sites.”

“We can now use Limit-EventLog cmdlet in PowerShell 5 to crank up the security event log! How about a 2 gigabyte security log on the fly?”

Limit-Eventlog –Logname Security –OverflowAction OverWriteasNeeded –MaximumSize 2GB

“….and just like with previous cmdlets, I can use the ComputerName parameter to target and tweak remote systems!”

Rusty was just jumping up and down when we saw this. He immediately pictured scheduling calls to all of his sites to tweak the appropriate logs. Instead of checking them all, he could run a quick script!

“We’ll chat again in a bit,” Sean said as he offered his hand to shake, “but I’ve got to pop back to continue the presentation.”

Poor Rusty! Now his eyes were red—not from lack of sleep, but from all the cool thoughts buzzing through his head!

Oh my! What will happen tomorrow? How much more of the amazingly cool features in Windows PowerShell 5.0 will Rusty be able to handle? Stay tuned!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP

0 comments

Discussion is closed.

Feedback usabilla icon