Script to get Computer Name

Posted on 2005-05-12
Medium Priority
Last Modified: 2013-12-28

I am writting a script to extract Windows server NT/2000/2003 user accounts password information for auditing. I achieved almost all information I need from my script.

However, I could not get this piece from my script to work with NT server (It works in 2K and 2K3), this small script come from Microsoft scripting center:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colSettings = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_ComputerSystem")
For Each objComputer in colSettings
    Wscript.Echo "Computer Name: " & objComputer.Name

This script works on W2K and W2K3 server but not NT. My goal is modify and add this to my script to get computer name and feed it back as a variable so I don't need to hard code or type in the computer name. I need to run my srcipt on different domains and stand alone server from NT to W2K3.

My test NT server already has ADSI installed. What do I need to do make this script works on NT?


Question by:locpham
  • 4
  • 3
  • 2
  • +1
LVL 97

Expert Comment

by:Lee W, MVP
ID: 13989757
You can use the environment variable %computername% - but how to incorporate that into a vbscript I'm not sure.  

Author Comment

ID: 13989820

I believe %computername% is used in a batch file. This ia VBS script.

I did try  %computername% before but it didn't work. May be I don't know how!
LVL 97

Expert Comment

by:Lee W, MVP
ID: 13990050
Are you sure the NT4 system have VBScript on them?  AS I recall, they didn't come with it, but I do think there was an add-on.  (One reason I never use it - I can't be sure the NT 4 systems are going to be able to execute it).

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 13990246

I am 100% sure. My VBS script already works on NT. It extracts user accounts, last time logon, never logon, last change password, expiration date, account expired and account disabled information.

The only draw back with my script is I have to manually enter the server name when I run it from different servers/domains. I want to automate it by having the script gets the computer name by itself.
LVL 97

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 90 total points
ID: 13990388
See if any of the information here helps:

Author Comment

ID: 13990566

Before I posted my question. I came accross the same link you prefer to. Basically the script in the link uses the same scripting method/syntax. But I will look at it again...
LVL 85

Accepted Solution

oBdA earned 210 total points
ID: 13990657
This will put the environment variable %Computername% into the VB script variable sComputername:

Set oShell=CreateObject("wscript.shell")
Set oEnvironment=oShell.Environment("Process")
wscript.Echo "Computername: [" & sComputerName & "]"

Author Comment

ID: 13991151

This script work! Now I need to add it to my script.

I'd like to give 30 point to leew for his effort. 70 point to oBdA.
LVL 85

Expert Comment

ID: 13997850
Well, go right ahead, then ;)
More than one Expert helped solve my problem. What do I do?

Expert Comment

ID: 20275038
ASP.NET copy and paste code into the page load for the form.

        // get the name of the computer the web page is running on.
        System.Net.IPHostEntry host = new System.Net.IPHostEntry();
        host = System.Net.Dns.GetHostByAddress(Request.ServerVariables["REMOTE_HOST"]);
        ComputerName = host.HostName;

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
The article covers five tools all IT professionals should know about, as they up productivity by a great deal!
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question