Saturday, March 20, 2010

How to Capture a Local Computer Name Using VBS

VBScript (VBS) is a plain text coding language that network administrators use to automate configurations on a computer. The VBS language can be used to retrieve the name of the computer on which it is running. This is useful for administrators who want to use logging capabilities or display the name of the computer to the user. Retrieving the name of the computer only takes a few lines of code in your VBS file.

Instructions
  1. Step 1

    Create the shell object. The shell object contains several properties and functions that can be used to retrieve information from the machine. The following code is placed as the first line in your editor:
    Set network = WScript.CreateObject( "WScript.Network" )

  2. Step 2

    Capture the name of the machine using the "ComputerName" property contained in the shell object created in Step 1. In this example, the computer name is captured and set to a string variable. The following code illustrates how to capture the local computer name:
    computer_name = network.ComputerName

  3. Step 3

    Display the computer name to the user in the command prompt. The shell object has a predefined function that displays output to the user. It's also useful for debugging your VBS application. Use the following line as the last statement in your VBScript code:
    WScript.Echo "Computer Name: " & computer_name

No comments:

Post a Comment