Friday Fun: I’m with the band.

black-guitarI like to have fun with PowerShell, as is hopefully evident with this Friday Fun serious, and today that is especially true. Perhaps you need a quick break from the end of the week grind. Or maybe you want to learn something new about PowerShell. Hopefully today’s fun will meet both requirements. Today’s fun will include XML, scope and Switch. Let’s rock.

If you haven’t figured it out, rock ‘n roll is the theme for today. I have put together a little rock and roll quiz. In many rock bands there’s at least one member is well known. If someone says “Axl Rose” you most likely will know Guns n’ Roses. But would you recognize the other members of the band? I created an XML document with a number of well known rock bands. Because band members change, I tried to use the line ups from the bands peak years. If you want to play you will need to download BandData.xml. Save as an XML file to same directory as the script, which I’ll show you in a moment. Try not to peek too much at the contents. This is the structure.

My quiz is a PowerShell script that processes the data in the XML document. It will display a list of band members, without the recognizable lead and multiple choice of possible bands. After answering the questions you will be judged, I mean graded.

Let’s look at a few key points of the script.

First, I need to load the XML document.

The [XML] type accelerator will create an XML document. When you have an XML document in PowerShell, each node can be treated like a property so it is very easy to navigate or get values, like a list of all the band names.

The script then selects a random number of band entries from the XML document. These will be the basis of the quiz. For each item I create a list of band choices and band members that will be displayed. You’ll also noticed that I initialize some counters with the $script prefix.

Here’s why. I am using a scriptblock, defined as $promptblock, to display each question and keep track of correct answers. The scriptblock runs in a new scope, or container. That means when it tries to do something with a variable like $Q it first looks in the current scope for that item. If it finds it, it uses it. Otherwise PowerShell searches up the scope hierarchy to the parent scope looking for the item. But here’s what trips people up. If you are only reading, like I am, for things like the $bandhash object, PowerShell will happily find it in the parent scope and display it. But when I try to modify a variable like $Q or $i it can only modify it in the current scope. But I need to use those variables outside of the scriptblock scope, so I preface the variable with $Script: to indicate the scope level for those variables. The general rule is to not reference out-of-scope variables, but since I’m using $script: I’m telling PowerShell I know what I’m doing.

After running through all the questions, the script can calculate how many correct answers you had and present a score card. I decided to use a Switch statement to assist.

Normally in a Switch you would use a simple value. But you can also use PowerShell expressions. In my Switch statement, if the value of $Correct is >= 90, then I assign a certain value to $quip. When using expressions, use $_. Remember that Switch will process every matching expression and since I don’t want that, I’m using the Break keyword so PowerShell knows not to keep checking the other possibilities.

When you run the quiz, you will get an item like this:
bandquiz-1

The display is from the prompt scriptblock. If you need a little help, enter 0 which re-displays the question this time with the (hopefully) more recognizable lead.

bandquiz-2

And the final snarky commentary on your rock knowledge.
bandquiz-3

Because I am of a certain age, the contents of my band data xml file might be slightly skewed. If you were born after 1985 you might have some problems.

I think XML files scare some IT Pros but they really aren’t that difficult to work with once you understand some basics. In fact, I’ll be coming back to my band xml file in future posts. In the mean time, party on and let me know if you have any questions about my quiz script.

Find and Replace Text with PowerShell

magnifying-glass I’ve just finished up a series of tweets with a follower who had a question about finding and replacing a bit of data in a text file. In his case it was a web.config file but it really could be any text file that you can view in PowerShell. In PowerShell 3.0 and later this isn’t too difficult. Although I haven’t tested with really large files to determine if there are any limitations. Before you try anything I’m going to show you, please have a backup copy of of the original file.

Let’s say I have a file like this:

I need to change the IP source address in the connection string. As you probably guessed Get-Content is going to come into play. Normally when you use Get-Content you get an array of strings which adds a little complexity. But starting in v3 Get-Content has a -Raw parameter which writes the entire file as a single string. If we have a single string then we can easily use -Replace.

Your replacement pattern could even be a regular expression pattern. This could even be done as a one-liner:

For many of you, especially with unstructured text files, this technique should get the job done. But in this case, because the text file is also an XML document we could tackle the problem a different way. This is especially useful if you have several changes you want to make to the file.

First, create an XML document from the file.

We can navigate the document until we find the entry we need to change.

xmlconfig

We can assign a new value with revised connection string:

All that remains is to save the file back to disk.

Or, if you didn’t necessarily know the structure of your document, you could use an XPath query with Select-XML to find the node and then change the value.

The node properties will vary depending on your XML file so you will need to look at the object.
xmlnode

The XML approach is a bit more involved but is predictable and you don’t have to save the file until you have the XML just the way you want it.

One thing I will say about both techniques is that the more you understand how to use regular expressions in PowerShell the more you can accomplish. If you are feeling a little wobbly on the topic, there is an entire chapter on regular expressions in PowerShell in Depth.

Get PowerShell View Definitions

When you write objects to the pipeline in Windows PowerShell, at the end of the pipeline PowerShell’s formatting system handles displaying the results to the console. It accomplishes this by using a set of rules stored in XML configuration files. This is why when you run Get-Process you get a table with a pre-defined set of properties. But sometimes there are alternate views defined. For example, for process objects there is a table view called Priority. Once you know the view name you can use it.

[cc lang=”PowerShell”]
PS C:\> Get-Process | Format-Table -view Priority
[/cc]

The difficult part is finding what alternate views have been defined. For that, you can use my Get-View function. Continue reading

Creating ACL Reports

I saw a tweet this morning that was a PowerShell one-liner for capturing folder permissions to a text file. There’s nothing wrong with it but it’s hard to be truly productive in 140 characters so I thought I would take the idea and run with it a little bit. Here are some ways you might want to extend the concept. Continue reading

Get GPO Backup

The GroupPolicy module from Microsoft offers a great deal of functionality from a command line. In terms of regular maintenance or administration it is pretty hard to beat, especially if you have 100s or 1000s of GPOs. When you have such a large number, backing them up is critical and easy to accomplish with the Backup-GPO cmdlet. However, if you want to restore or import from a backup, you have to resort to the graphical Group Policy Managment Console. I couldn’t find any way to manage GPO backups from PowerShell so I wrote my own.

My function, Get-GPOBackup, will search a GPO backup folder and retrieve backup objects. All of this is done using the file system. Technically you don’t need the Group Policy module to retrieve the backup information. However, if you want to restore anything then you of course need the Group Policy module loaded. The only parameter you need to specify is the path. Although the function is designed to use the value of a global variable, $GPBackupPath. So if you use this alot, set that variable. But perhaps before we get too far you’d like to see the function.

The complete function has comment based help.

get-gpobackp help

get-gpobackp help

As I said, the -Path parameter is key. It is not mandatory in the attribute-sense, but I have code that prompts for a missing value and the path is tested before anything else is done.

GPO backups are stored under a GUID for each GPO, so I use a regular expression pattern to identify these. I’m making an assumption that the path you specify won’t have any other folders with GUID-based names.

Each backup consists of a few XML files. My function parses the XML and creates a custom object for each backup. These are added to a temporary array.

Now for the really fun part. The function supports a few other parameters that allow you to search for a GPO by name. So if you specify -Name, everything else is filtered out.

I also include a parameter to get the latest backup. I accomplish this by grouping the backup objects by their GPO GUID, sorting by backup date and selecting the latest one.

You should end up with an object like this for each GPO backup.

I tried to create an object so that if you wanted to restore a GPO you could with minimal effort. Unfortunately, the Import-GPO cmdlet doesn’t have enough support for parameter binding. So if you want to restore a backup, you’ll need to resort to a ForEach-Object command like this:

My function is in a stand-alone script so to use it, you either need to copy and paste the function into your profile or dot source the script. Even though I wrote it to help with imports, you could easily use it to manage a backup folder, deleting obsolete backups. The function is writing an object to the pipeline that you can use in whatever manner you can think of. I hope you’ll let me know what you think and if there is anything you think is missing.

Download Get-GPOBackup.