Hey, Scripting Guy! How can I share a folder on a remote computer?
— RS
Hey, RS. One of the great things about WMI is the fact that – with one or two fairly obscure exceptions – anything you can do on the local computer you can also do on a remote computer. This is the big advantage scripting has over command line tools; a number of command line tools (including tools like net share) work only on the local computer. If you want to do something remotely, a WMI script is often the only way to go.
So how do you share a folder on a remote computer? Here’s one way:
Const FILE_SHARE = 0 Const MAXIMUM_CONNECTIONS = 25strComputer = “atl-ws-01” Set objWMIService = GetObject _ (“winmgmts:\\” & strComputer & “\root\cimv2”)
Set objNewShare = objWMIService.Get(“Win32_Share”)
errReturn = objNewShare.Create _ (“C:\Public”, “PublicShare”, FILE_SHARE, _ MAXIMUM_CONNECTIONS, “Public share for Fabrikam employees.”)
The preceding script shares out the folder C:\Public on a computer name atl-ws-01. The script starts by setting a pair of constants. First we assign the value 0 to the constant FILE_SHARE (which indicates the type of shared resource we are creating); if we set the value of this constant to 2147483648 we could create an Administrative share rather than a standard file share. We also assign the value 25 to the constant MAXIMUM_CONNECTIONS, which sets the maximum number of simultaneous connections. If we wanted to allow an unlimited number of simultaneous connections we wouldn’t use this constant at all, and then leave this parameter blank when we create the share.
After connecting to the WMI service on the remote computer we then connect to the Win32_Share class. At that point, all we have to do is call the Create method, passing the following five parameters:
Parameter |
Description |
“C:\Public” |
The local path to the folder being shared. |
“PublicShare” |
Share name for the shared folder. |
FILE_SHARE |
The type of share being created. |
MAXIMUM_CONNECTIONS |
The maximum number of simultaneous users that can connect to the share. |
“Public share for Fabrikam employees.” |
Optional description of the shared folder. |
That’s about all there is to it; run the script, and the folder C:\Public will be shared out as PublicShare. Note that the folder C:\Public must already exist on the computer atl-ws-01; the Win32_Share Create method won’t create the folder for you. If you aren’t sure if the folder C:\Public exists, here’s a quick way to check that:
strComputer = “atl-ws-01” Set objWMIService = GetObject _ (“winmgmts:\\” & strComputer & “\root\cimv2”)Set colFolders = objWMIService.ExecQuery _ (“Select * From Win32_Directory Where Name = ‘C:\\Public'”) Wscript.Echo colFolders.Count
This script echoes back the number of folders named C:\Public (note that in the query itself this must be specified as C:\\Public) found on the computer atl-ws-01. If the number of folders equals 0, then C:\Public doesn’t exist. If the number of folders equals 1, then it does exist.
By the way, we know a lot of you are going to ask about setting permissions on a shared folder. This can actually be done in a script, but the process is a bit complicated and requires more detailed discussion than we can provide in this column. But we hope to post a considerable amount of material explaining how to work with security descriptors very soon.
0 comments