Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use SMBShare resources with Windows PowerShell DSC.
Microsoft Scripting Guy, Ed Wilson, is here. Today the Scripting Wife and I are doing something a bit different. We are on a high-speed train to Leipzig, Germany. I am sipping a cappuccino, munching on a piece of dark chocolate cake, and listening to The Marriage of Figaro on my Zune. The Scripting Wife is munching on a gigantic pretzel and sipping a Coke. Every once in a while, she pokes me and says, “Oh wow, look at that.”
So, other than everything being different, things are the same. I have my laptop, four virtual machines fired up, and I am playing with the SMBShare module from the Windows PowerShell DSC Resource Kit.
Note Today is the fifth day in a series of blog posts about Desired State Configuration.
Today I am talking about creating SMB shares.
The first thing to do to use Windows PowerShell Desired State Configuration (DSC) to manage SMB shares is to go the Scripting Guys Script Repository and download the xSmbShare PowerShell Module, which is part of the DSC Resource Kit. This module comes in a ZIP file. I had to extract the file. Inside the compressed file, is a folder named xSmbShare. In the sSmbShare folder are a .psd1, a .psm1, and a .mof file.
All I really need to do is to copy the xSmbShare folder to my "C:\Program Files\WindowsPowerShell\Modules" folder. I need to do this on the computer from which I will run the configuration script and on the target computer. Here is what the folder looks like on my computer running Windows 8.1:
When I have the resource module on the appropriate computers and in the appropriate locations, I can write a configuration script to create a share. I begin with the Configuration keyword, and I assign a name to the configuration. I call it ScriptingShareConfig because it will create a share called ScriptingShare1. I then open my script block, and I must import the DSC resource. I got the name of the DSC resource from the schema MOF file in the XSmbShare folder. Following this, I specify the node that will be the target.
So to this point, there are the following steps:
The configuration script looks like this:
Import-DscResource -Name MSFT_xSmbShare
Now I need to specify the resource. In this example, it is xSmbShare. I can then highlight the name of the resource, and start IntelliSense by right-clicking and choosing Start IntelliSense, or by pressing CTRL plus the Spacebar. Whichever method I use, I am treated with the following pop-up menu that details what I need to know about the xSmbShare resource:
I want to ensure that the share is present, that it is named “ScriptingShare1”, that the path is c:\fso, and that it has a description. Following is the script that does this:
Ensure = "present"
Name = "ScriptingShare1"
Path = "C:\fso"
Description = "This is a scripting SMB Share"
Now, I simplify things by creating the configuration MOF file that I will use with the Start-DSCConfiguration cmdlet. In fact, I add both tasks to the script—creating the MOF and then starting the configuration. These two commands are shown here:
ScriptingShareConfig -outputpath c:\smbshare
Start-DscConfiguration -Path C:\Smbshare -wait -verbose
I like using the Verbose switch with my configuration because it lets me know what is going on, how long it will take, and what I can expect. It also provides useful error messages if needed.
So to the above three steps, I add four more:
4. Call the DSC resource and provide a name 5. Assign values for the resource properties 6. Call the configuration by name 7. Start the DSC configuration
Each of these seven steps is pretty generic when it comes to working with DSC configurations. They are indicated in the configuration script that is shown here:
That is all there is to using the xSmbShare PowerShell module from the Windows PowerShell DSC Resource Kit. DSC Week will continue tomorrow when I will talk about first creating the folder, populating it with scripts, and then sharing the folder. I will also do this on multiple computers.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy