While working with Windows XP/2003 Windows Product Activation (WPA), you will definitely run into your share of "activation" support calls. Luckily, Microsoft has had the foresight to include the ability to script common activation tasks .
To determine the activation status using Windows Script Host, proceed as follows :
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & computer & "\root\cimv2")
Set objWPA = objWMI.ExecQuery("Select * from Win32_WindowsProductActivation") For Each PA in objWPA If PA .ActivationRequired = 0 Then Wscript.Echo "Product Already Activated" Else Wscript.Echo "Product Not Activated" & vbcrlf & _ "Remaining Evaluation Period: " & PA .RemainingEvaluationPeriod & _ vbcrlf & _ "Remaining Grace Period: " & PA .RemainingGracePeriod End If Next
Note | The highlighted code above must be placed on one line. |
Here, computer is the computer name to query.
To prevent Windows Product Activation notices reminding you to activate using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objWMI = GetObject("winmgmts:{impersonationLevel= impersonate}!\" & computer & "\root\cimv2")
Set objWPA = objWMI .ExecQuery("Select * from Win32_WindowsProductActivation") For Each PA in objWPA PA .SetNotification(0) Next
Here, computer is the computer name to disable notices.
Note | The highlighted code above must be placed on one line. Disabling activation notices does not prevent the need to activate; it only prevents the reminder notices. |
To activate Windows XP/2003 using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objWMI = GetObject("winmgmts:{impersonationLevel= _ impersonate}!\" & computer & "\root\cimv2") Set objWPA = objWMI.ExecQuery("Select * from _ Win32_WindowsProductActivation") For Each PA in objWPA PA .ActivateOnline() Next
Here, computer is the computer name to activate.
While system restores are normally performed through the System Restore Utility, you can also script restores through WMI. The WMI system restore class (SystemRestore) provides methods to enable/ disable the system restore feature, create restore points, list restore points, and roll back to a restore point.
To enable system restore on all drives through WMI, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objWMI = GetObject("winmgmts:\" & c omputer & "\root\default") Set objSR = objWMI .Get("SystemRestore") objSR .Enable("")
Here, computer is the name of the remote computer.
To disable system restore on all drives, change the method name "Enable" to "Disable."
To create a restore point through WMI, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objWMI = GetObject("winmgmts:\" & computer & _ "\root\default:SystemRestore") objWMI .CreateRestorePoint "Scripted Restore Point", 0, 100
Here, computer is the name of the remote computer.
To list all system restore points through WMI, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objSR = GetObject("winmgmts:\" & computer & _ "\root\default").InstancesOf("SystemRestore") If objSR .Count = 0 Then WScript.Echo "No restore points found." Else Set objWMIDate = CreateObject("WbemScripting.SWbemDateTime") For Each RP in objSR Select Case RP .RestorePointType Case 0 RPT = "Application install" Case 1 RPT = "Application Uninstall" Case 2 RPT = "Desktop Settings" Case 3 RPT = "Accessibility Settings" Case 4 RPT = "Outlook Express Settings" Case 5 RPT = "Application Run" Case 6 RPT = "Restore" Case 7 RPT = "Checkpoint" Case 8 RPT = "Windows Shutdown" Case 9 RPT = "Windows Boot" Case 10 RPT = "Device Drive install" Case 11 RPT = "First Run" Case 12 RPT = "Modify Settings" Case 13 RPT = "Cancelled Operation" Case 14 RPT = "Backup Recovery" Case Else RPT = "Unknown" End Select objWMIDate .Value = RP.CreationTime Wscript.Echo "Date: " & objWMIDate .GetVarDate & vbcrlf & _ "Number: " & RP .SequenceNumber & vbcrlf & _ "Description: " & RP .Description & vbcrlf & _ "Type: " & RPT Next End If
Here, computer is the name of the remote computer.
To roll back to an existing restore point through WMI, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objWMI = GetObject("winmgmts:\" & computer & _ "\root\default:SystemRestore") objWMI .Restore RestoreNumber Set OS = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & computer & "\root\cimv2").ExecQuery ("select * from Win32_ OperatingSystem where Primary=true") For each System in OS System .Reboot() Next
Here, computer is the name of the remote computer and RestoreNumber is the restore point sequence number. The actual restore occurs during the reboot process.
Note | The highlighted code on the previous page must be placed on one line. |
Related solution: | Found on page: |
---|---|
Rebooting a System | 190 |
In Chapter 8 you learned how to script the MMC from the command line. This section will show you how to script the MMC using the MMC 2.0 Automation object and Window Script Host.
To load a console file using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objMMC = CreateObject("MMC20.Application") objMMC .Load(" ConsoleFile ") objMMC .Show objMMC .UserControl = 1
Here, ConsoleFile is the location of the console file to load.
To save a console file using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objMMC = CreateObject("MMC20.Application") Set objDOC = objMMC .Document objDOC .SaveAs(" ConsoleFile ") objDOC .Close(true)
Here, ConsoleFile is the location of the console file to save.
To add a snapin to an MMC using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objMMC = CreateObject("MMC20.Application") Set objDOC = objMMC .Document objDOC .SnapIns.Add " snapinname " objMMC. show objMMC .UserControl = 1
Here, snapinname is the name of the snapin to add (i.e. "Event Viewer", "Local Users and Groups").
Starting with Windows XP/2003, Microsoft has included a few new classes and objects to WMI. While the sections below explore a few additions, you can visit the following site for a complete list:
http://msdn.microsoft.com/library/default.asp?url=/library/enus/wmisdk/wmi/what_s_new_in_wmi.asp
WMI uses the Common Information Model (CIM) DateTime format for date and time values which displays dates and times as yyyymmddHHMMSS.mmmmmmsUUU or yyyy-mm-dd HH:MM:SS:mmm . You can use the sWbemDateTime object to translate CIM formatted dates and time. To translate a WMI CIM formatted date and time using the sWbemDateTime object, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set objDT = CreateObject("WbemScripting.SWbemDateTime") Set objWMI = GetObject("winmgmts:\" & Computer & "\root\cimv2") Set objOS = objWMI.ExecQuery("Select LocalDateTime from Win32_OperatingSystem") For Each OS in objOS objDT .Value = OS .LocalDateTime WScript.Echo "Original: " & OS .LocalDateTime & vbcrlf & _ "Formatted: " & objDT .GetVarDate Next
Note | The highlighted code above must be placed on one line. |
Here, computer is the name of the remote system. The example above retrieves the current time of the remote computer and displays both the original CIM formatted date/time and the translated date/time.
You can use the WMI Win32_PingStatus class to ping a network device and retrieve the returned results through scripting. To ping a network device and display the results using WMI, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
set objPING = GetObject("winmgmts:{impersonationLevel=impersonate}")._ ExecQuery ("select * from Win32_PingStatus where address ='"_ & NetworkDevice & "'") For Each PING In objPing Select Case PING .StatusCode Case 0 Wscript.Echo "Reply from " & PING .ProtocolAddress & _ ": bytes=" & PING .BufferSize & " " & _ "time=" & PING .ResponseTime & " " & _ "TTL=" & PING .ResponseTimeToLive Case 11001 wscript.echo "Buffer Too Small" Case 11002 wscript.echo "Destination Net Unreachable" Case 11003 wscript.echo "Destination Host Unreachable" Case 11004 wscript.echo "Destination Protocol Unreachable" Case 11005 wscript.echo "Destination Port Unreachable" Case 11006 wscript.echo "No Resources" Case 11007 wscript.echo "Bad Option" Case 11008 wscript.echo "Hardware Error" Case 11009 wscript.echo "Packet Too Big" Case 11010 wscript.echo "Request Timed Out" Case 11011 wscript.echo "Bad Request" Case 11012 wscript.echo "Bad Route" Case 11013 wscript.echo "TimeToLive Expired Transit" Case 11014 wscript.echo "TimeToLive Expired Reassembly" Case 11015 wscript.echo "Parameter Problem" Case 11016 wscript.echo "Source Quench" Case 11017 wscript.echo "Option Too Big" Case 11018 wscript.echo "Bad Destination" Case 11032 wscript.echo "Negotiating IPSEC" Case 11050 wscript.echo "General Failure" End Select Next
Here, networkdevice is the name or IP address of the device to ping.