我正在尝试检查办公室某些远程服务器上的硬盘空间。下面的文件调试正常,但文本文件中没有输出。我已经尝试过PS和其他VB代码,它们似乎可以工作,但我需要或者至少希望将数据放入文本文件中以便保存。
思想?
arrServers = Array("server.domain.net", "server2.domain.net", "server3.domain.net")
strFilePath = "freespace.txt"
On Error Resume Next
Set objFso = CreateObject("Scripting.FileSystemObject")
Set oFile = objFso.OpenTextFile(strFilePath, 2, vbTrue)
If Not IsNothing(oFile) Then
For Each strComputer In arrServers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If objWMIService Then
Set colDiskDrives = objWMIService.ExecQuery _
("Select * from Win32_PerfFormattedData_PerfDisk_LogicalDisk Where " _
& "Name <> '_Total'")
For Each objDiskDrive In colDiskDrives
oFile.WriteLine "Drive", objDiskDrive.Name, "on", strComputer, "has", _
objDiskDrive.FreeMegabytes & "MB (" & objDiskDrive.PercentFreeSpace & "%) Free"
Next
Else
oFile.WriteLine "Could not connect to " & strComputer
End If
Next
Else
WScript.Echo "Could not open text file."
End If
答案 0 :(得分:1)
删除on error error next并运行它。你看到了什么?
我认为这可能是你的问题:
If Not IsNothing(oFile) Then
应该是
If Not oFile Is Nothing Then
答案 1 :(得分:0)
虽然在Tim的回答中,EVIL Global OERN被正确识别,但是“应该”检查“oFile Is Nothing”是否具有误导性。
此代码
Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )
Dim sBadFSpec : sBadFSpec = ".\nix\nix.txt"
Dim tsOut, bIsNothing
On Error Resume Next
Set tsOut = goFS.OpenTextFile(sBadFSpec, ForWriting, True)
If 0 <> Err.Number Then WScript.Echo "Bingo!", Err.Description
On Error GoTo 0
WScript.Echo "tsOut:", VarType(tsOut), TypeName(tsOut)
On Error Resume Next
bIsNothing = tsOut Is Nothing
If 0 <> Err.Number Then WScript.Echo "Bingo!", Err.Description
On Error GoTo 0
及其输出:
Bingo! Path not found
tsOut: 0 Empty
Bingo! Object required
所示:
<强>更新强>
Tim的评论让我意识到上面仅适用于VBScript(这里未初始化的变量是子类型为Empty的普通变量)。对VBA一无所知,我相信Tim认为oFile Is Nothing的测试是该语言的有效策略。