Windows DevCenter    
 Published on Windows DevCenter (http://www.windowsdevcenter.com/)
 See this if you're having trouble printing code examples


O'Reilly Book Excerpts: Windows Server Cookbook

Cooking with Windows Server, Part 1

by Robbie Allen

In this excerpt from Robbie Allen's Windows Server Cookbook, Robbie shows you how to activate Windows Server 2003, how to find large folders and files on a volume, and how to hide a file or folder.

Activating Windows Server 2003

Problem

You want to activate or view the activation status of a Windows Server 2003 system. Microsoft requires that you activate Windows Server 2003 within 30 days of installation to validate you have a legal copy of the software.

Solution

Using a graphical user interface

  1. From the Start menu, select All Programs → Activate Windows.

  2. Select whether you want to activate by phone or over the Internet.

Using a command-line interface

The following command opens the Activation wizard described in the previous section:

> %systemroot%\system32\oobe\msoobe /a

There are no options that can activate the computer silently from the command line.

Using VBScript

' This code activates a Windows Server 2003 system.
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
   
boolActivateOnline = True  ' If this is true, boolActivateOffline should
                           ' be false
   
boolActivateOffline = False ' If this is true, boolActivateOnline should
                            ' be false
strOfflineConfirmationCode = "1234-5678" ' if Activating offline, you need
                                         ' specify a confirmation code
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colWPA = objWMI.InstancesOf("Win32_WindowsProductActivation")
for each objWPA in colWPA
   WScript.Echo "Activation Settings:"
   Wscript.Echo "  Activation Required: " & objWPA.ActivationRequired
   Wscript.Echo "  Caption: " & objWPA.Caption
   Wscript.Echo "  Description: " & objWPA.Description
   Wscript.Echo "  Notification On: " & objWPA.IsNotificationOn
   Wscript.Echo "  Product ID: " & objWPA.ProductID
   Wscript.Echo "  Remaining Eval Period: " & objWPA.RemainingEvaluationPeriod
   Wscript.Echo "  Remaining Grace Period: " & objWPA.RemainingGracePeriod
   Wscript.Echo "  Server Name: " & objWPA.ServerName
   Wscript.Echo "  Setting ID: " & objWPA.SettingID
   WScript.Echo
   
   if objWPA.ActivationRequired = True then
      if boolActivateOnline = True then
         intRC = objWPA.ActivateOnline
         if intRC <> 0 then
            WScript.Echo "Error activating online: " & intRC
         else
            WScript.Echo "Successfully activated online"
         end if
      end if
   
      if boolActivateOffline = True then
         intRC = objWPA.ActivateOffline(strOfflineConfirmationCode)
         if intRC <> 0 then
            WScript.Echo "Error activating offline: " & intRC
         else
            WScript.Echo "Successfully activated offline"
         end if
      end if
   end if
next

Discussion

Microsoft estimates that it has lost billions of dollars due to pirated, or illegally copied, software. Microsoft Product Activation (MPA) is a new technology used to reduce software piracy. Microsoft has integrated MPA into their most recent operating systems (i.e., Windows XP and Windows Server 2003) and some consumer applications (e.g., Microsoft Office). MPA is designed to prevent casual copying. For example, let's say you buy a single copy of Windows XP from Best Buy, install it on your computer, and then lend the CD to a friend who installs it on his computer. The second copy is considered pirated and not legitimate under MPA.

MPA is an umbrella term for the various Microsoft activation technologies such as Windows Product Activation (WPA) and Office Activation Wizard (OAW). You can run an unactivated copy of Windows for up to 30 days before you must activate it to use the system further. Over those 30 days, you are periodically reminded to activate Windows via a pop-up window from the system tray. The longer you wait the more frequent these notifications become. And they can be down right aggravating if you don't plan to activate the system for a number of days. Fortunately, you can disable these notifications. Here is a small script that does it:

strComputer = "."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colWPA = objWMI.InstancesOf("Win32_WindowsProductActivation")
for each objWPA in colWPA
   objWPA.SetNotification(0)
next

Just don't forget that you still need to activate the system!

The activation process is an anonymous and relatively quick operation. No personally identifiable information is needed to activate the product. There are three different ways you can activate Windows. First, you can use a broadband connection over the Internet. This requires that ports 80 and 443 (HTTP and HTTPS) are open through your firewall, which if you use the Internet they undoubtedly are. Second, WPA can automatically connect to the Internet using a modem. And third, you can call a toll-free number to activate it over the phone.

If you have questions about WPA, see MS KB 302878, which is the MPA FAQ.

See Also

MS KB 291983 (Ports That Are Used by Windows Product Activation), MS KB 291997 (How to Activate Windows XP Using an Unattend.txt File), MS KB 302806 (Description of Microsoft Product Activation), MS KB 302878 (Frequently Asked Questions about Microsoft Product Activation), MS KB 325510 (Support WebCast: Microsoft Windows XP and Office XP: Understanding the New Mandatory Product Activation), and MS KB 326851 (Activation and Registration of a Microsoft Product)

Windows Server Cookbook

Related Reading

Windows Server Cookbook
For Windows Server 2003 & Windows 2000
By Robbie Allen

Finding Large Files and Folders on a Volume

Problem

You want to find files or folders that exceed a certain size on a volume.

Solution

Using a graphical user interface

  1. From the Start menu, select Search.

  2. If you are presented with the options for what to search on, click All files and folders.

  3. Click on What size is it?

  4. Select the radio button beside Specify size and enter the size you want to search.

  5. Select additional criteria if necessary and click Search.

Using a command-line interface

The following command finds folders that are greater than 100 MB in size on the D: drive:

> diruse /s /m /q:100 /d d:

The /s option causes subdirectories to be searched, the /m option displays disk usage in megabytes, the /q:100 option causes folders that are greater than 100 MB to be marked, and the /d option displays only folders that exceed the threshold specified by /q.

Use the diskuse command to find files over a certain size. The following command displays files over 100 MB in size on the D: drive:

> diskuse D: /x:104857600 /v /s

The /x:104857600 option causes files over 104,857,600 bytes to be displayed and is valid only if you include the /v option (verbose). The /s option means subdirectories from the specified path (in this case, the D: drive) are searched.

Using VBScript

' This code finds all files over a certain size.
' ------ SCRIPT CONFIGURATION ------
strComputer = "<ServerName>" 
intSizeBytes = 1024 * 1024 * 500  ' = 500 MB
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colFiles = objWMI.ExecQuery _
    ("Select * from CIM_DataFile where FileSize > '" & intSizeBytes & "'")
for each objFile in colFiles
    Wscript.Echo objFile.Name & "  " & objFile.Filesize / 1024 / 1024 & "MB"
next

Discussion

If you find that you are running out of space on a volume and want to see what is consuming the most space, you are better off using the diruse command-line solution. With the other solutions, you could search for all files over 100 MB, for example, but a user could have created a bunch of 10 MB MPEG files. Unfortunately, you can't use the Search dialog box or VBScript to search on folder sizes, which leaves diruse as the most appropriate tool in this scenario.

See Also

Recipe 3.7 and MSDN: CIM_DataFile

Windows Server Cookbook

Related Reading

Windows Server Cookbook
For Windows Server 2003 & Windows 2000
By Robbie Allen

Hiding a File or Folder

Problem

You want to hide a file or folder from view within Windows Explorer.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. Browse to the file or folder you want to hide.

  3. Right-click the file or folder and select Properties.

  4. Check the box beside Hidden (to hide) or uncheck the box (to unhide).

  5. Click OK.

Using a command-line interface

To hide a file, use the attrib.exe command:

> attrib +H <Path>

Here is an example:

> attrib +H d:\mysecretscript.vbs

To unhide a file, use the -H option:

> attrib -H <Path>

Here is an example:

> attrib -H d:\mysecretscript.vbs

Using VBScript

' This code hides or unhides
 a file.
' ------ SCRIPT CONFIGURATION ------
strFile = "<FilePath>"  ' e.g., d:\mysecretscript.vbs
boolHide = True         ' True to hide, False to unhide 
' ------ END CONFIGURATION ---------
set objFSO = CreateObject("Scripting.FileSystemObject")
   
' Change this to GetFolder to hide/unhide a folder
set objFile = objFSO.GetFile(strFile)
   
if boolHide = True then
   if objFile.Attributes AND 2 then
      WScript.Echo "File already hidden" 
   else
      objFile.Attributes = objFile.Attributes + 2 
      WScript.Echo "File is now hidden"
   end if 
else
   if objFile.Attributes AND 2 then
      objFile.Attributes = objFile.Attributes - 2 
      WScript.Echo "File is not hidden"
   else
      WScript.Echo "File is already not hidden" 
   end if 
end if

Discussion

There are many operating system files that are hidden by default. Microsoft did this so you don't get yourself into trouble by accidentally editing or deleting important system files. You also may want to do this if you don't want users to see certain files or folders. The files and folders will still be accessible if the users know the full path; they just won't be visible by default in Windows Explorer. That, however, can be easily circumvented. Windows Explorer provides an option to make all hidden files and folders viewable. From the menu, select ToolsFolder Options. Click the View tab. You just need to select Show hidden files and folders and you'll be able to see them. If you truly don't want users to be able to access certain files or folders, your best bet is to restrict access to them via NTFS permissions.

See Also

MS KB 141276 (How to View System and Hidden Files in Windows)

Robbie Allen is the coauthor of Active Directory, 2nd Edition and the author of the Active Directory Cookbook.


View catalog information for Windows Server Cookbook

Return to the WindowsDevCenter.com.

Copyright © 2009 O'Reilly Media, Inc.