gethostids.vbs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. GenerateHostsFile()
  2. '------------------------------------------------------------
  3. ' Generate the myhostids.txt file.
  4. '------------------------------------------------------------
  5. Function GenerateHostsFile()
  6. ScriptPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
  7. RlmUtilExe = """" & ScriptPath & "rlmutil.exe"""
  8. 'Get info needed to create host file.
  9. DiskID = MakeSafeXmlText(Trim(RunExe(RlmUtilExe & " rlmhostid -q -32")))
  10. Ether = MakeSafeXmlText(Trim(RunExe(RlmUtilExe & " rlmhostid -q ether")))
  11. User = MakeSafeXmlText(RemoveJunk(RunExe(RlmUtilExe & " rlmhostid -q user")))
  12. Host = MakeSafeXmlText(RemoveJunk(RunExe(RlmUtilExe & " rlmhostid -q host")))
  13. 'Make queries for system information.
  14. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  15. Set colProcessorInfo = objWMIService.ExecQuery("Select * from Win32_Processor")
  16. Set colComputerInfo = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
  17. Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
  18. Set colVideoController = objWMIService.ExecQuery("Select * from Win32_VideoController",,48)
  19. Set colDisplayConfig = objWMIService.ExecQuery("Select * from Win32_DisplayConfiguration")
  20. Set colDesktopMonitor = objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")
  21. Set colControllerConfig = objWMIService.ExecQuery("Select * from Win32_DisplayControllerConfiguration")
  22. 'Echo the myhostids.txt file.
  23. Wscript.echo "<hostid>"
  24. Wscript.echo " <disk>" & DiskID & "</disk>"
  25. Wscript.echo " <ether>" & Ether & "</ether>"
  26. Wscript.echo " <user>" & User & "</user>"
  27. Wscript.echo " <host>" & Host & "</host>"
  28. Wscript.echo " <platform>" & GetOSName(colOSes) & " " & GetSpNumber(colOSes) & "</platform>"
  29. Wscript.echo " <langCode>" & GetLanguage() & "</langCode>"
  30. Wscript.echo " <VideoMemory>" & MakeSafeXmlText(GetVideoCardMemory(colVideoController)) & "</VideoMemory>"
  31. Wscript.echo " <DeviceName>" & MakeSafeXmlText(GetVideoCardDeviceName(colDisplayConfig)) & "</DeviceName>"
  32. Wscript.echo " <DriverVersion>" & MakeSafeXmlText(GetVideoCardDriverVersion(colDisplayConfig)) & "</DriverVersion>"
  33. Wscript.echo " <Resolution>" & MakeSafeXmlText(GetDisplayResolution(colDesktopMonitor)) & "</Resolution>"
  34. Wscript.echo " <BitsPerPixel>" & MakeSafeXmlText(GetDisplayBitsPerPixel(colControllerConfig)) & "</BitsPerPixel>"
  35. Wscript.echo " <ProcessorName>" & MakeSafeXmlText(GetProcessorName(colProcessorInfo)) & "</ProcessorName>"
  36. Wscript.echo " <PhysicalProcessors>" & MakeSafeXmlText(GetPhysicalProcessors(colComputerInfo)) & "</PhysicalProcessors>"
  37. Wscript.echo " <Cores>" & MakeSafeXmlText(GetProcessorCores(colProcessorInfo)) & "</Cores>"
  38. Wscript.echo " <LogicalProcessors>" & MakeSafeXmlText(GetLogicalProcessors(colProcessorInfo)) & "</LogicalProcessors>"
  39. Wscript.echo " <ClockSpeed>" & MakeSafeXmlText(GetProcessorClockSpeed(colProcessorInfo)) & "</ClockSpeed>"
  40. Wscript.echo " <Architecture>" & MakeSafeXmlText(GetProcessorArchitecture(colProcessorInfo)) & "</Architecture>"
  41. Wscript.echo " <BusSpeed>" & MakeSafeXmlText(GetProcessorBusSpeed(colProcessorInfo)) & "</BusSpeed>"
  42. Wscript.echo " <Manufacturer>" & MakeSafeXmlText(GetProcessorManufacturer(colProcessorInfo)) & "</Manufacturer>"
  43. Wscript.echo "</hostid>"
  44. End Function
  45. '------------------------------------------------------------
  46. ' Get the operating system's name.
  47. '------------------------------------------------------------
  48. Function GetOSName(colOSes)
  49. For Each objOS in colOSes
  50. 'Windows 2000
  51. If (InStr(ObjOS.Caption, "2000") <> 0) Then
  52. StrOsVer = "Win2K"
  53. 'Windows XP
  54. ElseIf ( InStr(objOS.Caption, "XP") <> 0) Then
  55. If ( InStr(objOS.Caption, "x64") <> 0) Then
  56. StrOsVer = "WinXP_x64"
  57. Else
  58. StrOsVer = "WinXP_x86"
  59. End If
  60. Else
  61. 'Vista
  62. If (InStr(ObjOS.Caption, "Vista") <> 0) Then
  63. StrOsVer = "Vista"
  64. 'Windows 7
  65. ElseIf (InStr(ObjOS.Caption, "Windows 7") <> 0) Then
  66. StrOsVer = "Win7"
  67. 'Windows 8.1 (This must be before 8)
  68. ElseIf (InStr(ObjOS.Caption, "Windows 8.1") <> 0) Then
  69. StrOsVer = "Win8.1"
  70. 'Windows 8
  71. ElseIf (InStr(ObjOS.Caption, "Windows 8") <> 0) Then
  72. StrOsVer = "Win8"
  73. End If
  74. If (IsEmpty(StrOsVer)) Then
  75. 'Unknown OS.
  76. StrOsVer = ObjOS.Caption & ", " & objOS.OSArchitecture
  77. Else
  78. 'Append the architecture.
  79. If ( InStr(objOS.OSArchitecture, "64") <> 0) Then
  80. StrOsVer = StrOsVer & "_x64"
  81. Else
  82. StrOsVer = StrOsVer & "_x86"
  83. End If
  84. End If
  85. End If
  86. Next
  87. GetOSName = StrOsVer
  88. End Function
  89. '------------------------------------------------------------
  90. ' Get the OS Service Pack Number.
  91. '------------------------------------------------------------
  92. Function GetSpNumber(colOSes)
  93. For Each objOperatingSystem in colOSes
  94. major = objOperatingSystem.ServicePackMajorVersion
  95. minor = objOperatingSystem.ServicePackMinorVersion
  96. Next
  97. If (Major=0) Then
  98. strSPnum = "RTM"
  99. Else
  100. strSPnum = "SP" & major
  101. End If
  102. GetSpNumber = strSPnum
  103. End Function
  104. '------------------------------------------------------------
  105. ' Get the OS language.
  106. '------------------------------------------------------------
  107. Function GetLanguage()
  108. set osvc = GetObject("winmgmts:")
  109. For each objOS in osvc.instancesof("win32_operatingsystem")
  110. LangCode = objOS.OSLanguage
  111. Next
  112. Select Case LangCode
  113. Case 1025
  114. strCountry= "AR" 'Arabic
  115. Case 1028
  116. strCountry= "ZHTW" 'Chinese
  117. Case 2052
  118. strCountry= "ZHCN" 'Chinese (Simplified)
  119. Case 3076
  120. strCountry= "ZHHK" 'Chinese (Hong Kong)
  121. Case 1029
  122. strCountry= "CS" 'CZech
  123. Case 1030
  124. strCountry= "DA" 'Danish
  125. Case 1031
  126. strCountry= "DE" 'German
  127. Case 1032
  128. strCountry= "EL" 'Greek
  129. Case 1033
  130. strCountry= "EN" 'English (US)
  131. Case 1034
  132. strCountry= "ES" 'Spanish
  133. Case 1035
  134. strCountry= "FI" 'Finnish
  135. Case 1036
  136. strCountry= "FR" 'French
  137. Case 1037
  138. strCountry= "HE" 'Hebrew
  139. Case 1039
  140. strCountry= "HU" 'Hungarian
  141. Case 1040
  142. strCountry= "IT" 'Italian
  143. Case 1041
  144. strCountry= "JA" 'Japanese
  145. Case 1042
  146. strCountry= "KO" 'Korean
  147. Case 1043
  148. strCountry= "NL" 'Dutch
  149. Case 1044
  150. strCountry= "NO" 'Norwegian
  151. Case 1045
  152. strCountry= "PL" 'Polish
  153. Case 1046
  154. Case 1070
  155. strCountry= "PT" 'Portuguese
  156. Case 1049
  157. strCountry= "RU" 'Russian
  158. Case 1053
  159. strCountry= "SV" 'Swedish
  160. Case 1054
  161. strCountry= "TH" 'Thai
  162. Case 1055
  163. strCountry= "TR" 'Turkish
  164. Case Else
  165. strCountry = "UnknownLanguage"
  166. End Select
  167. GetLanguage = strCountry
  168. End Function
  169. '------------------------------------------------------------
  170. ' Get video card information.
  171. '------------------------------------------------------------
  172. Function GetVideoCardMemory(colVideoController)
  173. If CollectionHasItems(colVideoController) Then
  174. For Each objItem in colVideoController
  175. VideoCardInfo = FormatNumber(RetrieveProperty("objItem.AdapterRAM")/1024\1024, 0) & " MB"
  176. Next
  177. End If
  178. GetVideoCardMemory = SetToUnknownIfEmpty(VideoCardInfo)
  179. End Function
  180. Function GetVideoCardDeviceName(colDisplayConfig)
  181. If CollectionHasItems(colDisplayConfig) Then
  182. For Each objItem in colDisplayConfig
  183. VideoCardInfo = RetrieveProperty("objItem.DeviceName")
  184. Next
  185. End If
  186. GetVideoCardDeviceName = SetToUnknownIfEmpty(VideoCardInfo)
  187. End Function
  188. Function GetVideoCardDriverVersion(colDisplayConfig)
  189. If CollectionHasItems(colDisplayConfig) Then
  190. For Each objItem in colDisplayConfig
  191. VideoCardInfo = RetrieveProperty("objItem.DriverVersion")
  192. Next
  193. End If
  194. GetVideoCardDriverVersion = SetToUnknownIfEmpty(VideoCardInfo)
  195. End Function
  196. Function GetDisplayResolution(colDesktopMonitor)
  197. If CollectionHasItems(colDesktopMonitor) Then
  198. For Each objItem in colDesktopMonitor
  199. If (Not IsEmpty(objItem.ScreenWidth) And Not IsEmpty(objItem.ScreenHeight)) Then
  200. VideoCardInfo = RetrieveProperty("objItem.ScreenWidth") & "x" & RetrieveProperty("objItem.ScreenHeight")
  201. End If
  202. Next
  203. End If
  204. GetDisplayResolution = SetToUnknownIfEmpty(VideoCardInfo)
  205. End Function
  206. Function GetDisplayBitsPerPixel(colControllerConfig)
  207. If CollectionHasItems(colControllerConfig) Then
  208. For Each objItem in colControllerConfig
  209. VideoCardInfo = RetrieveProperty("objItem.BitsPerPixel")
  210. Next
  211. End If
  212. GetDisplayBitsPerPixel = SetToUnknownIfEmpty(VideoCardInfo)
  213. End Function
  214. '------------------------------------------------------------
  215. ' Get processor information.
  216. '------------------------------------------------------------
  217. Function GetProcessorName(colProcessorInfo)
  218. If (CollectionHasItems(colProcessorInfo)) Then
  219. For Each objItem in colProcessorInfo
  220. ProcName = replace(objItem.Name, " ", " ")
  221. ProcName = replace(ProcName, "(TM)","")
  222. ProcName = replace(ProcName, "(R)","")
  223. ProcessorInfo = ProcName
  224. Next
  225. End If
  226. GetProcessorName = SetToUnknownIfEmpty(ProcessorInfo)
  227. End Function
  228. Function GetPhysicalProcessors(colComputerInfo)
  229. For Each objItem in colComputerInfo
  230. ProcessorInfo = RetrieveProperty("objItem.NumberOfProcessors")
  231. Next
  232. GetPhysicalProcessors = SetToUnknownIfEmpty(ProcessorInfo)
  233. End Function
  234. Function GetProcessorCores(colProcessorInfo)
  235. If (CollectionHasItems(colProcessorInfo)) Then
  236. For Each objItem in colProcessorInfo
  237. ProcessorInfo = RetrieveProperty("objItem.NumberOfCores")
  238. Next
  239. End If
  240. GetProcessorCores = SetToUnknownIfEmpty(ProcessorInfo)
  241. End Function
  242. Function GetLogicalProcessors(colProcessorInfo)
  243. If (CollectionHasItems(colProcessorInfo)) Then
  244. For Each objItem in colProcessorInfo
  245. ProcessorInfo = RetrieveProperty("objItem.NumberOfLogicalProcessors")
  246. Next
  247. End If
  248. GetLogicalProcessors = SetToUnknownIfEmpty(ProcessorInfo)
  249. End Function
  250. Function GetProcessorClockSpeed(colProcessorInfo)
  251. If (CollectionHasItems(colProcessorInfo)) Then
  252. For Each objItem in colProcessorInfo
  253. ProcessorInfo = RetrieveProperty("objItem.MaxClockSpeed")
  254. Next
  255. End If
  256. GetProcessorClockSpeed = SetToUnknownIfEmpty(ProcessorInfo)
  257. End Function
  258. Function GetProcessorArchitecture(colProcessorInfo)
  259. If (CollectionHasItems(colProcessorInfo)) Then
  260. For Each objItem in colProcessorInfo
  261. ProcessorInfo = RetrieveProperty("objItem.AddressWidth")
  262. Next
  263. End If
  264. GetProcessorArchitecture = SetToUnknownIfEmpty(ProcessorInfo)
  265. End Function
  266. Function GetProcessorBusSpeed(colProcessorInfo)
  267. If (CollectionHasItems(colProcessorInfo)) Then
  268. For Each objItem in colProcessorInfo
  269. ProcessorInfo = RetrieveProperty("objItem.ExtClock")
  270. Next
  271. End If
  272. GetProcessorBusSpeed = SetToUnknownIfEmpty(ProcessorInfo)
  273. End Function
  274. Function GetProcessorManufacturer(colProcessorInfo)
  275. If (CollectionHasItems(colProcessorInfo)) Then
  276. For Each objItem in colProcessorInfo
  277. ProcessorInfo = RetrieveProperty("objItem.Manufacturer")
  278. Next
  279. End If
  280. GetProcessorManufacturer = SetToUnknownIfEmpty(ProcessorInfo)
  281. End Function
  282. '------------------------------------------------------------
  283. ' Make sure a collection has accessible items in it.
  284. '------------------------------------------------------------
  285. Function CollectionHasItems(SomeCollection)
  286. On Error Resume Next
  287. CollectionHasItems = SomeCollection.Count
  288. If (Err.Number <> 0) Then
  289. CollectionHasItems = False
  290. Else
  291. CollectionHasItems = True
  292. End If
  293. On Error Goto 0
  294. End Function
  295. '------------------------------------------------------------
  296. ' Retrieve a property of an object or return an Empty value.
  297. '------------------------------------------------------------
  298. Function RetrieveProperty(ObjectProperty)
  299. On Error Resume Next
  300. RetrieveProperty = Eval(ObjectProperty)
  301. On Error Goto 0
  302. End Function
  303. '------------------------------------------------------------
  304. ' Strip off any garbage that rlmutil may output.
  305. ' EG:
  306. ' rlmutil.exe -rlmhostid -q internet
  307. ' outputs 'ip=xxx.xxx.xxx.xxx'. This function will
  308. ' strip the garbage so it returns the ip address part.
  309. '------------------------------------------------------------
  310. Function RemoveJunk(SomeString)
  311. TokenArray = Split(Trim(SomeString), " ")
  312. For ElemNum = 0 to UBound(TokenArray)
  313. StrLen = Len(TokenArray(ElemNum))
  314. EqualLoc = InStr(TokenArray(ElemNum), "=")
  315. TokenArray(ElemNum) = Trim(Right(TokenArray(ElemNum), StrLen - EqualLoc))
  316. Next
  317. RemoveJunk = Join(TokenArray)
  318. End Function
  319. '------------------------------------------------------------
  320. ' If the variable is empty, set it to "Unknown"
  321. '------------------------------------------------------------
  322. Function SetToUnknownIfEmpty(Data)
  323. If (IsEmpty(Data)) Then
  324. Data = "Unknown"
  325. End If
  326. SetToUnknownIfEmpty = Data
  327. End Function
  328. '------------------------------------------------------------
  329. ' The following characters aren't allowed
  330. ' in XML data:
  331. ' < > " ' &
  332. '
  333. ' This method determines if one of these invalid characters
  334. ' is contained in the value passed in and if so it wraps
  335. ' the string in a CDATA node to make the string valid XML data.
  336. '------------------------------------------------------------
  337. Function MakeSafeXmlText(SomeString)
  338. ContainsInvalidChars = false
  339. If (InStr(SomeString, "<") > 0) Then
  340. ContainsInvalidChars = true
  341. End If
  342. If (InStr(SomeString, """") > 0) Then
  343. ContainsInvalidChars = true
  344. End If
  345. If (InStr(SomeString, "'") > 0) Then
  346. ContainsInvalidChars = true
  347. End If
  348. If (InStr(SomeString, "&") > 0) Then
  349. ContainsInvalidChars = true
  350. End If
  351. If (ContainsInvalidChars = true) Then
  352. 'It is pretty unlikely that the string will already
  353. 'contain a CDATA node but remove it if it does.
  354. If (InStr(SomeString, "<![CDATA[")) Then
  355. SomeString = Replace(SomeString, "<![CDATA[", "")
  356. End If
  357. If (InStr(SomeString, "]]>")) Then
  358. SomeString = Replace(SomeString, "]]>", "")
  359. End If
  360. result = "<![CDATA[" & SomeString & "]]>"
  361. Else
  362. result = SomeString
  363. End If
  364. MakeSafeXmlText = result
  365. End Function
  366. '------------------------------------------------------------
  367. ' Execute a file and return the output from standard out.
  368. '------------------------------------------------------------
  369. Function RunExe(ExeFilePlusArgs)
  370. Set WshShell = CreateObject("WScript.Shell")
  371. Set oExec = WshShell.Exec(ExeFilePlusArgs)
  372. Do While (oExec.Status = 0)
  373. 'Do Nothing here
  374. Loop
  375. RunExe = oExec.StdOut.ReadLine()
  376. End Function