When it comes to scripting tasks for the Windows Server 2003 system, you can find the server objects in Active Directory using ADSI or ADO. To actually connect to the server to make changes or record information, you should use Windows Management Instrumentation. WMI can be used to access the operating system to collect performance statistics. ADSI can be used to create, query, or modify Active Directory objects through an LDAP or global catalog (GC) connection. Likewise, command-line tools can be used to perform a variety of tasks, from remotely rebooting a server to mapping network drives and printers. Introducing Windows Management InstrumentationWindows Server 2003 systems can be managed using the interfaces and object models available with Windows Management Instrumentation. WMI is Microsoft's implementation of Web-Based Enterprise Management (WBEM), which is an industry initiative, aimed at providing better management and improved remote monitoring of server and network devices in the enterprise. WMI can be used to access and manipulate server files and file security and handle server configurations such as creating file shares or installing printers and managing services. WMI can also be used to manage applications such as terminal services through specific WMI providers. Creating a Simple WMI ScriptAs in the previous scripts outlined to manage users, before you can access or manipulate objects, you must know how to connect, what properties can be managed, and how they can be managed. In other words, you need to become familiar with the WMI interface and the object model for servers or workstations. To create a simple WMI script to connect to a specific server and list each of the local volume drive letter assignments and total volume capacity, follow these steps:
You can extend this script to also list free space by changing the oDisk.Size reference to oDisk.FreeSpace. Change this code in your script to see the difference. Administrators also usually need to remotely stop and restart a service on a server. Although you can perform this task using a graphical user interface, using a WMI script provides the flexibility to change the script to manage a different server or many servers in an OU or domain. As an example, the following script will connect to the server dc1.companyabc.com and stop and start the World Wide Web Publishing service:
Using the preceding code, you should change the reference from dc1.companyabc.com to the fully qualified domain name of your server; this will work only if the server already has IIS Web services installed. Because this service may take a few seconds to stop, the wscript.sleep command was added to pause the script for 5 seconds, or 5,000 milliseconds. The better way to let the service finish before restarting is to monitor the status of the service (Starting, Running, Stopping, Stopped, and so on). Another significant improvement would be to enumerate dependent services and restart them along with this service. To change this script to manage a different service, replace W3SVC with the desired service name. To locate the correct service name to use with this script, open the Services applet, open the properties of the desired service, and locate the service name referenced at the top of the General tab. Use the service name, not the display name. |