Guy always like to get the job done. In this case the job is
persuading PowerShell to create a network share on an existing folder
using the Win32_Share .create method.
I am going to divide this project into three stages. Firstly, enumerate the shares,
secondly, research methods for the WMI class Win32_Share. Finally, create a new
share with PowerShell.
I have created this script with just one line and two commands.
# Microsoft PowerShell script to list shared
folders # Author: Guy Thomas # Version 1.5 February 2010 tested
on PowerShell v 2.0
Get-WmiObject -class Win32_Share | sort type, name
Note 1: You could use the alias gwmi instead of
Get-WmiObject.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
What this script reveals is that the Win32_Share has a method called
create. Furthermore, if you drill down .create has the following detailed definition: Path, Name,
Type, MaximumAllowed (connections), Description (comment), Password and
Access.
To help make sense of this information try a walk-through whereby you create a
network share manually 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 Win32_Share needs a 'Type',
just trust me that for our purposes its value is Zero. Else go ahead, create a Share then run this PowerShell
share script.
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 and
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 create share script # Author: Guy Thomas
## Version 1.5 February 2010 tested on PowerShell v 2.0
Note 1: For 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 wish I knew more about the line:
$objWMI = [wmiClass] 'Win32_share'. All I can say is that if we generate
this object, apply the .create() method, then the script creates the network
share as planned. Alternatively, you could substitute:
$objWMI = Get-WMIObject Win32_share.
»
Summary of PowerShell Create Share
By introducing the .create method, we produce a more advanced Microsoft
PowerShell script, than simply listing existing shares. As you tackle this
script take the time to study the parameters. Perhaps a manual
walk-through of creating a share will help your understanding of Win32_Share.
If you like this page then please share it with your friends
Please email me if you have a example scripts. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.