Windows Script Host  

WSH Network Administrator Sample Script

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:

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

  1. Establish the necessary security settings on the remote machines.
  2. Copy the AdminScript.vbs script below into your scripting text editor.
  3. Change the constants to reflect your network paths and machine names.
  4. Replace remmachine with the applicable remote machine name and run the script:
    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

' 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

WSH Walkthrough | Accessing Networks | Setting up Remote WSH | Running Scripts Remotely