Windows Server 2003 Scripting


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 Instrumentation

Windows 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 Script

As 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:

1.

Log on to a workstation or server with an account that has administrative rights on the server you want to query.

2.

Choose Start, Run.

3.

Type notepad.exe and click OK. Enter the following code in the Notepad window:

[View full width]

Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}! \\dc1.companyabc .com\root\cimv2") Set colDisks = oWMIService.ExecQuery ("Select * from Win32_LogicalDisk") For Each oDisk In colDisks If oDisk.DriveType = 3 Then Wscript.echo oDisk.Name & vbTab & CStr(Round(oDisk.Size/1048576)) End If Next


4.

Using the code from step 3, change the reference from dc1.companyabc.com to the fully qualified domain name of your server or workstation.

5.

Save the file as diskinfo.vbs in the c:\Scripts directory and close Notepad.

6.

Choose Start, Run.

7.

Type cmd.exe and click OK to open a command prompt.

8.

At the command prompt, type c:\scripts\diskinfo.vbs and press Enter. The disk drive letters should then be listed along with their volume capacity.

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:

[View full width]

Set oService = GetObject("winmgmts:{impersonationLevel=impersonate}! \\dc1\root\CIMV2 :Win32_Service.Name=" + Chr(34) + "W3SVC" + Chr(34)) If oService.Started Then oService.StopService Wscript.echo "the service has been stopped" Wscript.sleep 5000 oService.StartService Wscript.echo "The service has been restarted." Else Wscript.echo "The service is not currently running and will not be started." End If Set oService = Nothing


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.




Microsoft Windows Server 2003 Unleashed(c) R2 Edition
Microsoft Windows Server 2003 Unleashed (R2 Edition)
ISBN: 0672328984
EAN: 2147483647
Year: 2006
Pages: 499

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net