New PowerShell Pluralsight Course

WatermarkLogo151x79I am so happy to announce that my first course under the Pluralsight badge has been released. My latest course is titled PowerShell v4 New Features. This course is aimed at IT Pros with previous experience using PowerShell, especially PowerShell v3. The course runs just under 3 hours (although it felt much longer than that while preparing it 🙂 ) and covers just the new bits that have changed from v3 including an introduction to Desired State Configuration (DSC). I provide enough information to get you started.

If you need to get caught up on v3 first, you can find my other courses on my author page. I hope you find my new course helpful.

Creating a DSC Configuration Template

During the recent TechEd in Houston, there was a lot of talk and excitement about Desired State Configuration, or DSC. I’m not going to cover DSC itself here but rather address a minor issue for those of you just getting started. When you build a configuration, how can your figure out what resource settings to use?

To start with, you can look use the Get-DSCResource cmdlet which will list all resources in the default locations.

get-dscresource

The properties for each resource are also there, although you have to dig a little.

get-dscresourceproperties

From the screen shot you can see which properties are mandatory and the possible values for the rest. The Get-DSCResource cmdlet will even take this a step further and show you the complete syntax on how to use a particular resource.

get-dscresourcesyntax

To build a configuration, you can copy, paste and edit. But I wanted more, as I often do when it comes to things PowerShell. So I wrote a function called New-DSCConfigurationTemplate that will generate a complete configuration with as many resources as you have available.

The function will create a configuration template with whatever resources you specify.
new-dscconfig01
By default the function writes to the pipeline, but you can specify a file name and even open the file in the ISE. Here’s how to create a single template for all DSC resources on your computer.

new-dscconfig02
Mandatory properties are preceded by a *, which you need to delete. Properties have all of the possible values or at the very least what type of value you need to enter. Simply delete what you don’t want and in no time you have a complete DSC configuration! Don’t forget to substitute a name in for without the <>.

I wrote this with the assumption that I could run this once to create a template, then copy and paste what I need into a new configuration. Another option would be to create ISE snippets for these resources.

Personally, anything that saves me typing is a great help. I hope you’ll let me know what you think.

Configure Local User Account with DSC

talkbubble Yesterday I posted an article on how to use PowerShell and the [ADSI] type accelerator to set a local user account. However, if you are running PowerShell 4.0 you have another option: Desired State Configuration (DSC).

I’m going to assume you have some basic understanding of how DSC works. If not, head over to the Public OneDrive folder for PowerShell.org and grab a copy of the free DSC ebook.

DSC ships with a provider resource for user accounts.

dsc-userresource

Because of the account password, the official stance is to use a certificate to handle encrypting the password. You can read about that on the PowerShell team blog. But that’s more than I want to deal with right now, plus I trust the security of my local test network so my configurations will store the passwords as plain text in the resulting MOFs. I suppose I should show you the script I came up with.

This script is intended to define a set of MOFs for computers in my Globomantics domain that all start with “CHI”. The script takes an array of computernames. From there it defines the configuration, defines the necessary configuration data to allow plain text passwords and then executes the configuration. The configuration also has a Group resource to add the account to the local Administrators group. Note the DependsOn setting in the group configuration. This ensures that the account will be set up before adding it to the group.

To create the configurations I run my script specifying the computer names.
config-localpassword-dsc

PowerShell will prompt me for the credentials. When finished I am left with a MOF file for each computer under C:\Scripts\LocalUserAccounts, because I specified an output path. When I’m ready, I can push the configuration to the servers:

And that’s it! I can verify using the NET USER command in a remote session.

verify-dsc-user

DSC promises to change the way IT Pros get their work done, and in a positive way!

Creating CIM Scripts without Scripting

When Windows 8 and Windows Server 2012 came out, along with PowerShell 3.0, we got our hands on some terrific technology in the form of the CIM cmdlets. Actually, we got much more than people realize. One of the reasons there was a big bump in the number of shipping modules and cmdlets was CDXML. With some work and a properly formatted XML file, you could create your own commands based on WMI and CIM classes. Modules like SMBShare were built the same way. But creating a CDXML based module is not a task for someone just getting started with PowerShell.

So I decided to build a tool that just about anyone could use to create their own CIM-based commands, using relatively common PowerShell scripting techniques. You might find some of the things I do in my script just as interesting or useful. I was also motivated because I know that many IT Pros want to script but don’t have the time or feel they don’t have the skills. I hope what I’ve come up with will help jump start or accelerate the process.

My script, CIMScriptMaker.ps1, will guide you through identifying a WMI class and create an advanced function to get instances of that class. You can even add a filter and select which properties to display. You can then edit the function further if you want and end up with a practical tool that didn’t take a lot of time to write. Let me walk you through the process. The script works in either the console or the ISE. I’ll launch the script from the prompt.

The script defaults to the local computer, but you can specify a remote computer running PowerShell 3 or later. The script then enumerates the namespaces and presents a list using Out-Gridview. I use the title as a prompt.

I’ll select Root\CimV2 and click OK. My script then queries all the classes in that namespace and again displays a list using Out-Gridview. What’s nice is that you can use the filtering capability to quickly find a class you are interested in.

I’m going to select Win32_PageFileUsage and click OK. Using the Popup method from the old Wscript.Shell VBScript object I prompt if the user wants to test retrieving all instances of the selected class.

Naturally, I do. If there are results, they will be displayed in Out-Gridview again.

The script will wait for me to close the gridview. After which I’ll be prompted to continue.

Next the script will display a list of properties for the class and ask if I want to filter.

If you opt to filter, you’ll get a gridview with a list of operators to choose from. The default is =. Then you’ll get a Visual Basic message box asking you to enter in a value. For this demo I clicked Cancel to skip filtering.

Next I am prompted to select the properties. Clicking cancel will in essence give you the full CIM object. If you select properties then behind the scenes I’m generating code that is piping a Get-CimInstance command to Select-Object. I’ll select a few.

And that’s it! The script will generate an advanced function based on the selections you have made. The function defaults to a name using the Get verb and the noun is the class name. The function is copied to the clipboard so that you can paste it into your script editor and also saved to a global variable, $cimscript, just in case you accidentally overwrite the clipboard

If you are in the console, you can right-click to paste it right into your current session. The function includes comment based help. The function can take computer names or you can use CIMSessions.

Here’s what the generated code looks like.

Now I have a CIM based tool that just works or I can develop it further.

Right now, all my script does is generate a command to get WMI information using Get-CimInstance. With a little more work I could probably have it generate commands that do things as well using WMI class methods. But that’s for another day. In the mean time, here is my script.

What do you think?

I Want PowerShell 4.0

021913_2047_WordTest1.pngI’ve been writing a bit about PowerShell 4.0 lately, especially on my Prof. PowerShell column. If you are running Windows 7 it isn’t too difficult to go to http://www.microsoft.com/en-us/download/details.aspx?id=40855 and download the Windows Management Framework 4 package. Remember you will also need version 4.5 of the .NET Framework. This will also apply to Windows Server 2012 and Windows Server 2008 R2. Win7 and Server 2008 both need to be at Service Pack 1. Of course, be sure the read the install notes so you don’t accidentally install yourself out of job by putting it on something like an Exchange server. That would be known as a bad thing.

But what if you are running Windows 8? In your eagerness don’t try and install the same update I’ve shown above. You will most likely get an error about an invalid certificate. It is a strange error that threw me the first time I saw it. A reader asked me about it which is why I dug back into this. What I neglected to remember at first is that the Windows Management Framework 4.0, which includes PowerShell 4.0, cannot be installed on Windows 8. If you want PowerShell 4.0, and frankly show doesn’t, you will need to take the free upgrade to Windows 8.1. That is the only way Windows 8 users can get PowerShell 4.0. And don’t forget while you are at it, to upgrade to the Windows 8.1 version of Remote Server Administration Tools.