' ========================================================================== ' VBSNames.vbs, Version 3.00 ' ' This script demonstrates several techniques to retrieve user names, ' computer names, domain names and some related properties. ' ' Just run it to see which techniques work in your environment and which ' ones don't. This script won't change anything on your computer, it only ' reads some properties. ' ' Written by Rob van der Woude ' http://www.robvanderwoude.com ' ========================================================================== Option Explicit Dim arrDomainRole( 5 ), colItems, objIP, objItem, objJava, objLM Dim objReg, objSysInfo, objWMISvc, strHostname, wshNetwork, wshShell On Error Resume Next ' ========================================================================== ' Except for "Logon Server" and "User Domain", the next technique will work ' on any computer running Windows NT 4, 2000, XP, Server 2003 or Vista, ' wether stand-alone, in a workgroup, NT or AD domain. ' "Logon Server" and "User Domain" will be empty on stand-alone machines. ' This technique doesn't work on Windows 95/98/ME machines. ' ========================================================================== Set wshShell = Wscript.CreateObject( "Wscript.Shell" ) WScript.Echo "Windows Environment Variables:" WScript.Echo "==============================" WScript.Echo "Computer Name : " & wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" ) WScript.Echo "Logon Server : " & wshShell.ExpandEnvironmentStrings( "%LOGONSERVER%" ) WScript.Echo "User Domain : " & wshShell.ExpandEnvironmentStrings( "%USERDOMAIN%" ) WScript.Echo "User Name : " & wshShell.ExpandEnvironmentStrings( "%USERNAME%" ) WScript.Echo Set wshShell = Nothing ' ========================================================================== ' This technique should work on any Windows computer with WSH installed ' (wich excludes 95), wether stand-alone, in a workgroup, NT or AD domain. ' Requires WSH, so it won't work in HTAs. ' ========================================================================== Set wshNetwork = Wscript.CreateObject( "Wscript.Network" ) WScript.Echo "WSHNetwork:" WScript.Echo "===========" WScript.Echo "Computer Name : " & wshNetwork.ComputerName WScript.Echo "User Domain : " & wshNetwork.UserDomain WScript.Echo "User Name : " & wshNetwork.UserName WScript.Echo Set wshShell = Nothing ' ========================================================================== ' Except for "PDC" and "User Domain", the following next will work on any ' computer running NT 4 or later, wether in a workgroup, NT or AD domain. ' "PDC" will be empty on stand-alones or workgroup members. ' "User Domain" will be empty on stand-alones. ' This technique works on Windows 95/98/ME machines too, if AD client ' software is installed. ' ========================================================================== Set objSysInfo = CreateObject( "WinNTSystemInfo" ) WScript.Echo "ADSI (WinNTSystemInfo):" WScript.Echo "=======================" WScript.Echo "Computer Name : " & objSysInfo.ComputerName WScript.Echo "PDC : " & objSysInfo.PDC WScript.Echo "User Domain : " & objSysInfo.DomainName WScript.Echo "User Name : " & objSysInfo.UserName WScript.Echo Set objSysInfo = Nothing ' ========================================================================== ' The next technique requires Active Directory. ' It doesn't work on stand-alones or computers in a workgroup or NT domain. ' This technique works on Windows 95/98/ME machines too, if AD client ' software is installed. ' Use custom error handling when using this technique. ' ========================================================================== Set objSysInfo = CreateObject( "ADSystemInfo" ) WScript.Echo "ADSI (ADSystemInfo):" WScript.Echo "====================" WScript.Echo "Computer Name : " & objSysInfo.ComputerName WScript.Echo "User Domain : " & objSysInfo.DomainName WScript.Echo "User Name : " & objSysInfo.UserName WScript.Echo Set objSysInfo = Nothing ' ========================================================================== ' The next technique will work on any WMI enabled computer, wether ' stand-alone, in a workgroup,' NT domain or AD domain. ' Domain related properties will be empty on stand-alone machines or ' workgroup menmbers. ' "Workgroup" is unreliable, as it often doesn't even work on workgroup ' members running Windows XP Professional SP2. ' This technique works on Windows 95/98/ME machines, if they have WMI ' enabled. ' ========================================================================== Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" ) Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 ) WScript.Echo "WMI (Win32_ComputerSystem class):" WScript.Echo "=================================" arrDomainRole( 0 ) = "Standalone Workstation" arrDomainRole( 1 ) = "Member Workstation" arrDomainRole( 2 ) = "Standalone Server" arrDomainRole( 3 ) = "Member Server" arrDomainRole( 4 ) = "Backup Domain Controller" arrDomainRole( 5 ) = "Primary Domain Controller" For Each objItem in colItems WScript.Echo "Computer Domain : " & objItem.Domain WScript.Echo "Domain Role : " & arrDomainRole( objItem.DomainRole ) WScript.Echo "Computer Name : " & objItem.Name WScript.Echo "Part Of Domain : " & objItem.PartOfDomain WScript.Echo "User Name : " & objItem.UserName WScript.Echo "Workgroup : " & objItem.Workgroup WScript.Echo Next Set colItems = Nothing Set objWMISvc = Nothing ' ========================================================================== ' The next technique requires Windows XP, Server 2003 or Vista, and an NT or ' Active Directory domain. ' It doesn't work on stand-alones or computers in a workgroup. ' Nor does it work in Windows 95, 98, ME, NT 4 or 2000. ' Use custom error handling when using this technique. ' ========================================================================== WScript.Echo "WMI (Win32_NTDomain class):" WScript.Echo "===========================" Set objWMISvc = GetObject("winmgmts:\\.\root\CIMV2") Set colItems = objWMISvc.ExecQuery("SELECT * FROM Win32_NTDomain", "WQL", 48 ) For Each objItem In colItems WScript.Echo "Computer Domain : " & objItem.DomainName If objItem.DSPrimaryDomainControllerFlag Then WScript.Echo "PDC : " & objItem.DomainControllerName Else WScript.Echo "DC : " & objItem.DomainControllerName End If WScript.Echo "DC is AD Server : " & objItem.DSDirectoryServiceFlag WScript.Echo Next Set colItems = Nothing Set objWMISvc = Nothing ' ========================================================================== ' The next technique uses the native WSH Shell object to read the host name ' from the registry. ' This will work in Windows 2000 and later, but I'm not sure about earlier ' Windows versions. ' ========================================================================== WScript.Echo "Registry (WSH Shell):" WScript.Echo "=====================" Set wshShell = CreateObject( "WScript.Shell" ) WScript.Echo "Host Name : " & wshShell.RegRead( "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname" ) Set wshShell = Nothing WScript.Echo ' ========================================================================== ' The next technique will work on any WMI enabled computer, wether ' stand-alone, in a workgroup,' NT domain or AD domain. ' It does require a TCP/IP based network, which is standard for Windows. ' This technique works on Windows 95/98/ME machines, if they have WMI ' enabled. ' ========================================================================== Const HKLM = &H80000002 WScript.Echo "Registry (WMI StdRegProv):" WScript.Echo "==========================" Set objReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv" ) objReg.GetStringValue HKLM, "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "Hostname", strHostname Set objReg = Nothing WScript.Echo "Host Name : " & strHostname WScript.Echo ' ========================================================================== ' The next technique requires System Scripting Runtime by Franz Krainer, ' available at http://www.netal.com/ssr.htm ' Use custom error handling when using this technique, as this is not native ' in Windows. ' ========================================================================== WScript.Echo "System Scripting Runtime:" WScript.Echo "=========================" Set objLM = CreateObject( "SScripting.LMNetwork" ) WScript.Echo "Computer Name : " & objLM.ComputerName Set objLM = Nothing Set objSysInfo = CreateObject( "SScripting.System" ) WScript.Echo "User Name : " & objSysInfo.UserName Set objSysInfo = Nothing Set objIP = CreateObject( "SScripting.IPNetwork" ) WScript.Echo "Computer Domain : " & objIP.Domain WScript.Echo "Host Name : " & objIP.Hostname WScript.Echo "IP address of www.google.com: " & objIP.DNSLookup( "www.google.com" ) WScript.Sleep 1000 Set objIP = Nothing WScript.Echo ' ========================================================================== ' The next technique requires Java Webstart and a TCP/IP based network. ' Use custom error handling when using this technique, as Java is not native ' in Windows. ' ========================================================================== WScript.Echo "Java Webstart:" WScript.Echo "==============" Set objJava = CreateObject( "JavaWebStart.isInstalled" ) WScript.Echo "IP address of www.google.com: " & objJava.dnsResolve( "www.google.com" ) Set objJava = Nothing WScript.Echo On Error Goto 0