Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for vbsnames.vbs

(view source code of vbsnames.vbs as plain text)

  1. ' ==========================================================================
  2. ' VBSNames.vbs, Version 3.00
  3. '
  4. ' This script demonstrates several techniques to retrieve user names,
  5. ' computer names, domain names and some related properties.
  6. '
  7. ' Just run it to see which techniques work in your environment and which
  8. ' ones don't. This script won't change anything on your computer, it only
  9. ' reads some properties.
  10. '
  11. ' Written by Rob van der Woude
  12. ' http://www.robvanderwoude.com
  13. ' ==========================================================================
  14.  
  15. Option Explicit
  16.  
  17. Dim arrDomainRole( 5 ), colItems, objIP, objItem, objJava, objLM
  18. Dim objReg, objSysInfo, objWMISvc, strHostname, wshNetwork, wshShell
  19.  
  20. On Error Resume Next
  21.  
  22. ' ==========================================================================
  23. ' Except for "Logon Server" and "User Domain", the next technique will work
  24. ' on any computer running Windows NT 4, 2000, XP, Server 2003 or Vista,
  25. ' wether stand-alone, in a workgroup, NT or AD domain.
  26. ' "Logon Server" and "User Domain" will be empty on stand-alone machines.
  27. ' This technique doesn't work on Windows 95/98/ME machines.
  28. ' ==========================================================================
  29.  
  30. Set wshShell = Wscript.CreateObject( "Wscript.Shell" )
  31.  
  32. WScript.Echo "Windows Environment Variables:"
  33. WScript.Echo "=============================="
  34.  
  35. WScript.Echo "Computer Name   : " & wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
  36. WScript.Echo "Logon Server    : " & wshShell.ExpandEnvironmentStrings( "%LOGONSERVER%" )
  37. WScript.Echo "User Domain     : " & wshShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
  38. WScript.Echo "User Name       : " & wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
  39. WScript.Echo
  40.  
  41. Set wshShell = Nothing
  42.  
  43. ' ==========================================================================
  44. ' This technique should work on any Windows computer with WSH installed
  45. ' (wich excludes 95), wether stand-alone, in a workgroup, NT or AD domain.
  46. ' Requires WSH, so it won't work in HTAs.
  47. ' ==========================================================================
  48.  
  49. Set wshNetwork = Wscript.CreateObject( "Wscript.Network" )
  50.  
  51. WScript.Echo "WSHNetwork:"
  52. WScript.Echo "==========="
  53.  
  54. WScript.Echo "Computer Name   : " & wshNetwork.ComputerName
  55. WScript.Echo "User Domain     : " & wshNetwork.UserDomain
  56. WScript.Echo "User Name       : " & wshNetwork.UserName
  57. WScript.Echo
  58.  
  59. Set wshShell = Nothing
  60.  
  61. ' ==========================================================================
  62. ' Except for "PDC" and "User Domain", the following next will work on any
  63. ' computer running NT 4 or later, wether in a workgroup, NT or AD domain.
  64. ' "PDC" will be empty on stand-alones or workgroup members.
  65. ' "User Domain" will be empty on stand-alones.
  66. ' This technique works on Windows 95/98/ME machines too, if AD client
  67. ' software is installed.
  68. ' ==========================================================================
  69.  
  70. Set objSysInfo = CreateObject( "WinNTSystemInfo" )
  71.  
  72. WScript.Echo "ADSI (WinNTSystemInfo):"
  73. WScript.Echo "======================="
  74.  
  75. WScript.Echo "Computer Name   : " & objSysInfo.ComputerName
  76. WScript.Echo "PDC             : " & objSysInfo.PDC
  77. WScript.Echo "User Domain     : " & objSysInfo.DomainName
  78. WScript.Echo "User Name       : " & objSysInfo.UserName
  79. WScript.Echo
  80.  
  81. Set objSysInfo = Nothing
  82.  
  83. ' ==========================================================================
  84. ' The next technique requires Active Directory.
  85. ' It doesn't work on stand-alones or computers in a workgroup or NT domain.
  86. ' This technique works on Windows 95/98/ME machines too, if AD client
  87. ' software is installed.
  88. ' Use custom error handling when using this technique.
  89. ' ==========================================================================
  90.  
  91. Set objSysInfo = CreateObject( "ADSystemInfo" )
  92.  
  93. WScript.Echo "ADSI (ADSystemInfo):"
  94. WScript.Echo "===================="
  95.  
  96. WScript.Echo "Computer Name   : " & objSysInfo.ComputerName
  97. WScript.Echo "User Domain     : " & objSysInfo.DomainName
  98. WScript.Echo "User Name       : " & objSysInfo.UserName
  99. WScript.Echo
  100.  
  101. Set objSysInfo = Nothing
  102.  
  103. ' ==========================================================================
  104. ' The next technique will work on any WMI enabled computer, wether
  105. ' stand-alone, in a workgroup,' NT domain or AD domain.
  106. ' Domain related properties will be empty on stand-alone machines or
  107. ' workgroup menmbers.
  108. ' "Workgroup" is unreliable, as it often doesn't even work on workgroup
  109. ' members running Windows XP Professional SP2.
  110. ' This technique works on Windows 95/98/ME machines, if they have WMI
  111. ' enabled.
  112. ' ==========================================================================
  113.  
  114. Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
  115. Set colItems  = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
  116.  
  117. WScript.Echo "WMI (Win32_ComputerSystem class):"
  118. WScript.Echo "================================="
  119.  
  120. arrDomainRole( 0 ) = "Standalone Workstation"
  121. arrDomainRole( 1 ) = "Member Workstation"
  122. arrDomainRole( 2 ) = "Standalone Server"
  123. arrDomainRole( 3 ) = "Member Server"
  124. arrDomainRole( 4 ) = "Backup Domain Controller"
  125. arrDomainRole( 5 ) = "Primary Domain Controller"
  126.  
  127. For Each objItem in colItems
  128. 	WScript.Echo "Computer Domain : " & objItem.Domain
  129. 	WScript.Echo "Domain Role     : " & arrDomainRole( objItem.DomainRole )
  130. 	WScript.Echo "Computer Name   : " & objItem.Name
  131. 	WScript.Echo "Part Of Domain  : " & objItem.PartOfDomain
  132. 	WScript.Echo "User Name       : " & objItem.UserName
  133. 	WScript.Echo "Workgroup       : " & objItem.Workgroup
  134. 	WScript.Echo
  135. Next
  136.  
  137. Set colItems  = Nothing
  138. Set objWMISvc = Nothing
  139.  
  140. ' ==========================================================================
  141. ' The next technique requires Windows XP, Server 2003 or Vista, and an NT or
  142. ' Active Directory domain.
  143. ' It doesn't work on stand-alones or computers in a workgroup.
  144. ' Nor does it work in Windows 95, 98, ME, NT 4 or 2000.
  145. ' Use custom error handling when using this technique.
  146. ' ==========================================================================
  147.  
  148. WScript.Echo "WMI (Win32_NTDomain class):"
  149. WScript.Echo "==========================="
  150.  
  151. Set objWMISvc = GetObject("winmgmts:\\.\root\CIMV2")
  152. Set colItems = objWMISvc.ExecQuery("SELECT * FROM Win32_NTDomain", "WQL", 48 )
  153.  
  154. For Each objItem In colItems
  155. 	WScript.Echo "Computer Domain : " & objItem.DomainName
  156. 	If objItem.DSPrimaryDomainControllerFlag Then
  157. 		WScript.Echo "PDC             : " & objItem.DomainControllerName
  158. 	Else
  159. 		WScript.Echo "DC              : " & objItem.DomainControllerName
  160. 	End If
  161. 	WScript.Echo "DC is AD Server : " & objItem.DSDirectoryServiceFlag
  162. 	WScript.Echo
  163. Next
  164.  
  165. Set colItems  = Nothing
  166. Set objWMISvc = Nothing
  167.  
  168. ' ==========================================================================
  169. ' The next technique uses the native WSH Shell object to read the host name
  170. ' from the registry.
  171. ' This will work in Windows 2000 and later, but I'm not sure about earlier
  172. ' Windows versions.
  173. ' ==========================================================================
  174.  
  175. WScript.Echo "Registry (WSH Shell):"
  176. WScript.Echo "====================="
  177.  
  178. Set wshShell = CreateObject( "WScript.Shell" )
  179. WScript.Echo "Host Name       : " & wshShell.RegRead( "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname" )
  180. Set wshShell = Nothing
  181.  
  182. WScript.Echo
  183.  
  184. ' ==========================================================================
  185. ' The next technique will work on any WMI enabled computer, wether
  186. ' stand-alone, in a workgroup,' NT domain or AD domain.
  187. ' It does require a TCP/IP based network, which is standard for Windows.
  188. ' This technique works on Windows 95/98/ME machines, if they have WMI
  189. ' enabled.
  190. ' ==========================================================================
  191.  
  192. Const HKLM = &H80000002
  193.  
  194. WScript.Echo "Registry (WMI StdRegProv):"
  195. WScript.Echo "=========================="
  196.  
  197. Set objReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv" )
  198. objReg.GetStringValue HKLM, "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "Hostname", strHostname
  199. Set objReg = Nothing
  200.  
  201. WScript.Echo "Host Name       : " & strHostname
  202. WScript.Echo
  203.  
  204. ' ==========================================================================
  205. ' The next technique requires System Scripting Runtime by Franz Krainer,
  206. ' available at http://www.netal.com/ssr.htm
  207. ' Use custom error handling when using this technique, as this is not native
  208. ' in Windows.
  209. ' ==========================================================================
  210.  
  211. WScript.Echo "System Scripting Runtime:"
  212. WScript.Echo "========================="
  213.  
  214. Set objLM  = CreateObject( "SScripting.LMNetwork" )
  215. WScript.Echo "Computer Name   : " & objLM.ComputerName
  216. Set objLM  = Nothing
  217.  
  218. Set objSysInfo = CreateObject( "SScripting.System" )
  219. WScript.Echo "User Name       : " & objSysInfo.UserName
  220. Set objSysInfo = Nothing
  221.  
  222. Set objIP = CreateObject( "SScripting.IPNetwork" )
  223. WScript.Echo "Computer Domain : " & objIP.Domain
  224. WScript.Echo "Host Name       : " & objIP.Hostname
  225. WScript.Echo "IP address of www.google.com: " & objIP.DNSLookup( "www.google.com" )
  226. WScript.Sleep 1000
  227. Set objIP = Nothing
  228.  
  229. WScript.Echo
  230.  
  231. ' ==========================================================================
  232. ' The next technique requires Java Webstart and a TCP/IP based network.
  233. ' Use custom error handling when using this technique, as Java is not native
  234. ' in Windows.
  235. ' ==========================================================================
  236.  
  237. WScript.Echo "Java Webstart:"
  238. WScript.Echo "=============="
  239.  
  240. Set objJava = CreateObject( "JavaWebStart.isInstalled" )
  241. WScript.Echo "IP address of www.google.com: " & objJava.dnsResolve( "www.google.com" )
  242. Set objJava = Nothing
  243.  
  244. WScript.Echo
  245.  
  246. On Error Goto 0
  247.  

page last modified: 2024-02-26; loaded in 0.0281 seconds