PowerShell Ezine, Logon Scripts

Ezine 162 - Creating Shares with PowerShell

Ezine 162 - Creating Shares with PowerShell

The keyword in this ezine is 'create'.  In the last issue we listed shares, and thus got experience of WMI in general, and Win32_Share in particular.  So now we are ready to turn our focus on the .create method.

Topics for Creating New Shares with PowerShell

This week's Secret

Scripts that create objects on YOUR computer make me feel twitchy.  Guy's subscriber from hell is going to get the wrong end of the stick and create something completely inappropriate.  To let you into another secret, if there is one thing that makes me more nervous than explaining how a script creates, it's explaining a how a script deletes.

For this reason, this week's scripts are slightly more cryptic than usual.  There are no instructions for installing PowerShell, or explanation of how to copy and paste the code.  My thinking is that if readers cannot understand how to perform these basic steps, then in all honesty, they should not be given a script that could start deleting objects on their computer.PowerShell Shares Create Script

Mission to Create a Network Share

Our mission is to create a network share from an existing folder.  While we are going to use PowerShell, I am a great believer in having a manual walk-through with Windows Explorer.  The advantage is that you can see the fields, and get a feel for which items are essential for your script.

I am going to divide this network share project into two stages.  In the first stage we are going to research methods to use with the WMI class Win32_Share.  Then in the second stage we will use this knowledge to create a new share with PowerShell.

So, 'let's roll, and let's be careful out there', as Sargeant Esterhaus of Hill Street Blues would say.

Preamble, List Existing Shares

This script is a useful reminder to see how WMI lists shares.

# Microsoft PowerShell script to list shared folders
# Author: Guy Thomas
# Version 1.5 March 2008 tested on PowerShell v 1.0

get-WmiObject -class Win32_Share | sort type, name

Note 1:  You could use the alias gwmi instead of get-WmiObject.

Example 1: Research Win32_Share's Methods and Definitions

If you are interested in researching PowerShell's commands, then please study this example, else go to example 2.

# PowerShell script to Research Win32_Share
# Author: Guy Thomas
# Version 1.2 April 2008 tested on PowerShell v 1.0

$objWMI = [wmiClass] 'Win32_share'
$objWMI |gm -memberType method |format-List

What this script reveals is that Win32_Share has a method called: 'Create'.  Furthermore, if you drill down .create has the following six-part definition: Path, Name, Type, MaximumAllowed (connections), Description (comment), Password and Access.

To help make sense of this information, try a manual walk-through whereby you create a network share in Windows Explorer; click on the Tools Menu, then Map Network Drive.  If you try this technique, then you will see why PowerShell requires a Name and Path.  What is less clear is why Win32_Share needs a 'Type', just trust me that for our purposes its value is zero.

Example 2: Create a Network Share

We have a slight problem.  I cannot see your c:\ drive.  The example below creates a network share from the temp folder.  However, it would be better, and it would aid your understanding, if you created a folder on your c:\ drive.  Once you have a suitable folder then amended the value for $FolderPath in my script below.  If you are up for another challenge, then edit the value of $ShareName in my script.

# Microsoft PowerShell script to create a network share
# Author: Guy Thomas
## Version 1.6 April 2008 tested on PowerShell v 1.0

$FolderPath = "C:\Temp"
$ShareName = "ChangeMe"
$Type = 0
$objWMI = [wmiClass] 'Win32_share'
$objWMI.create($FolderPath, $ShareName, $Type)

Note 1:  For the sake of simplicity we are only scripting three parameters for Win32_Share, Path, Name and Type.  The value for each parameter is held by a corresponding variable, thus each is easy to change.

Note 2: I am experimenting with new technique for get-WMIObject:
$objWMI = [wmiClass] 'Win32_share'. 

Alternatively, you could substitute line 8 with this more traditional technique
$objWMI = get-WMIObject Win32_share.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


Example 3: Deleting Shares with PowerShell

To be frank, the most useful job for this script is to delete, the network share so that you can run Example 2 again.

The idea with this script is to list the shares on your machine, then filter that list for the just the share name you wish to delete.  The last line of Example 3 then deletes that one share.

# Microsoft PowerShell script to delete a network share
# Author: Guy Thomas
# Version 2.2 April 2008 tested on PowerShell v 1.0

$ShareName = "Temporary"
$ShareDel = get-WmiObject Win32_Share -filter "Name ='$ShareName' " $ShareDel.delete()

Note 1:  PowerShell is consistent in that it always uses a method called .delete.  (PowerShell has no .remove, or .erase)  My point is that once you learn .delete here, you can be on the lookout for the same method with other objects, for example file objects.

ˆ

Summary of Creating Shares with PowerShell

Listing computer objects, such as shares, is all well and good, but sooner or later you need a script to create new objects.  My only concern is that you will get the wrong end of the stick, and create something that conflicts with existing objects on your computer.

When the time is ripe and your knowledge is sufficient, then research which features your .create method needs to do its job.  In this instance, you need a name for the share, and of course a path or file location, take it on faith that the type is zero.

So, as Sargeant Esterhaus of Hill Street Blues would say: 'let's roll, and let's be careful out there'.

See more Windows PowerShell real life tasks

PowerShell Home  • Real life tasks  • IpConfig  • Exchange  • Services  • Syntax

Com objects  • PowerShell Registry

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.