This WSH network sample demonstrates how a typical network administrator may use a script on several remote machines on the network. The sample script performs useful administrative tasks including:
-
Connecting the machines to a network printing device
-
Setting the newly connected printing device as the default printer
-
Creating a common public folder on the machines
-
Copying files to the newly created folder
-
Establishing the newly created folder as a share using Windows Management Instrumentation (WMI).
Note WMI is a powerful, sophisticated technology based on Web Based Enterprise Management (WBEM). WMI is primarily used for accessing and instrumenting management information in an enterprise environment. For more information on WMI, see the WMI SDK at (http://msdn.microsoft.com).
The Administrator must establish the necessary security settings on the remote machines. For more information, see Setting up Remote WSH. Next the administrator must copy and paste the sample into the scripting editor and change the constants to reflect the corresponding network paths and machine names. Finally the administrator can run the script.
To run this sample
-
Establish the necessary security settings on the remote machines.
-
Copy the AdminScript.vbs script below into your scripting text editor.
-
Change the constants to reflect your network paths and machine names.
-
Replace remmachine with the applicable remote machine name and run the script:
Copy Code var oController = new ActiveXObject"WSHController" var oProcess = oController.CreateScript "c:\MyLocalDir\\AdminScript.vbs", "remmachine" oProcess.Execute() while (oProcess.Status != 2) WScript.Sleep(100) WScript.Echo"Done"
AdminScript.vbs Sample
Copy Code | |
---|---|
' Remote WSH Admin Sample AdminScript.vbs ' ' This sample code does a few common administrative tasks which a ' network administrator might want to do to a number of the machines ' on his or her network: it creates a public directory, populates ' it with some files and shares the directory out. It also sets ' up the machines default printer connection. ' Note that in the interests of keeping this example code small, error ' handling has been omitted. Actual production code should use ' appropriate error handling as many of these operations could fail; ' the disks could run out of space, for instance. Option Explicit Dim FSO Dim Services Dim SecDescClass Dim SecDesc Dim Trustee Dim ACE Dim Share Dim InParam Dim Network Const FolderName = "C:\Public" Const AdminServer = "\\AdminMachine" Const ShareName = "Pubs" Const PrinterShare = "\\CorpPrinters\PrinterShare" ' First we add a printer to this machine and make it the default. Set Network = CreateObject("Wscript.Network") Network.AddWindowsPrinterConnection PrinterShare Network.SetDefaultPrinter PrinterShare ' Next we create a folder and populate it with some files. Set FSO = CreateObject("Scripting.FileSystemObject") If Not FSO.FolderExists(FolderName) Then FSO.CreateFolder(FolderName) End If Call FSO.CopyFile(AdminServer & "\Public\Images\*.*", FolderName) ' Make the folder into a share using WMI ' See the WMI SDK for information on how this code works. Set Services = GetObject("WINMGMTS:{impersonationLevel=impersonate,(Security)}!" & AdminServer & "\ROOT\CIMV2") Set SecDescClass = Services.Get("Win32_SecurityDescriptor") Set SecDesc = SecDescClass.SpawnInstance_() Set Trustee = Services.Get("Win32_Trustee").SpawnInstance_ Trustee.Domain = Null Trustee.Name = "EVERYONE" Trustee.Properties_.Item("SID") = Array(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0) Set ACE = Services.Get("Win32_Ace").SpawnInstance_ ACE.Properties_.Item("AccessMask") = 2032127 ACE.Properties_.Item("AceFlags") = 3 ACE.Properties_.Item("AceType") = 0 ACE.Properties_.Item("Trustee") = Trustee SecDesc.Properties_.Item("DACL") = Array(ACE) Set Share = Services.Get("Win32_Share") Set InParam = Share.Methods_("Create").InParameters.SpawnInstance_() InParam.Properties_.Item("Access") = SecDesc InParam.Properties_.Item("Description") = "Public Share" InParam.Properties_.Item("Name") = ShareName InParam.Properties_.Item("Path") = FolderName InParam.Properties_.Item("Type") = 0 Share.ExecMethod_("Create", InParam) ' And we're done. |
See Also
Tasks
WSH WalkthroughConcepts
Setting up Remote WSHRunning Scripts Remotely