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


Summary: Rusty the Red-Eyed Scripter learns how PowerShell can convert raw string data to an object.

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

Yesterday, our good friend Rusty found his head spinning with ideas after getting shown some new PowerShell cmdlets—cmdlets he could immediately put to work when using Windows PowerShell 5.0.

He decided to move to the front row with Thomas to get a better view. There was some powerful stuff coming on the screen, and he didn’t want to miss a bit. “And now we have a special treat for you folks,” the speaker started, “Here to introduce one of the coolest features in Windows PowerShell 5—your friend and mine, Ed ‘Hey, Scripting Guy’ Wilson!”

Rusty almost fell out of his chair. He’d heard of the mysterious Microsoft Scripting Guy, but he thought it was just some imaginary creature. Was there actually a real Scripting Guy?

“Hey everybody!” his voice boomed out with a smooth southern accent. “Welcome to PowerShell Saturday where I’m going to show you possibly some of the coolest features yet!”

Boy was Rusty excited. Here was the actual Scripting Guy from Microsoft about to introduce some PowerShell 5 bits. Was he glad he moved to the front row!

“Now!” Ed clapped his hands together, “There are some cmdlets you might have overlooked on the way to Windows PowerShell 5. How would you like an easy way to create a temporary file?”

Rusty didn’t get that one. You make a folder, put your file in there and...

As if reading his thoughts, the Scripting Guy continued, “New-TemporaryFile produces a blank file in the user’s temp folder with a proper .tmp extension.”

Ed demonstrated the use this cmdlet, which was a simple line that produced the following output:

New-TemporaryFile

Image of command output

“This cmdlet,” he continued, “not only creates the blank file in a proper temporary location, it also returns the path of the file for you to consume with your script or module.”

Rusty sat upright. “Such a cool and simple idea!” he burst out loud.

“Exactly! That’s the same thing I thought!” responded Ed. “Now if you thought that was cool, hold on to your seats for this one. So who here loves parsing log files manually?”

Strangely, not a single hand went up. Well one…it appears that Sean was being a bit of troublemaker.

“OK,” he smiled at Sean, “for those of us who don’t live on coffee…”

“Log files and large text files can be a royal pain. Especially files that aren’t in nice neat columns. This brings us to ConvertFrom-String, which is like magic!”

Eyebrows began to raise about the room. Thomas nudged Rusty, “You’re going to love this!”

“Here we have a simple text file that contains names and addresses. I could probably write something with regular expressions to parse it, but I’d rather be home relaxing with the Scripting Wife.”

Joe B Smith

906 110th Avenue

Seattle, WA

253-555-5938

Peter O Jones

545 Microsoft Way

Seattle, WA

425-555-2481

Mary J Beeblebrox

585 110th Avenue

Seattle, WA

425-555-1149

“As you can see, there is a pattern. But we have nothing to say, “Here’s the name, here’s the address, and so on. With ConvertFrom-String, I can convert this string array into an actual object including specifying types of data.”

Ed pulled up a small file called DBTemplate.txt. “In this file, I have added some parameters around actual data from our file. There’s a new engine feature in PowerShell that allows it to intelligently analyze the data and look for the same pattern.”

{Userinfo*:{First:Louise} {Middle:X} {Last:Vader}

{Street:1 Microsoft Way}

{City:Redmond}, {State:WA}

{Phone:360-555-4467}}

Joe B Smith

906 110th Avenue

Seattle, WA

253-555-5938

Peter O Claus

545 Microsoft Way

Seattle, WA

425-555-2481

“The first tag you’ll see is called Userinfo*:. This declares that this entire set of data is an object called Userinfo.” Ed pointed to the section on the screen.

“Here, where it says first, middle, and last,” he continued, “the engine is told to look at this pattern on the screen and try to find something similar. This could be a line with a bunch of letters, a space after, more squiggly letters, a space, more letters, and a carriage return at the end.”

{First:Louise} {Middle:X} {Last:Vader}

“Now to help the engine along, we give it more information such as defining as much of the chunks of data as we can. This way if it sees a number, a space, more letters, and things such as that, it might start to learn, ‘Hey, that looks like an address, dude!”

{Street:1 Microsoft Way}

“The rest of the information is to give the engine something to practice to see if it can learn the patterns. Real data from the actual file works best.” Ed paused dramatically before continuing.

“Now we can use ConvertFrom-String to save this file as dbtemplate.txt or whatever name you desire, and feed in the original file using this template.”

Get-Content C:\Foo\DB.txt | ConvertFrom-String –template dbtemplate.txt

Image of command output

“This is now an object that I can parse in PowerShell like this:”

$Info=Get-Content C:\Foo\DB.txt | ConvertFrom-String –template dbtemplate.txt

$Info.UserInfo | Get-Member

Image of command output

“TaDa! It’s now all one giant PowerShell custom object!”

Ooooohs and aaahhhhssss could be heard across the room. Rusty piped up, “And could I somehow get it to define that an object is an integer?”

“Oh yes sir, you can! Here’s that same dbtemplate.txt file with the [string] types added in. It presumes a string, but you can use almost any built-in PowerShell type—integer, date, time, whatever!”

{Userinfo*:{[STRING]First:Louise} {[STRING]Middle:X} {[STRING]Last:Vader}

{[STRING]Street:1 Microsoft Way}

{[STRING]City:Redmond}, {[STRING]State:WA}

{[STRING]Phone:360-555-4467}}

Rusty’s mind was churning with ideas. “Is there some way to tell PowerShell that when I see a field such as the phone number, that it is actually a phone number?”

“That there is,” smiled Ed, “and our class about classes begins soon.”

Join us tomorrow when Ed and his friends show Rusty one of the coolest changes in Windows PowerShell 5.0.

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 

Comments (0)

Skip to main content