1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 47 pps

10 223 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 145,2 KB

Nội dung

440 ‘use the objWshNetwork object’s MapNetworkDrive() method to map to drive objWshNet.MapNetworkDrive DriveLetter, NetworkPath End Sub Disconnecting Mapped Drives You can use the WshNetwork objects’ RemoveNetworkDrive() method to disconnect a mapped drive when it’s no longer needed. For example, you might want to do this at the end of the script that created the drive mapping, after it has completed its assigned task. The syntax of the RemoveNetworkDrive() method is as follows: WshNetwork.RemoveNetworkDrive letter, [kill], [persistent] Letter is the drive that has been assigned to the mapped drive. Kill is an optional setting with a value of either True or False. Setting it to True disconnects a mapped drive even if it is currently in use. Persistent is also optional. Set it to True to disconnect a permanently mapped drive. The following VBScript demonstrates how to disconnect the network drive that was mapped by the previous script: ‘************************************************************************* ‘Script Name: DriveBuster.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script demonstrates how to add logic to VBScripts in ‘order to terminate a network drive mapping. ‘************************************************************************* ‘Initialization Section Option Explicit On Error Resume Next Dim objWshNet ‘Instantiate the objWshNetwork object Set objWshNet = WScript.CreateObject(“WScript.Network”) Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘Main Processing Section ‘Call procedure that deletes network drive connections, passing it the ‘the drive letter to be removed MapNetworkDrive “z:” WScript.Quit() ‘Terminate script execution ‘Procedure Section ‘This subroutine disconnects the specified network drive connection Sub MapNetworkDrive(DriveLetter) ‘Use the objWshNetwork object’s RemoteNetworkDrive() method to disconnect ‘the specified network drive objWshNet.RemoveNetworkDrive DriveLetter End Sub Printer Administration Printer administration involves many tasks. One task is setting up network printer connections. Other tasks include managing print jobs and physically managing the printer, including refilling its paper, ink, ribbon, or toner supply. Another task includes removing printer connections when they are no longer needed. The next two sections demonstrate how to use VBScript and the WSH to set up and disconnect network printer connections. Connecting to a Network Printer To create a connection to a network printer, you need to use the WshNetwork object’s AddWindows- PrinterConnection() method. This method has two different types of syntax, depending on the operating system on which the script is executed. The syntax for the AddWindowsPrinterConnection() method when used on a computer run- ning Windows NT, 2000, or XP is WshNetwork.AddWindowsPrinterConnection(strPrinterPath) 441 Appendix A • WSH Administrative Scripting 442 The syntax for the AddWindowsPrinterConnection() method when used on a computer running Windows 95, 98, or Me is as follows: WshNetwork.AddWindowsPrinterConnection(strPrinterPath, strDriverName [, strPort]) StrPrinterPath is the UNC path and name for the network printer. StrDriverName is the name of the appropriate printer software driver, and StrPort is an optional port assignment for the printer connection. The following VBScript demonstrates how to set up a network printer connection: ‘************************************************************************* ‘Script Name: PrinterMapper.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script demonstrates how to use a VBScript to set up a ‘connection to a network printer. ‘************************************************************************* ‘Initialization Section Option Explicit Dim objWshNet ‘Instantiate the objWshNetwork object Set objWshNet = WScript.CreateObject(“WScript.Network”) ‘Main Processing Section ‘Call the procedure that creates network printer connections passing ‘it a port number and the UNC pathname of the network printer SetupNetworkPrinterConnection “\\ICS_Server\CanonPrinter” WScript.Quit() ‘Terminate script execution Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘Procedure Section Sub SetupNetworkPrinterConnection(NetworkPath) ‘Use the objWshNetwork object’s AddWindowsPrinterConnection() method ‘to connect to the network printer objWshNet.AddWindowsPrinterConnection NetworkPath End Sub Disconnecting from a Network Printer As with network drives, removing a printer connection is a little easier to do than connect- ing it initially. Printer connections need to be removed for a number of reasons. For example, every printer eventually breaks and must be replaced. Sometimes people move from one loca- tion to another, necessitating changes to printer connections. By scripting the setup and removal of printer connections, you can automate this process. To remove a network printer connection, you need to use the WshNetwork object’s RemovePrinterConnection() method: WshNetwork.RemovePrinterConnection resource, [kill], [persistent] Resource identifies the printer connection and may be either the connection’s assigned port number or its UNC name and path. Kill is an optional setting with a value of either True or False. Setting it to True disconnects a printer connection even if it is currently in use. Persistent is also optional. Set it to True to disconnect a permanent printer connection. The following VBScript demonstrates how to remove the printer connection established by the previous VBScript: ‘************************************************************************* ‘Script Name: PrinterBuster.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script demonstrates how to use a VBScript to disconnect ‘a network printer connection. ‘************************************************************************* ‘Initialization Section 443 Appendix A • WSH Administrative Scripting 444 Option Explicit Dim objWshNet ‘Instantiate the objWshNetwork object Set objWshNet = WScript.CreateObject(“WScript.Network”) ‘Main Processing Section ‘Call the procedures that disconnect network printer connections passing ‘it the UNC pathname of the network printer SetupNetworkPrinterConnection “\\ICS_Server\CanonPrinter” ‘Terminate script execution WScript.Quit() ‘Procedure Section Sub SetupNetworkPrinterConnection(NetworkPath) ‘Use the objWshNetwork object’s RemovePrinterConnection() method to ‘disconnect from a network printer objWshNet.RemovePrinterConnection NetworkPath, “True”, “True” End Sub Computer Administration The term computer administration represents a very broad category of tasks. Rather than try to list or explain them all, I’ll simply present you with two computer administration examples. The first example demonstrates how to use VBScript and the WSH to manage Windows services, and the second example demonstrates automated user account creation. Managing Services On computers running Windows NT, 2000, and XP operating systems, much of the operat- ing system’s core functionality is provided in the form of services. These services perform Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition tasks, such as managing Windows plug and play, handling the spooling of printer jobs, and administering the execution of scheduled tasks. By starting and stopping Windows services, you can enable and disable specific Windows functions (that is, control just what users can and cannot do). You can use the Windows NET STOP and NET START commands to stop and start Windows ser- vices. To execute these commands from within a VBScript, you can use the WshShell object’s Run() method, as demonstrated in the following script: ‘************************************************************************* ‘Script Name: ServiceCycler.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script demonstrates how to use VBScript to stop and ‘start Windows services. ‘************************************************************************* ‘Initialization Section Option Explicit On Error Resume Next Dim objWshShl, strServiceToManage ‘Instantiate the WshShell object Set objWshShl = WScript.CreateObject(“WScript.Shell”) ‘Main Processing Section ‘Prompt the user to specify the name of the service to cycle strServiceToManage = InputBox(“What service would you like to cycle?”) ‘Call the procedure that stops a service StopService(strServiceToManage) 445 Appendix A • WSH Administrative Scripting 446 ‘Pause for 5 seconds WScript.Sleep(15000) ‘Call the procedure that starts a service StartService(strServiceToManage) ‘Terminate script execution WScript.Quit() ‘Procedure Section ‘This subroutine stops a specified service Function StopService(ServiceName) objWshShl.Run “net stop “ & ServiceName, 0, “True” End Function ‘This subroutine starts a specified service Function StartService(ServiceName) objWshShl.Run “net start “ & ServiceName, 0, “True” End Function User Account Administration User administration involves many tasks, including creating, modifying, and removing user accounts from the computer or the Windows domain to which the computer is a member. To perform user account administration, you need to have administrative privileges within the context that the script will execute (that is, on the computer or at the domain level). One way to create a new user account is with the Windows NET USER command. You also can use the NET GROUP command to add a newly created user account into a global domain group account or the NET LOCALGROUP command to add the user account to a local group. TRICK Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition For example, the following VBScript uses the WshShell object’s Run() method and the NET USER command to create a new user account on the Windows NT, 2000, or XP computer on which the script is executed. ‘************************************************************************* ‘Script Name: AccountCreator.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script demonstrates how to use VBScript to create new ‘user accounts. ‘************************************************************************* ‘Initialization Section Option Explicit On Error Resume Next Dim objFsoObject, objWshShl, strNewAccts, strAcctName ‘Instantiate the FileSystemObject object Set objFsoObject = CreateObject(“Scripting.FileSystemObject”) ‘Instantiate the WshShell object Set objWshShl = WScript.CreateObject(“WScript.Shell”) ‘Specify the location of the file containing the new user account name Set strNewAccts = _ objFsoObject.OpenTextFile(“c:\Temp\UserNames.txt”, 1, “True”) ‘Main Processing Section CreateNewAccts() ‘Call the procedure that creates new user accounts WScript.Quit() ‘Terminate script execution 447 Appendix A • WSH Administrative Scripting 448 ‘Procedure Section Sub CreateNewAccts() ‘This procedures creates new accounts ‘Create a Do While loop to process each line in the input file Do while False = strNewAccts.AtEndOfStream ‘Each line of the file specifies a unique username strAcctName = strNewAccts.ReadLine() ‘Create the new account objWshShl.Run “net user “ & strAcctName & “ “ & strAcctName & _ “ /add”, 0 Loop ‘Close the input file strNewAccts.Close End Sub To make the script more flexible, it has been set up to use VBScript FileSystemObject methods, which allow it to open and retrieve a list of names from an external file called UserNames.txt, located in the C:\Temp folder. This way, the script can be used over and over again without any modification. All you need to do is modify the script’s input text file. Scheduling the Execution of Administrative Scripts Windows operating systems provide two means of scheduling script execution. The first option is the AT command, which is executed from the Windows command prompt. The sec- ond option is to use the Scheduled Task Wizard, which provides a step-by-step walk-through of the scheduling process. In addition, you can also use either of these two options to auto- mate the execution of a single master scheduling script, from which you can programmati- cally control the execution of other VBScripts. The AT Command The Windows AT command allows you to set up and manage scheduled tasks from the Windows command prompt, which you can access from within your VBScripts using the WshShell object’s Run() method. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition There are two different types of syntax for the AT command. The syntax used to delete sched- uled tasks: at [\\computername] [[id] [/delete] | /delete [/yes]] To view a list of currently scheduled tasks, type the AT command at the Windows command prompt and press the Enter key. To set the computer at which a new scheduled task is to be executed, specify the computer’s name in place of \\computername. Id identifies a numbered task ID assigned to every task by the scheduler service. /Delete performs a task deletion. /Yes is used to supply a confirmation for a delete operation. The syntax used to create a new scheduled task is at [\\computer] time [/interactive] [/every:date[, ] | /next:date[, ]] command \\Computer specifies the computer where the task is to be scheduled. Time specifies the time that task is to be executed in the format of hh:mm on a 24-hour clock. Specify /interactive to allow the script to interact with the desktop and the logged on user. / Every:date[, ] spec- ifies the task’s schedule using specified days of the week or month. For example, dates are specified as M, T, W, Th, F, S, Su, and days of the month are specified as 1 - 31. /Next: date[, ] sets the tasks to run on the next occurrence of a specified day or date. Finally, command specifies the name of the application or script to be scheduled. The following VBScript demonstrates how to use the Windows AT command to set up sched- uled tasks for three VBScripts: ‘************************************************************************* ‘Script Name: AtScheduler.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script demonstrates how to use the Windows AT command ‘within a VBScript to schedule the execution of other scripts. ‘************************************************************************* ‘Initialization Section Option Explicit TRICK 449 Appendix A • WSH Administrative Scripting . VBScripts using the WshShell object’s Run() method. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition There are two different types of syntax for the AT command systems, much of the operat- ing system’s core functionality is provided in the form of services. These services perform Microsoft WSH and VBScript Programming for the Absolute Beginner, Second. Programming for the Absolute Beginner, Second Edition For example, the following VBScript uses the WshShell object’s Run() method and the NET USER command to create a new user account on the Windows

Ngày đăng: 03/07/2014, 18:20

TỪ KHÓA LIÊN QUAN