基本VBS帮助 - 优化VBS脚本

时间:2012-08-09 21:25:02

标签: vbscript wmi

我找到了这个输出逻辑磁盘大小的简单脚本。

strComputer = "."

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

Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk")

For Each objDisk in colDisks
    Wscript.Echo "DeviceID: " & objDisk.DeviceID & " with a Disk Size: " & objDisk.Size

Next

我的VBS技能很差,我需要帮助:

  1. 我想获得单个数量的只有C和D分区加在一起
  2. 如果尺寸(来自步骤1)不等于500-GB(450,000,000,000和550,000,000,000之间),我需要电脑提示警告并“按任意键”继续
  3. 我不想要一个弹出窗口,因为这将从WinPE的提示符开始运行,是否可以在提示窗口中获取输出?
  4. 我问了很多,所以如果你能提供帮助,请提前感谢你

1 个答案:

答案 0 :(得分:1)

您需要使用cscript启动脚本。 代码来自http://ask.metafilter.com/79481/vbscript-printing-to-command-line 这允许回声转到命令行而不是消息框。

CheckStartMode
strComputer = "."

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

Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")

For Each objDisk in colDisks
    If(objDisk.DeviceID="C:" or objDisk.DeviceID="D:") then
        Wscript.Echo "DeviceID: " & objDisk.DeviceID & " with a Disk Size: " & objDisk.Size
        TotalSize = CCur(TotalSize) + CCur(objDisk.Size)
    End if
Next
If(TotalSize <450000000000 or TotalSize >550000000000) then
    Wscript.Echo "Disk size of " & TotalSize & " is out of range."
    Wscript.Echo "Press enter to contine."
    z = WScript.StdIn.Read(1)
End if

Wscript.Echo "Complete, Press enter to end."
z = WScript.StdIn.Read(1)
Sub CheckStartMode
     ' Returns the running executable as upper case from the last \ symbol
     strStartExe    = UCase( Mid( wscript.fullname, instrRev(wscript.fullname, "\") + 1 ) )

     If Not strStartExe = "CSCRIPT.EXE" Then
          ' This wasn't launched with cscript.exe, so relaunch using cscript.exe explicitly!
          ' wscript.scriptfullname is the full path to the actual script

          set           oSh = CreateObject("wscript.shell")
          oSh.Run   "cscript.exe """ & wscript.scriptfullname & """"
          wscript.quit

     End If
End Sub