Ezine 162 - Creating Shares with PowerShellEzine 162 - Creating Shares with PowerShellThe 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 SecretScripts 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. Mission to Create a Network ShareOur 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 SharesThis script is a useful reminder to see how WMI lists shares. # Microsoft PowerShell script to list shared
folders 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 DefinitionsIf you are interested in researching PowerShell's commands, then please study this example, else go to example 2. # PowerShell script to Research Win32_Share $objWMI = [wmiClass] 'Win32_share' 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 ShareWe 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 $FolderPath = "C:\Temp" 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: Alternatively, you could substitute line 8 with this more traditional
technique If you are looking for handy network utilities, try some of the free downloads at Tools4Ever Example 3: Deleting Shares with PowerShellTo 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 $ShareName = "Temporary" 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 PowerShellListing 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.
*
|
||||||