Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Script to retrieve Disk Information 2

Status
Not open for further replies.

mutley1

MIS
Jul 24, 2003
909
Hi Experts,

Sorry this is a cross post, but I put it in the wrong VB forum previously.

Some kind soul helped me once before to write an Active X VB script (for part of a SQL2000 DTS package) to use WMI / WIN32_Services to retrieve the status of services on servers (please see below). I understand there is another WMI that can pull back disk size and free space and was wondering if anyone could help me adapt the below to do this. I have a text file with a list of servers to query and would like to use WIN32_LogicalDisk to run through the same list and bring back the computer name, disk letter, size and free space. Is this possible?

I'm guessing it is the section in red that needs amending - I've been trying but with no success as I have never really touched VB.

Thanks in advance,

M


Code:
'**********************************************************************
'  Visual Basic ActiveX Script
'************************************************************************

Function Main()

On Error Resume Next

'Set the report output directory (end with a backslash)
dir = "D:\ServicesAnalysis\"
'Set the text report file name
filename = "ServiceStatus.txt"


'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'open the data file
Set oTextStream = oFSO.OpenTextFile("D:\ServicesAnalysis\LISTWorkgroup.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each strComputer In RemotePC

[COLOR=red]Set objWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colRunningServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
    For Each objService in colRunningServices
        If ucase (objService.DisplayName) = "WORKGROUP SERVER" Then
        Report = Report & vbCrLf & "Computer ," & strComputer & ", reports service ," & objService.DisplayName  & ", is ," & objService.State 
        End If
    Next
Next
[/color red]
If Not oFSO.FolderExists(dir) Then
    oFSO.CreateFolder(dir)
End If
Set ts = oFSO.CreateTextFile (dir & filename, True)
ts.write report



    Main = DTSTaskExecResult_Success
End Function
 
This is an example...

This should be useful to you too:
Code:
strComputer = "."
Set objWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk")
For Each objDisk in colDisks
	WScript.Echo objDisk.Name
	WScript.Echo objDisk.Size
	WScript.Echo objDisk.FreeSpace
	WScript.Echo ""
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I'll give that a go over the weekend.

Thanks a lot for the help dm4ever - much appreciated.

Cheers,

M.
 
Hi Jake,

Yes - I've been playing with the script dm4ever posted and also tried the MS scripting tool, but can't get either of them to work. Any ideas would be appreciated.

Cheers,

M.
 
The script that dm4ever posted should work just fine. The only issue is that it uses "." as the computer name, which is the local computer. If you wanted it to check each of the disks on each of the remote computers, you would need to take that line out and nest it where you have the code in red above.

Other things to look at, is your array (RemotePC) being created correctly? Try putting a Wscript.Echo strComputer in there to make sure that it's actually trying a valid PC name.

Then there's connectivity. This script assumes that the PC is up and that you can connect to it's WMI provider. If it has a firewall installed, then necessary ports could be blocked.
 
Hi KMCFerrin,

The original script I posted works absolutely fine to collect the service status for all 40 odd servers in the text file defined in the script (D:\ServicesAnalysis\LISTWorkgroup.txt). I was assuming that just changing the section I highlighted in red to the other script to call Win32_LogicalDisk as per dm4ever's script would do the trick but it doesn't seem to write out to a file. The DTS package says it has executed sucessfully, it produces the output text file, but the output file is blank.

Thanks,

M.
 
OK. There are a couple issues then if you are adding it to the script above. As I mentioned previously, you would need to remove the strComputer="." line. That line sets the value of strComputer to the local machine, which basically undoes the line above it where you say "for each strComputer in RemotePC."

The other thing is that dm4ever's script doesn't write to a file, it merely echoes it to the screen. Instead of the three lines to echo the name, size and freespace you will need to instead replace them with a line that appends them to the "report" variable that you are eventually writing to a text file.
 
Thanks a million for the help.

Is the writing not covered in the end section of my original post

Code:
If Not oFSO.FolderExists(dir) Then
    oFSO.CreateFolder(dir)
End If
Set ts = oFSO.CreateTextFile (dir & filename, True)
ts.write report

Sorry - I know nothing about VB really!

TIA,

M.
 
ts.write report" writes the contents of "report", but you're not adding the disk info to the value of "report".

Let's break down this line:

Code:
Report = Report & vbCrLf & "Computer ," & strComputer & ", reports service ," & objService.DisplayName  & ", is ," & objService.State

"vbCrLf" is a carriage return followed by a line feed. Basically it means go to the next line. When you use "&" in VBScript you are concatenating variables. So the line above translated to English would be:

Change the value of the variable named "Report" to a new value. The new value should be the current value of "Report", but then tack on a new line, then add the text "Computer ,", then add the value of the variable "strComputer", then add the text "reports service ,", then add the DisplayName property of objService, then the text ", is ,", then the State property of objService.

So if you wanted to add more data to that "report" variable then you would want to add a line at the end of the code that gets the disk info. It would be something like:

Code:
Report = Report & "Disk: " & objDisk.Name & " Size: " & objDisk.Size & " Freespace: " & objDisk.FreeSpace
 
Aaaahhhhh right! As I say I have no experience with VB at all, but what you have just told me makes total sense. I just needed it in dummy terms! I will have a go at that in the morning (at home with no access now). Basically, it was all working fine but writing out field that didn't exist in the object?

Much appreciated and a purple thing to follow.

Will let you know how I get on.

Thanks again,

M.
 
Hi KMC,

I've got this far but nothing is being written out. I took out the On Error resume next and it seems to be complaining about line 16, Error 0, Object Required: wscript.

Seems strange because the other script (for services status) runs fine - it's only the Logical disk and report output that's changed. Any ideas?

Thanks for any help & thanks for all the help so far.

Code:
'**********************************************************************
'  Visual Basic ActiveX Script
'************************************************************************

Function Main()

'On Error Resume Next

'Set the report output directory (end with a backslash)
dir = "C:\DiskSpace\"
'Set the text report file name
filename = "DiskStatus.txt"


'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
[COLOR=red yellow]set WSHShell = wscript.createObject("wscript.shell") [/color]
'open the data file
Set oTextStream = oFSO.OpenTextFile("C:DiskSpace\DiskSpaceTest.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each strComputer In RemotePC

Set objWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colitems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk")
For each objitem in colitems
        Report = Report & vbCrLf & objitem.SystemName & "Disk: " & objitem.Name & " Size: " & objitem.Size & " Freespace: " & objitem.FreeSpace & "Desc: " & objitem.Description
        'End If
   Next
Next

If Not oFSO.FolderExists(dir) Then
    oFSO.CreateFolder(dir)
End If
Set ts = oFSO.CreateTextFile (dir & filename, True)
ts.write report




	Main = DTSTaskExecResult_Success
End Function
 
I think I've found it - I wasn't setting colitems and objWMIService to nothing at the end. I've added that in and it is working now.

Thanks to all for your help - much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top