How Can I List All Servers in a Domain That Have DHCP Installed?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I quickly produce a list of all servers in the domain that have the DHCP Service installed?

- SC

SpacerHey, Scripting Guy! Answer

Hi SC,

On Windows Server 2008 and ater, use the WIN32_ServerFeature WMI class. The GetDHCPServer.vbs script shows this process:

GetDHCPServer.vbs

Set objWMIService = GetObject("winmgmts:")Set colFeatureList = objWMIService.ExecQuery("SELECT * FROM Win32_ServerFeature WHERE Name = 'DHCP Server'")For Each objFeature In colFeatureList   WScript.Echo objFeature.ID & ": " & objFeature.Name & " (Parent: " & objFeature.ParentID & ")"Next

 

How Can I Get This VBScript to Work?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! When I copy (or completely retype) the code from the How Do I Run an Office Excel Macro on Multiple Workbooks “Hey, Scripting Guy!” article, I receive a compile error 800A0408. This error claims that the character (1,1) (aka $) is invalid. Any ideas why this is occurring?

- CR

SpacerHey, Scripting Guy! Answer

Hi CR,

That script is written in Windows Powershell and not VBScript. I know it can be a bit confusing, and I generally try to say something about Windows PowerShell in the article, but I forget occasionally.

So how can you tell if a script is written in Windows PowerShell? Here are three signs you are dealing with a Windows PowerShell script:

It will use the dollar sign ($), which is used for variables in Windows PowerShell. This is always a dead giveaway you have a Windows PowerShell script because, as you saw in your error message that you listed in your e-mail message, the dollar sign is not an allowed character in VBScript.

The comment character in Windows PowerShell is the number sign (#). This will cause strange results in VBScript.

Windows PowerShell uses cmdlets. All cmdlets have a dash in their name, things such as Get-Content. Even though we could use a dash in VBScript names, most VBScript writers (including me) do not like dashes because they are too difficult to type—so we avoid them.

A typical Windows PowerShell script might resemble the GetDiskInformation.ps1 script seen here. You can see the number sign comment character (#), the dollar sign variable name ($computer), and the Get-WmiObject cmdlet:

GetDiskInformation.ps1

# ------------------------------------------------------------------------# NAME: GetDiskInformation.ps1# AUTHOR: Ed Wilson, Microsoft# DATE: 4/8/2009## KEYWORDS: Get-WmiObject, Win32_LogicalDisk## COMMENTS: This script retrieves the free disk space # for each logical hard drive on a computer## ------------------------------------------------------------------------$computer = "$env:computername"Get-WmiObject -Class Win32_LogicalDisk `  -computername $computer -Filter "drivetype = 3"

On the other hand, VBScripts have several things that generally will let you know that you are dealing with a VBScript. Here are some of the more telling signs:

They use CreateObject or GetObject.

The comment characters are either REM or a single quotation mark ().

They use Wscript.Echo to print information out.

Variables have no special characters, but resemble objWMIService.

To store objects in variables, VBScript uses the Set command.

A VBScript that produces the same output as the GetDiskInformation.ps1 is the GetDiskInformation.vbs script. In the GetDiskInformation.vbs script, the comment character is the single quotation mark ('). The variables in the GetDiskInformation.vbs script are strComputer, SWBemlocator, objWMIService, objItem, and colItems. CreateObject is used to create an instance of the WbemScripting.SWbemLocator object, and the Set command is used to store three objects. The GetDiskInformation.vbs script is seen here:

GetDiskInformation.vbs

' ------------------------------------------------------------------------' NAME: GetDiskInformation.vbs' AUTHOR: ed wilson, Microsoft' DATE: 4/8/2009'' KEYWORDS: SwbemLocator Object, Win32_LogicalDisk'' COMMENTS: This script retrieves the free disk space ' for each logical hard drive on a computer'' ------------------------------------------------------------------------strComputer = "."Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2")Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk",,48)For Each objItem In colItems                WScript.Echo "DeviceID: " & objItem.DeviceID                WScript.Echo "DriveType: " & objItem.DriveType                WScript.Echo "ProviderName: " & objItem.ProviderName                WScript.Echo "Size: " & objItem.Size                WScript.Echo "VolumeName: " & objItem.VolumeNameNext

Here is the Getting Started Guide for VBScript and the Getting Started Guide for Windows PowerShell. From a learning perspective, if I were just starting out I would learn Windows PowerShell because it is the up-and-coming scripting language. It is part of the Microsoft strategic direction moving forward. It is included with Windows Server 2008, and installed by default on Windows 7 and Windows 2008 R2. Our new management tools are being built on Windows PowerShell as well. VBScript, on the other hand, is in maintenance mode, which means no new features are being added and the only development work that is being done is for software updates.

 

How Does the Future of the Script Center Look?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I received the January through June Scripting Newswire messages, but nothing since then. Was it eliminated? Clicking either of the links at the bottom—update your profile or sign up for other newsletters—takes me to a search page. Also, I cannot find a link on the Script Center for any subscriptions. Did I miss it?

- GB

SpacerHey, Scripting Guy! Answer

Hi GB,

The Scripting Newswire has died. When we convert the Script Center to a new publishing platform (midsummer time frame), we will have much more Web functionality available to us than we have had. Believe it or not, the current site is a completely manually updated site. No feeds whatsoever.

If you follow us on Twitter, you can keep up with which “Hey, Scripting Guy!” articles are on their way, what is current, what our plans are for the monthly TechNet Magazine article, what new scripts are posted, and so on. We also plan to pop out cool little tricks we collect along the way.

 

How Do I Check My Computer for Viruses?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! My computer is acting somewhat strange. I think that I may have a virus, but I am not sure what to do. Can you help me?

- JB

SpacerHey, Scripting Guy! Answer

Hi JB,

Have you seen our free Windows Live OneCare safety scanner? It is pretty cool.

 

How Can I Compile Scripts?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need your help, if it is possible. I wrote several scripts, and one of these scripts starts the others. Now, I want to make a unique .exe file that has all these scripts: is this possible? If yes, how?

- UG

SpacerHey, Scripting Guy! Answer

Hi UG,

If you wrote the script in VBScript, you can transfer it fairly easily to Visual Basic and compile it. If you wrote the script in Windows PowerShell, you can transfer it without much difficulty to C# because both are .NET Framework languages.

As for directly making an executable out of a VBScript, we have no tools to do this. I know there is at least one third-party tool that will do this. But the last time I checked, the executable was not a stand-alone executable and as a result requires the tool to be installed on each computer that will run the compiled file.

There is one other thing that you could do that might be an interesting solution. Convert your VBScript code to script components. We have some fairly good documentation about script components on MSDN. Check it out; it may be able to do what you have to do.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys