VBS script to query server storage

Soldato
Joined
9 Oct 2006
Posts
3,761
Location
here
We use a VBS script at work to query various servers and report back the capacities and names of the attached drives. So far the script works fine when the admin account of all servers remains the same, and this is obviously something we cannot allow. So far I have this:

Function TryLocalAdminConnect
On Error Resume Next
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer (Computer, "root\cimv2" ,"Admnistrator", "password")
If err Then
ofile.writeline Computer & ",CONNECTION ERROR : Host not found or Access Denied"
err = 0
Else

As specified this uses one set of log on details for the local admin account, but how would I specify a number of different accounts for it to use? For example if account 1 does not work, use account 2 etc etc?

Regards,

Schnippzle
 
Cheers for the tips. We don't run this locally on any servers, they are all queried over the network.

Sorry for my n00bishness but I am really clueless when it comes to VBS: so where would I add the additional accounts? Is there a possibility of using a function to use the same account name but with a different password?
 
Sorry to sound dim, but when I try to run this I get the error Error at line 99 char 6, expected end of statement. Is this the "Next i" line?
 
Alright man,
So far it doesnt seem to want to work. The spreadsheet that was previously generated with a few missing servers is now completely blank bar the headings. Please see attached code:
Code:
' SDSpace.vbs 
' Server Disk Space checker written by ******** 10/04/09
'
' Usage
' Requires input file containing list of computers to be checked, first connect uses logged on user credentials, if fail 
' will try local admin account.
' 
' Change Parameters below to suit
'

localdate = Date()
' wscript.echo localdate


' ************************************************************
' Parameters, Change to Suit.
' ************************************************************

InputFile="C:\Capacity Monitoring\Server list.txt" 
Outputfile="C:\Capacity Monitoring\Freespacelist_"+cstr(Year(now()))+"_"+cstr(Month(now()))+"_"+cstr(day(now()))+".csv" 


' ************************************************************

Const CONVERT_GB = 1073741824
Const HARD_DISK = 3
On Error Resume Next
Set iFSO = CreateObject("Scripting.FilesyStemObject") 
Set oFSO = CreateObject("Scripting.FilesyStemObject") 
Set ofile = ofso.createTextFile(OutputFile, True) 
Set ifile = iFSO.OpenTextFile(inputfile) 


' Open Output file and write headings
ofile.writeline "Computer,Drive,Label,Disk Size (GB),Used (GB),FreeSpace (GB),% Available" 

' Read Input file and process
Do until ifile.AtEndOfLine 
 Computer = ifile.ReadLine 
 Set objWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" & Computer & "\root\cimv2")
  If err Then
    ' Error Encountered using current credentials, try local Admin connect
    err = 0
    TryLocalAdminConnect
  Else
    Set colLogicalDisk = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk WHERE DriveType = " & HARD_DISK & "")

     For Each objLogicalDisk In colLogicalDisk 
      If objLogicalDisk.Size <> "" Then

       WriteOutput

      End if
    Next
  End If
Loop

' **************************************************************************************************************************
'Email out resulting csv to the whole world - well at least TS team.
' **************************************************************************************************************************

ofile.Close
ExecuteShellProgram "C:\Program Files\febooti Command line email\febootimail.exe -SERVER Exchange4 -FROM ******** -TO ********* -SUBJECT Server Disk Space Report for "+cstr(Date())+" -MSG See attached for details of server disk space usage as of today -ENTER -ENTER -MSG System list located at WS010898\C:\Capacity Monitoring\Server list.txt -ATTACH "+Outputfile



Function WriteOutput

Free = FormatNumber(objLogicalDisk.FreeSpace/CONVERT_GB,2,,,0) 
Total = FormatNumber(objLogicalDisk.Size/CONVERT_GB,2,,,0)
Used = FormatNumber (Total - Free,2,,,0)
Percent = FormatNumber((Free/Total) * 100,1)
VolName = objLogicalDisk.VolumeName

ofile.writeline Computer & "," & objLogicalDisk.DeviceID & "," & VolName & "," & Total & "," & Used & "," & Free & "," & Percent
' wscript.echo Computer & "," & objLogicalDisk.DeviceID & "," & Total & "," & Used & "," & Free & "," & Percent

End Function



Function TryLocalAdminConnect
On Error Resume Next
' Create log file objects
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFso.CreateTextFile("C:\ConnectLog.txt", True)
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
arrayPasswords = Array("*******", "*******", "*********")
objLogFile.WriteLine "Maximum Passwords to try: " & UBound(arrayPasswords) + 2
For i = 0 To UBound(arrayPasswords)
    Set objSWbemServices = objSWbemLocator.ConnectServer(sComputer, "root\cimv2", "Administrator", arrayPasswords(i))
 
    objLogFile.WriteLine "Connection Attempt: " & i + 1
 
    If Err Then
        ' Output error if need be...
        objLogFile.WriteLine "Connection to: " & sComputer & " Using Password: " & arrayPasswords(i) & " Failed."
        objLogFile.WriteLine "Reason: " & Err.Description
        objLogFile.WriteLine "----------------------------------------------------------------------------"
 
    Else
        ' Do what you need
        objLogFile.WriteLine "Connection to: " & sComputer & " Using Password: " & arrayPasswords(i) & " Succeeded."
        objLogFile.WriteLine "Reason: N/A"
        objLogFile.WriteLine "----------------------------------------------------------------------------"
 
    TryAdminConnect = True ' Success
    Exit Function
 End If
 
Next 
 
End Function


Function ExecuteShellProgram(ByVal sFileName)

Dim poShell 
Dim poProcess
Dim iStatus

Set poShell = CreateObject("WScript.Shell")
Set poProcess = poShell.Exec(sFileName)

'Check to see if we started the process without error

if ((poProcess.ProcessID=0) and (poProcess.Status=1)) then
Err.Raise vbObjectError,,"Failed executing process"
end if

'Now loop until the process has terminated, and pull out
'any console output

Do
'Get current state of the process
iStatus = poProcess.Status

'Forward console output from launched process
'to ours
WScript.StdOut.Write poProcess.StdOut.ReadAll()
WScript.StdErr.Write poProcess.StdErr.ReadAll()

'Did the process terminate?
if (iStatus <> 0) then
Exit Do
end if
Loop 

'Return the exit code
ExecuteShellProgram = poProcess.ExitCode

End Function

Cheers for all the time and effort mate.
 
Afraid to say this hasnt worked either. We have decided that we are going to go with a single account that will have minimal access rights that will run the script and check the hard drive space. This might simplify things considerably as we will eliminate the possibility of getting a password wrong. Sorry about all this dude, really appreciate all the work you put in for this but would you please show me how to just use one single domain log on for this instead of the password array?
 
Back
Top Bottom