Compartir a través de


WSH Walkthrough

The following walkthrough describes how a typical Network Administrator or other IT professional might use WSH 5.6 to create procedures that accomplish useful tasks.

Note

The walkthrough is presented in VBScript. The process for creating these scripts is nearly the same for developers using VBScript or JScript.

During the course of this walkthrough, you will perform the following activities:

  • Create a script that creates a common share on several remote machines and populate it with files.

  • Create a script that creates a common Printing Device connection on several remote machines and establish it as the default printing device.

To complete the walkthrough, all remote machines must be properly configured to enable Remote WSH. For more information on enabling these security settings, see Setting up Remote WSH.

Note

The following code is from the sample included in this documentation. To view the entire sample, see WSH Network Administrator Sample Script.

Create Variables and Constants

To create the necessary variables and constants

  1. In your text-scripting editor, enter the variables.`

    Dim FSO
    Dim Services
    Dim SecDescClass
    Dim SecDesc
    Dim Trustee
    Dim ACE
    Dim Share
    Dim InParam
    Dim Network
    
  2. In your text-scripting editor, enter the constants, changing the values to reflect the UNC names and paths applicable to your network environment.

    Const FolderName = "C:\Public"
    Const AdminServer = "\\AdminMachine"
    Const ShareName = "Pubs"
    Const PrinterShare = "\\CorpPrinters\PrinterShare"
    

Connecting to a printer and setting it as default

To connect the machine to a common printing device

  • In your text-scripting editor, enter the code that creates a printing device. This code uses the Network variable and PrinterShare constant initialized in the previous step.

    Set Network = CreateObject("Wscript.Network")
    Network.AddWindowsPrinterConnection PrinterShare
    

To set the machines default printing device

  • In your text-scripting editor, enter the code that sets the default printing device. This code uses the Network variable and PrinterShare constant initialized in the first step.

    Network.SetDefaultPrinter PrinterShare
    

Creating a common share, copying files to it, and sharing it

To create a common share on the machine

  • In your text-scripting editor, enter the code that creates a File System Object (FSO) and creates a folder. The script verifies the existence of the folder. If the folder does not exist, the script creates it. This code uses the FSO variable and the FolderName constant initialized in the first step.

    Set FSO = CreateObject("Scripting.FileSystemObject")
    If Not FSO.FolderExists(FolderName) Then 
       FSO.CreateFolder(FolderName)
    End If
    

To copy files to the newly created folder

  • In your text-scripting editor, enter the code that creates a File System Object (FSO) and copies files from your local machine to the remote machine. This code uses the FSO variable and the FolderName constant initialized in the first step.

    Call FSO.CopyFile(AdminServer & "\Public\Images\*.*", FolderName)
    

To establish the newly created folder as a share with WMI

  • In your text-scripting editor, enter the code that creates a share using Windows Management Instrumentation (WMI). The share is established on the folder generated above. The script first connects to WMI. Next, it sets the security impersonation level and the Windows NT privilege that lets you set Discretionary Access Control Lists (DACLs) and Security Access Control Lists (SACLs). Next, it creates a new security descriptor and sets up a couple of Access Control Entries (ACEs) for the new share. Finally, it creates a new share with the new security descriptor. This code uses the Services, SecDescClass, SecDesc, Trustee, ACE, Share, and InParam variables, and the FolderName, AdminShare, and ShareName constants initialized in the first step.

    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, search for WMI overview on MSDN Online.

    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)
    

Next Steps

The sample included in this documentation contains a complete, executable script with all of the functionality above. See WSH Network Administrator Sample Script.

Before running the script, ensure that all remote machines have been properly configured to run remote scripts. This is accomplished with Poledit.exe on the server. For more information, see Setting up Remote WSH.

When running remote WSH, the script is copied to the remote machines. Once the remote machine's security settings have been verified and the script is successfully copied, a return indicates success or failure. If successful, the script is then executed on the remote machines. For more information on running a remote WSH script, see Running Scripts Remotely.

See Also

Concepts

Setting up Remote WSH

Running Scripts Remotely

Other Resources

Accessing Networks