Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for iscdwriter.vbs

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

  1. Option Explicit
  2.  
  3. Dim arrCapabilities, arrDrives, arrWriters
  4. Dim blnIsCDWriter
  5. Dim intCapability, intRC, intWriters
  6. Dim colItems, objItem, objWMIService
  7. Dim strArg, strDrive, strComputer, strMsg
  8.  
  9. If WScript.Arguments.Named.Count > 0 Then Syntax "" ' No command line switches
  10.  
  11. intRC       = 0
  12. intWriters  = 0
  13. strComputer = "."
  14.  
  15. Set arrDrives  = CreateObject( "System.Collections.ArrayList" ) ' List of drives to be tested
  16. Set arrWriters = CreateObject( "System.Collections.ArrayList" ) ' List of CD/DVD drives found
  17.  
  18. For Each strArg In WScript.Arguments.Unnamed
  19. 	If Len( strArg ) = 2 Then
  20. 		If Right( strArg, 1 ) = ":" Then
  21. 			If arrDrives.Contains( UCase( strArg ) ) Then
  22. 				Syntax "Duplicate argument """ & strArg & """"
  23. 			Else
  24. 				arrDrives.add UCase( strArg )
  25. 			End If
  26. 		Else
  27. 			If strComputer = "." Then
  28. 				strComputer = strArg
  29. 			Else
  30. 				Syntax "Duplicate computer name arguments """ & strComputer & """ and """ & strArg & """"
  31. 			End If
  32. 		End If
  33. 	Else
  34. 		If strComputer = "." Then
  35. 			strComputer = strArg
  36. 		Else
  37. 			Syntax "Duplicate computer name arguments """ & strComputer & """ and """ & strArg & """"
  38. 		End If
  39. 	End If
  40. Next
  41.  
  42. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  43. Set colItems      = objWMIService.ExecQuery( "SELECT * FROM Win32_CDROMDrive" )
  44.  
  45. If arrDrives.Count = 0 Then
  46. 	For Each objItem in colItems
  47. 		arrDrives.Add objItem.Drive
  48. 	Next
  49. End If
  50.  
  51. For Each objItem in colItems
  52. 	If arrDrives.Contains( UCase( objItem.Drive ) ) Then
  53. 		For Each intCapability In objItem.Capabilities
  54. 			If intCapability = 4 Then arrWriters.Add objItem.Drive
  55. 		Next
  56. 	End If
  57. Next
  58.  
  59. Set colItems      = Nothing
  60. Set objWMIService = Nothing
  61.  
  62. strMsg = arrDrives.Count & " drive"
  63. If arrDrives.count <> 1 Then strMsg = strMsg & "s"
  64. strMsg = strMsg & " tested, " & arrWriters.Count & " CD/DVD writer"
  65. If arrWriters.Count <> 1 Then strMsg = strMsg & "s"
  66. strMsg = strMsg & " found:" & vbCrLf
  67.  
  68. For Each strDrive In arrDrives
  69. 	If arrWriters.Contains( strDrive ) Then
  70. 		strMsg = strMsg & vbTab & strDrive & vbTab & "CD/DVD writer" & vbCrLf
  71. 	Else
  72. 		strMsg = strMsg & vbTab & strDrive & vbTab & "is not a writer" & vbCrLf
  73. 	End If
  74. Next
  75.  
  76. WScript.Echo strMsg
  77.  
  78. intRC = arrWriters.Count
  79.  
  80. Set arrWriters = Nothing
  81. Set arrDrives  = Nothing
  82.  
  83. WScript.Quit intRC
  84.  
  85.  
  86. Sub Syntax( strError )
  87. 	Dim strMsg
  88. 	If strError <> "" Then strMsg = vbCrLf & "ERROR: " & strError & vbCrLf
  89. 	strMsg = strMsg _
  90. 	       & vbCrLf _
  91. 	       & "IsCDWriter.vbs,  Version 1.00" _
  92. 	       & vbCrLf _
  93. 	       & "Check whether specified drives are CD/DVD writers or not" _
  94. 	       & vbCrLf & vbCrLf _
  95. 	       & "Usage:" & vbTab & "CSCRIPT.EXE  IsCDWriter.vbs  [ computer ]  [ drive:  [ drive:   ... ] ]" _
  96. 	       & vbCrLf & vbCrLf _
  97. 	       & "Where:" & vbTab & "computer" & vbTab & "is the optional remote computer's name or IP address" _
  98. 	       & vbCrLf _
  99. 	       & "      " & vbTab & "        " & vbTab & "(default: the local computer)" _
  100. 	       & vbCrLf _
  101. 	       & "      " & vbTab & "drive:  " & vbTab & "is/are the CD/DVD drive(s) to be tested" _
  102. 	       & vbCrLf _
  103. 	       & "      " & vbTab & "        " & vbTab & "(default: all available CD/DVD drives)" _
  104. 	       & vbCrLf & vbCrLf _
  105. 	       & "Note:   The return code equals the number of CD/DVD writers, or -1 for errors." _
  106. 	       & vbCrLf & vbCrLf _
  107. 	       & "Written by Rob van der Woude" _
  108. 	       & vbCrLf _
  109. 	       & "http://www.robvanderwoude.com"
  110. 	WScript.Echo strMsg
  111. 	WScript.Quit -1
  112. End Sub
  113.  

page last uploaded: 2017-08-21, 14:26