sams teach Yourself windows Script Host in 21 Days phần 6 ppsx

51 278 0
sams teach Yourself windows Script Host in 21 Days phần 6 ppsx

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Dim filesys Set filesys = Wscript.CreateObject("Scripting.FileSystemObject") ’ ’ Iterate through the arguments & create folders ’ For Each sFolderPath In Wscript.Arguments filesys.CreateFolder sFolderPath If Err.Number = 0 Then Wscript.Echo "makfldr.vbs: The folder ’" & _ sFolderPath & "’ was created successfully." Else Wscript.Echo "" Wscript.Echo "makfldr.vbs: An error occurred while " & _ "creating the folder ’" & sFolderPath & "’" Wscript.Echo "makfldr.vbs: (" & Err.Number & ") " & Err.Description Wscript.Echo "" End If Next This script differs from the previous script because it takes multiple command-line arguments. A For Each loop iterates through the arguments and creates directories accordingly. Let’s take a quick look at the effects of running the script. The before picture is illustrated in Figure 11.12. Figure 11.12: Directories before running the makfldr.vbs script. If you run the script using the command line makfldr H:\ROOT H:\ROOT\BIN H:\ROOT\SRC H:\ROOT\HELP H:\ROOT\HELP\ADMIN, you’ll see the output illustrated in Figure 11.13, and if you go back to the Windows Explorer, you’ll see something that looks like Figure 11.14. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 11.13: Output of the makfldr.vbs script. Figure 11.14: Directories after running the makfldr.vbs script. Deleting a Folder Deleting a folder is just as simple as the preceding examples. Listing 11.15 uses the FileSystemObject.DeleteFolder() method instead of FolderExists() or CreateFolder(). Listing 11.15 delfldr.vbs—Code to Delete Folder(s) ’ FILE: delfldr.vbs ’ DESC: This script demonstrates how you can use VBScript ’ to delete a folder. ’ AUTH: Thomas L. Fredell ’ DATE: 12/9/1998 ’ ’ Copyright 1998 Macmillan Publishing Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com On Error Resume Next ’ ’ Check arguments & show usage if necessary ’ If Wscript.Arguments.Count < 1 Then Wscript.Echo "USAGE: delfldr.vbs [folder1] [folderN]" Wscript.Echo "" Wscript.Echo " This allows you to delete one or more" Wscript.Echo " folders." Wscript.Echo "" Wscript.Quit 1 End If ’ ’ Create a FileSystemObject; we’ll use it to delete the folder(s) ’ Dim sFolderPath Dim filesys Set filesys = Wscript.CreateObject("Scripting.FileSystemObject") ’ ’ Iterate through the arguments & delete folders ’ For Each sFolderPath In Wscript.Arguments filesys.DeleteFolder sFolderPath If Err.Number = 0 Then Wscript.Echo "delfldr.vbs: The folder ’" & _ sFolderPath & "’ was deleted successfully." Else Wscript.Echo "" Wscript.Echo "delfldr.vbs: An error occurred while " & _ "deleting the folder ’" & sFolderPath & "’" Wscript.Echo "delfldr.vbs: (" & Err.Number & ") " & Err.Description Wscript.Echo "" End If Next The flow of delfldr.vbs is identical to the makfldr.vbs script. The only differences are the messages and the call to DeleteFolder(). Summary In this chapter, you’ve seen a number of examples that illustrate how you can use the Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com WSH objects to perform standard administrative tasks. You’ve looked at examples that show how you can perform the following functions: • Script user logins • Check and change directory mappings • Check files and file versions • Examine and change program Registry settings • Check for, create, and delete shortcuts and folders You can take the script samples and the embedded functions and use them for your own scripts. You can even extend the standard capabilities with objects that plug into WSH. Later in this book, you'll see how to use the WSH objects with other objects to administer Windows NT server programs. Q&A Q What object should I use to access the Windows Registry? A You can use the WshShell object to read and write the Windows Registry. Q How can I determine the path to a user’s desktop folder? A The WshShell object provides a SpecialFolders property that allows you to determine the path to special Windows folders such as the desktop. Q Which object should I use to change network directory mappings? A You can use the WshNetwork object to change mappings. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Day 12: Automating Microsoft SQL Server Administration Using WSH Scripts Overview In this chapter, you’re going to see how you can use Microsoft SQL Server Management Objects to automate Microsoft SQL Server administration through WSH scripts. You’ll learn how to automate SQL Server tasks, and you’ll see scripts that give you examples you can use to create your own administrative scripts. On the 12th day of your introduction to WSH, you’ll do the following: • Discover Microsoft SQL Server and SQL Server Administration • Learn about the SQL Server Distributed Management Objects • See examples that show WSH scripts that use the SQL Server Distributed Management Objects to administer Devices, Databases, and Tables Introduction to SQL Server 6.5 Administration Microsoft SQL Server is a very popular database server for the Windows NT platform. It provides a robust database engine, tight integration with the Windows NT operating system, and a powerful and easy-to-use management interface. With the management interface, you can create new databases or database devices, back up or restore databases, change SQL Server execution settings, and perform many other tasks. Interestingly enough for you, the management interface uses ActiveX objects exposed by SQL Server to perform its functions. You can use the same ActiveX objects to perform your own administrative tasks programmatically. In this chapter, you'll see how you can use the ActiveX objects with WSH scripts to perform SQL Server administration. Note The concepts within this chapter are directly applicable to Microsoft SQL Server 7.0. The administration interface is different because with SQL Server 7.0, Microsoft incorporated SQL Server administration into the standard Microsoft Management Console (MMC) interface. However, the underlying objects are very similar. First, you're going to take a look at some very typical administrative tasks for SQL Server. You'll run through the steps that you would perform using the standard management interface. You're going to focus on the tasks of creating and deleting devices, databases, and tables. Next, you'll look at the administration objects that are available for SQL Server. For each object, you'll briefly examine the purpose of the object and its use. Finally, you'll look at detailed script examples that will show you how you can use the administration objects in practice. In the examples, you'll find functions that you can cut and paste into your own scripts if you choose. By the end of this chapter, you should have a clear idea of how administrative tasks can be performed with SQL Server and WSH scripts. You'll cover the basics in this chapter; using it as a foundation, you'll be able to create your own custom scripts. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Standard SQL Server Administration If you’re performing SQL Server administration, you’ll most typically use the SQL Server Enterprise Manager. The Enterprise Manager, depicted in Figure 12.1, provides a Windows Explorer style view of an instance of SQL Server. You’ll notice that different folders represent different "parts" of the SQL Server. Figure 12.1: The SQL Server Enterprise Manager. Table 12.1 summarizes the folders. Table 12.1 SQL Server Enterprise Manager Folders Folder Name Description Database Devices Devices are the actual files in the file system that store information for SQL Server databases. A single database might be split among multiple devices. It’s also very common for a database to use one device to store data and a separate device to store the database transaction log. Backup Devices Backup devices are used to store backups of databases. Databases Databases store all the information contained within SQL Server. Each database contains one or more tables, and each table consists of multiple rows and columns. If you expand the databases folder, you’ll see a list of the databases on the server. If you expand a database, you’ll see two folders: Group/Users contains all the valid groups and users for the database, and Objects contains all the components of the database, such as tables, views, and stored procedures. Logins This contains all the valid user logins for the SQL Server. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com To perform an administrative task, you can right-click an object or folder and pick a selection from the context menu that appears. For example, if I right-click on the Databases folder, as depicted in Figure 12.2, a menu will appear that will give me the option to create a new database. If I right-click on a specific database in the databases folder, the context menu gives me the capability to delete the database or edit it. Each object in the Enterprise Manager provides an appropriate context menu that gives you quick access to management functions. Figure 12.2: The context menu for the Databases folder. Alternatively, I could select one of the options on the Manage menu, such as Manage, Databases. If I do so, the Manage Databases dialog box will appear, as illustrated in Figure 12.3. Using the interface I can create, edit, or delete databases using convenient icons at the top-left of the interface. Figure 12.3: The Manage Databases dialog box. There’s yet another way to perform administrative tasks in SQL Server; you can use SQL commands in the SQL Query Tool. To display the SQL query window, you select Tools, SQL Query Tool from the menu. You can see the SQL query window illustrated in Figure 12.4; in the illustration, you can see a SQL query that will create a new database device. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 12.4: The SQL Query Tool. With the exception of the SQL query tool, all the other tools in the SQL Enterprise Manager use the SQL Distributed Management Objects (DMO) provided by SQL Server. The Distributed Management Objects provide a high-level, object-oriented interface for SQL Server administration. Fortunately for you, you can access the SQL DMO objects in your own programs to perform administrative tasks. In the next section, I’ll discuss the DMO objects and their capabilities. Administration Objects The SQL Server DMO administration objects can be used to administer literally any SQL Server feature. Consequently, there are many objects in the DMO object hierarchy. Figure 12.5 shows a diagram of the SQL DMO object hierarchy taken from the Microsoft SQL Server online documentation. Figure 12.5: The Microsoft SQL Server DMO object hierarchy. For all the details for the SQL DMO objects, please refer to the Microsoft SQL Server documentation. You’re going to use the objects in Table 12.2 in your WSH SQL Server administration scripts. Table 12.2 SQL Server DMO Objects Object Name Description Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com SQLServer The SQLServer object is the most important object in the SQL Server DMO object hierarchy. It’s the object that you use to connect to an instance of SQL Server to perform administrative functions. It provides collections of databases and devices that can be used to add or remove databases or devices. Device The Device object represents preallocated storage that SQL Server uses to store databases, transaction logs, or database backups. To add a new device to an instance of SQL Server, you’ll need to instantiate a Device object and add it to the SQLServer.Devices collection. Database The Database object contains information stored using a SQL Server. It provides the capability to change database attributes, execute database commands, and perform numerous other functions. For your purposes, you’re going to use it to create a new database. You’ll use it in conjunction with the Device and SQLServer objects. Table The Table object represents a table in a database. Table objects contain columns that represent the fields in a table. Column A Column object represents a single field within a database table. Scripting the SQL Server Distributed Management Objects Using WSH Now, you’re going to see how you can use WSH to script the management objects provided by SQL Server. First, you’ll see how you can connect to SQL Server to perform changes, and then you’ll work with scripts that affect database devices. Database devices are the physical files that contain databases and their tables; you’ll see how you can create and delete them. Next, you’ll look at creating and deleting databases. Databases contain one or more tables that store your information. Finally, you’ll see how you can use WSH to create, modify, and delete database tables. If you have SQL Server version 6.5 installed, you can follow along and execute the scripts as you go. If you have a different version, you might have problems because the management objects might be different. Connecting to SQL Server If you are working with the SQL Server Distributed Management Objects, you need to begin by connecting to a SQL Server. You do that by creating an instance of the SQLOLE.SQLServer object. Here’s a code snippet that shows how it’s done: Dim sqlSrv Set sqlSrv = Wscript.CreateObject("SQLOLE.SQLServer") sqlSrv.Connect "My Server", "My Login", "My Password" Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com including devices, databases, tables, and columns. When you have the object instance, you connect to the SQL Server using the SQLServer.Connect() method, which takes the server name, user login ID, and user password as parameters. Most of your scripts that use the SQL Server objects will begin with similar code so that you can connect to SQL Server. Creating and Deleting Devices Devices contain databases on SQL Server. Before you can create a database, you must have one or more devices to contain it. A typical configuration is to put the data portion of a database on one device and the transaction log for the database on a separate device. Doing so provides a significant performance enhancement. In either case, each device corresponds to a single file in the NT file system. To create devices, you use the Device object. To create the object, you use Wscript.CreateObject("SQLOLE.Device"). After you’ve created the Device object, you can change its settings by using its properties. When you’ve finished configuring it, you tell SQL Server to create it by adding it to the SQLServer.Devices collection. The code example in Listing 12.1 shows the details. Listing 12.1 Creating Devices Using SQL Server Management Objects ’ FILE: Makedev.vbs ’ DESC: This script demonstrates how you can create a new ’ device using the SQL Server management objects. ’ AUTH: Thomas Fredell ’ DATE: 1/16/99 ’ ’ Copyright 1999 MacMillan Publishing ’ ’ ’ Constants for the Server name, login ID, and password ’ NOTE: YOU MUST CHANGE THESE TO MATCH YOUR ENVIRONMENT. ’ Const SERVER_NAME = "TLFSRV1" Const LOGIN_ID = "sa" Const LOGIN_PW = "" Const DB_PATH = "F:\MSSQL\DATA\" ’ ’ The following are constants that were taken from the SQL ’ server objects type library as viewed using the OLE view utility. ’ ’ This is the listing that was taken directly from the ’ type information: ’ ’ SQLOLEDevice_Unknown = 100, Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... scripting capabilitiesà à •Ãà Learn about the administration components for IISà à à à à à à à à à à à Ãà à à Ãà à à Ãà à •Ãà See script examples that illustrate the use of the IIS objects from WSH scriptsà Introduction to Using WSH to Administer IISà à à à à à This chapter gives you some examples of how you can use Microsoft's Windows Scripting Host to automate the administration of the Internet Information... Server administration SQL Server is an excellent database engine that has become increasingly popular due to its ease of use and tight integration with Windows NT Administering SQL Server is fairly easy; however, some administrative tasks might be repetitive, tedious, or error-prone You can simplify SQL Server administration by implementing WSH scripts that enable you to automate your administrative... administer IIS through IIS Administration Objects Using WSH, you can easily develop scripts that perform many IIS administration functions using the administration objects In this chapter, you’ll learn you how IIS administration works and how you can use the IIS objects through WSH.à à On the 13th day of your introduction to WSH, you’llà à •Ãà Get an introduction to Microsoft IIS and administrative scripting... object using Wscript.CreateObject("SQLOLE.Table") The Table object contains Column objects that represent the fields in the table To add the columns, I need to create individual Column objects, change the settings for each Column, and then add the Column to the Table object using the Table.InsertColumn() method After I’ve finished adding columns to the table, I create the table in the database by adding... the device You can do so in your script using the standard File systemObject.à à à à Simpo 13: Automating Microsoft IIS Day PDF Merge and Split Unregistered Version - http://www.simpopdf.com Administration Using WSH Scripts à à à à à Overviewà à Microsoft provides an excellent Web server, Microsoft Internet Information Server (IIS), as a free add-on for the Windows NT operating system IIS comes with... the table to go ahead and make the changes using the Table.DoAlter() method.à à à à Deleting a table is very easy You might guess—correctly—that deleting a table probably involves the Database.Tables collection and the Remove() method Listing 12 .6 shows how you can delete the Products table that you created using a previous script. à à Listing 12 .6 Deleting Tablesà à ’ ’ ’ ’ ’ ’ ’à ’ ’à à à à à à Ã... chapter discusses IIS components and administration functions Finally the chapter shows some concrete examples that illustrate how you can automate IIS administration with WSH scripts.à à IIS Overviewà à Microsoft Internet Information Server is Microsoft's standard Web server for the Windows NT operating system IIS provides robust Web server functionality and includes components that provide mail, NNTP,... chapter to get listings of constants that I use within the sample scripts.à à Getting back to the script again, after the constants I create a SQLServer object that I use to connect to my database server Next, I create a Device object I configure the object by setting its Name, PhysicalLocation, Size, and Type properties Finally, I add the Device object to the SQLServer.Devices collection using the SQLServer.Devices.Add()... -à Sub EchoErr()à Wscript.Echo "ERROR: (" & Err.Number & ") " & Err.Descriptionà End Subà à à à à à à à à à à à After you run the script using cscript createdb.vbs, you’ll see two new tables in the NEWDB database in the SQL Server Enterprise Manager, as illustrated in Figures 13.13 and 13.14.à à à à à à à à à Figure 12.13: The new Customers table shown in the SQL Server Enterprise Manager.Ã... same coding pattern throughout the code examples.à à If you run the script using cscript makedev.vbs from the command line à à à à à à à device named NEWDEV has been created as illustrated in Figure http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Version - 12.11.à à à à à à à Figure 12.11: The new device listed in the SQL Server Enterprise Manager.à à à Ãà à à à Creating and Deleting Databasesà . of running the script. The before picture is illustrated in Figure 11.12. Figure 11.12: Directories before running the makfldr.vbs script. If you run the script using the command line makfldr. database server for the Windows NT platform. It provides a robust database engine, tight integration with the Windows NT operating system, and a powerful and easy-to-use management interface. With. plug into WSH. Later in this book, you'll see how to use the WSH objects with other objects to administer Windows NT server programs. Q&A Q What object should I use to access the Windows

Ngày đăng: 13/08/2014, 08:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan