PowerCORE – Server 2008R2 and Powershell Unleashed Part 4

Powershell

So we now have a Server 2008R2 core box, Powershell enabled that can easily Add/Remote Roles and Features.  Let’s make this a useful box.

One of the simpler roles you’ll typically run is the File Server so you can share files.   Using the Powershell module “ServerManager” makes adding or removing these changes very easy.  

Just execute in Powershell

IMPORT-MODULE ServerManager
ADD-WINDOWSFEATURE “File-Services”

It will require one reboot after it’s done to allow it to take foot properly

Once done you now have a potential file server.   I say “Potential” since you still have to create Shares, Files and Permissions.

Well generally once you have a Folder you can create a Share Correct?

So we’re going to place a Folder at the root of the C: Drive called “DATA”

NEW-ITEM –path C:DATA –type directory –force

Now to create a Network share in Powershell we’re going to leverage the built in WMI libraries in Windows to create them.  WMI (Windows Management Instrumentation) allows you to not only View but manipulate many features of Windows.  

[wmiclass]”win32-share”

Is the technique you can use in Powershell to access the Keying in a

[wmiclass]”win32-share” | get-member

Will show us all the available members of this object.  There is one in particular called “Create”. 

Definition : System.Management.ManagementBaseObject Create(System.String Path, System.String Name, System.UInt32 Type, System.UInt32 MaximumAllowed, System.String Description, System.String Password, System.Management.ManagementObject#Win32-SecurityDescriptor Access)

For us non Programmers it breaks down to this.  The Create Method will take up 5 particular parameters in this order.

PATH, NAME,PermissionFlags, MaximumAllowedConnections, DescriptionOfShare,SharePassword,

Creating a share is just a matter of passing some basic information to this class.  Namely the Name of the Share, the physical location and the maximum allowed users.

So if I have a potential folder called “C:Powershell” and I’d like to share it out to my network as “POWERSHELL” as a fileshare I just fire up this command.

([wmiclass]”win32-share”).create(“C:Powershell”,”Powershell”,0)

Why the “0”? You don’t just have file shares.  You also have Printer shares and IPC as well.   The same command is used to create and mange them all

But just as easily, we can make this into a function to add to out Powershell profile.   Just substitute in what we need to change on a regular basis.

function global:NEW-SHARE($Location,$Name) {
    ([wmiclass]”win32-share”).Create($Location, $Name)

}

This is of course a very simple function but one we can build on later.    Like the ability to set share security and the maximum number of allowed users.  As well we should check the status of whether it was created successfully.

But that’s where Powershell is just fantastic.  We can build the tool to MEET our needs in the short term and later on add on to it.

Next time we’ll dig into some more features of Server 2008R2 Core and how we can use Powershell to manage them.  

The Power of Shell is in YOU

Sean
The Energized Tech

Leave a Reply