Page 1 of 2 12>
Topic Options
#29374 - 2002-09-20 07:51 PM WMIQuery DriveType 3 list
Anonymous
Unregistered


Hi!

I'm noodling around with the WMIQuery UDF script and have modified it to meet my nefarious ends, but there's one last bit I haven't been able to figure out.

I would like to spit out a text file with the drive name, file system, drive size, and free space for each local drive like so:

code:
C:	NTFS	79990847488	74390184960
D: NTFS 79990847488 74390184960
E: NTFS 79990847488 74390184960

I'm guessing you split the info for drive type 3 the same way it's done for dimms and nics in the WMIQuery UDF script:

code:
For Each $dimm in Split(WMIQuery("Capacity","Win32_PhysicalMemory"),"|")
$DimmMem = Val($dimm) / 1048576
WriteLine(6, @wksta + chr(9) + $DimmMem + Chr(13) + Chr(10))
Next

I can't figure that part out, though. The dimm and nic examples only deal with one bit of info ($dimm and $nic), but I need 4 bits per local disk ($drivename, $filesystem, $drivesize, and $freespace).

Plus, the PC I'm testing this all on only has one partition so even if I could figure it out I'd never be sure if it was working.

I'm pulling the info for local disks like so:

code:
	$DriveName = WMIQuery("Name","Win32_LogicalDisk",,"DriveType","3")
$FileSystem = WMIQuery("FileSystem","Win32_LogicalDisk",,"DriveType","3")
$FreeSpace = WMIQuery("FreeSpace","Win32_LogicalDisk",,"DriveType","3")
$DriveSize = WMIQuery("Size","Win32_LogicalDisk",,"DriveType","3")

...but that only gives me info for drive c:

I hope I'm explaining this clear enough...

Many thanks in advance for any help. Truly appreciated.

Top
#29375 - 2002-09-20 08:45 PM Re: WMIQuery DriveType 3 list
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
This is work in progress, but it may give you some clues, as I'm working towards the same objective. Function dependencies are wmiquery(), words(), word() which you can find in the UDF forum on this BB.
code:
;
;local disk info
$numparts=WMIQuery("Partitions","Win32_DiskDrive","$machine")
$disksize=WMIQuery("Size","Win32_DiskDrive","$machine")
$index1=0
FOR EACH $item IN SPLIT(WMIQuery("DriveType","Win32_LogicalDisk","$machine"),"|")
$index1=$index1+1
IF TRIM($item) == "3"
$disktype=$disktype+" "+$item
$index2=0
FOR EACH $item IN SPLIT(WMIQuery("DeviceID","Win32_LogicalDisk","$machine"),"|")
$index2=$index2+1
IF $index1==$index2
$diskid=$diskid+" "+$item
$index3=0
FOR EACH $item IN SPLIT(WMIQuery("FreeSpace","Win32_LogicalDisk","$machine"),"|")
$index3=$index3+1
IF $index1==$index3
$diskfree=$diskfree+" "+$item
$index4=0
FOR EACH $item in SPLIT(WMIQuery("FileSystem","Win32_LogicalDisk","$machine"),"|")
$index4=$index4+1
IF $index1=$index4
$filesys=$filesys+" "+$item
ENDIF
NEXT
ENDIF
NEXT
ENDIF
NEXT

ENDIF
NEXT

;?? "TEST OUTPUT"
? "total local disk size = "$disksize" Bytes"
? "number of disk partitions = "$numparts
;rearrange the strings
$m=""
for $j = $numparts to 1 step -1
$m = $m +" "+word($disktype,$j)
next
$disktype=$m
? "local disk type(s) = "$disktype
$m=""
for $j = $numparts to 1 step -1
$m = $m +" "+word($diskid,$j)
next
$diskid=$m
? "local disk name(s) = "$diskid
$m=""
for $j = $numparts to 1 step -1
$m = $m +" "+word($diskfree,$j)
next
$diskfree=$m
? "local disk free space(s) = "$diskfree" Bytes"
$m=""
for $j = $numparts to 1 step -1
$m = $m +" "+word($filesys,$j)
next
$filesys=$m
? "local disk file system(s) = "$filesys
;

Hope this helps... [Smile]

[ 20. September 2002, 21:55: Message edited by: Waltz ]
_________________________
We all live in a Yellow Subroutine...

Top
#29376 - 2002-09-20 08:55 PM Re: WMIQuery DriveType 3 list
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
This is a 'rebuild' of the script i use to keep track of the disks in my servers

The function: DiskInfo() returns an array of diskinfo on the form:
Drive,Filesystem,Size,FreeSpace

code:
Function DiskInfo($PC)
Dim $Disks[0],$NumDisks,$Found,$Row
$DiskInfo = 0
$NumDisks = 0
$Found = 0
$Row = ''
$objWMI = GetObject("winmgmts:{impersonationlevel=impersonate}!//" + $PC)
If @Error
Return
EndIf
$colDisks = $objWMI.ExecQuery("Select * From win32_logicalDisk")
For Each $objDisk In $colDisks
If $objdisk.mediatype = 12
$Row = $objdisk.name + ','
$Row = $Row + $objdisk.FileSystem + ','
$Row = $Row + $objDisk.size + ','
$Row = $Row + $objDisk.FreeSpace
ReDim Preserve $Disks[$NumDisks]
$Disks[$NumDisks] = $Row
$NumDisks = $NumDisks + 1
$Row = ''
$Found = 1
EndIf
Next
$objWMI = 0
If $Found
$DiskInfo = $Disks
EndIf
EndFunction

; * Test code
Break On

$PC = 'Server1'
For Each $Disk In DiskInfo($PC)
$Disk ?
Next

Get $x

Return
; * End Test code

-Erik

[ 21. September 2002, 18:50: Message edited by: kholm ]

Top
#29377 - 2002-09-20 09:26 PM Re: WMIQuery DriveType 3 list
Anonymous
Unregistered


Alas, Waltz, I couldn't get your script to run.

Kholm, your script works just fine and dandy, but it sort of hangs for a good long while (15+ seconds) before returning the prompt.

In my attempt to get the output to a text file, I tried:

code:
$PC = @wksta
if open(9, "s:\computing\scripts\" + @wksta + "_dr.txt",5) = 0
For Each $Disk In DiskInfo($PC)
$Disk ?
writeline(9, @wksta + chr(9) + $objdisk.name + chr(9) + $objdisk.filesystem + chr(9) + $objdisk.size + chr(9) + $objdisk.freespace + chr(13) + chr(10))
close (9)
Next
else
? "File open error = " @error
endif

Get $x

Return

Kinda odd. The file never opens, but the script continues to run as if it did. My screen output is:

code:
S:\COMPUT~1\scripts>kix32 dr
C: NTFS 79990847488 74390422016
00

I modified the output to use tabs instead of commas. So it spits out the drive info then spits out the two 0's, but then hangs.

Any ideas? (other than I need to learn how to do this stuff on my own [Smile]

Thanks.

Top
#29378 - 2002-09-20 09:26 PM Re: WMIQuery DriveType 3 list
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
Kholm...
Nice Func()
A more elegant solution than mine... [Wink]
Cheers
_________________________
We all live in a Yellow Subroutine...

Top
#29379 - 2002-09-20 09:34 PM Re: WMIQuery DriveType 3 list
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
Shane...
That's odd, because it runs fine for me - Kix ver 4.12 Beta on XP Pro...

I've just ammended my script to include 'filesystem' and reran it moments ago with no problems...
Be sure you have the word() and words() functions for the string parsing thingy.

If you still want to pursue this, after seeing KHolm's solution [Big Grin] , post reply.

Cheers...
_________________________
We all live in a Yellow Subroutine...

Top
#29380 - 2002-09-20 09:50 PM Re: WMIQuery DriveType 3 list
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Shane,

You don't get the object returned from DiskInfo(), but an array of comma separated strings.

After your rebuild it will return an array of tabulator separated strings, and you should write it
directly to the file.

Use it like this:
code:
$PC = @wksta
if open(9, "s:\computing\scripts\" + @wksta + "_dr.txt",5) = 0
For Each $Disk In DiskInfo($PC)
$Disk ?
$RC = writeline(9, $Disk + @CRLF)
$RC = close (9)
Next
else
? "File open error = " @error
endif

Get $x

Return

-Erik

Top
#29381 - 2002-09-20 10:18 PM Re: WMIQuery DriveType 3 list
Anonymous
Unregistered


I must be doing something very wrong [Mad] cause it just hangs and never writes a file (though it does output to screen). I have to hit ctrl-z to get the prompt back.

Here is the whole script as I have it now:
code:
; * Test code
$PC = @wksta
if open(9, "s:\computing\scripts\" + @wksta + "_dr.txt",5) = 0
For Each $Disk In DiskInfo($PC)
$Disk ?
$RC = writeline(9, $Disk + @CRLF)
$RC = close (9)
Next
else
? "File open error = " @error
endif

Get $x

Return

; * End Test code

Function DiskInfo($PC)
Dim $Disks[0],$NumDisks,$Found,$Row
$DiskInfo = 0
$NumDisks = 0
$Found = 0
$Row = ''
$objWMI = GetObject("winmgmts:{impersonationlevel=impersonate}!//" + $PC)
If @Error
Return
EndIf
$colDisks = $objWMI.ExecQuery("Select * From win32_logicalDisk")
For Each $objDisk In $colDisks
If $objdisk.mediatype = 12
Dim $Row[4]
$Row = $objdisk.name + chr(9);','
$Row = $Row + $objdisk.FileSystem + chr(9) ;','
$Row = $Row + $objDisk.size + chr(9) ;','
$Row = $Row + $objDisk.FreeSpace
ReDim Preserve $Disks[$NumDisks]
$Disks[$NumDisks] = $Row
$NumDisks = $NumDisks + 1
$Row = ''
$Found = 1
EndIf
Next
$objWMI = 0
If $Found
$DiskInfo = $Disks
EndIf
EndFunction

Any reason why it would be hanging? Geez, I feel like an idiot.

In my 8th grade science class I was the only kid whose fake volcanoe didn't work. Go figure...

[ 20. September 2002, 22:21: Message edited by: Shane ]

Top
#29382 - 2002-09-20 10:27 PM Re: WMIQuery DriveType 3 list
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11162
Loc: Boston, MA, USA
You could also try REDIRECTOUTPUT or WRITEPROFILESTRING if you can't get the WRITELINE to work.
_________________________
There are two types of vessels, submarines and targets.

Top
#29383 - 2002-09-20 10:38 PM Re: WMIQuery DriveType 3 list
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
Shane...

I get an invalid file name error with your test code, even when I substitute my own output destination. I rewrote the test code to use redirectoutput instead of writeline and it works fine.
Try this test code...
code:
; * Test code
Break on
$PC = @wksta
;$outfile = "c:\temp\" + @wksta + "_dr.txt"
$outfile = "s:\computing\scripts\" + @wksta + "_dr.txt"
if redirectoutput ($outfile,1) = 0
For Each $Disk In DiskInfo($PC)
$Disk ?
Next
else
? "File open error = " @error
endif

; * End Test code



[ 20. September 2002, 22:59: Message edited by: Waltz ]
_________________________
We all live in a Yellow Subroutine...

Top
#29384 - 2002-09-23 02:43 PM Re: WMIQuery DriveType 3 list
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
look at this for a different approach.

$HD =WMIQuery("Size","Win32_DiskDrive")
$hdGB =left($hd,len($hd)-9)

WMIQuery will retrieve all your info, you just need to specifiy where you are getting it from....
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#29385 - 2002-09-23 04:13 PM Re: WMIQuery DriveType 3 list
Breaker Offline
Hey THIS is FUN
*****

Registered: 2001-06-15
Posts: 268
Loc: Yorkshire, England
Try this if you are still having problems - you can alter the location of the logfile in the script but it should at least generate a file with some info in it.

code:
Break on
? "What PC would you like to query for disk info? (Press return for local machine) "
Gets $target
If not $target
$target = @WKSTA
Endif
$WMI = GetObject("winmgmts:{impersonationlevel=impersonate}!//" + $target)
If $WMI
$Disks = $WMI.ExecQuery("select * from win32_logicaldisk")
;$x = Open(1,"\\server\share\disklog.txt",5)
$x = Open(1,"c:\disklog.txt",5)
If @ERROR <> 0
? "Error opening logfile!"
Goto end
Endif
for each $disk in $disks
$name = $disk.name
$size = $disk.size
$filesystem = $disk.filesystem
$freespace = $disk.freespace
$x = Writeline(1,$name + " " + $filesystem + " " + $size + " " + $freespace + chr(13) + chr(10))
next
$x = Close(1)
Else
? "WMI not connected - error!"
Endif

:end
Exit

Hope this helps.
_________________________
================================================
Breaker


Top
#29386 - 2002-09-23 05:58 PM Re: WMIQuery DriveType 3 list
Anonymous
Unregistered


Thanks a lot guys. I have it sorta/kinda working with Waltz' suggestion:

code:
if exist ("s:\computing\scripts\wksta\" + @wksta + "_dr.txt")
goto "over"
else
$PC = @wksta
$outfile = "s:\computing\scripts\wksta\" + @wksta + "_dr.txt"
if redirectoutput ($outfile,1) = 0
For Each $Disk In DiskInfo($PC)
$Disk ?
Next
; redirectoutput ("")
endif
; redirectoutput ("")
return
; redirectoutput ("")
endif

; redirectoutput ("")
? "stuff for the screen"
:over

Problem is, I can't get it to redirect the output back to the screen.

I've tried
code:
redirectoutput ("")

like it says in the Kixtart help, but it won't spit stuff out back to the screen. I've put the command after the NEXT, after the nested ENDIF, after the RETURN, and after the bottom ENDIF. "Stuff for the screen" never makes it to the screen, however.

What am I doing wrong?

Top
#29387 - 2002-09-23 06:21 PM Re: WMIQuery DriveType 3 list
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11162
Loc: Boston, MA, USA
Yes, but you commented them all out in your code.
_________________________
There are two types of vessels, submarines and targets.

Top
#29388 - 2002-09-23 06:24 PM Re: WMIQuery DriveType 3 list
Anonymous
Unregistered


Jens, I'm only showing where I've tried putting them. When I was trying it out one of them was uncommented.
Top
#29389 - 2002-09-23 06:36 PM Re: WMIQuery DriveType 3 list
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
does this do what you need???

code:
$name=	split(wmiquery("name","win32_logicalDisk"),"|")
$fs= split(wmiquery("filesystem","win32_logicalDisk"),"|")
$size= split(wmiquery("size","win32_logicalDisk"),"|")
$fsp= split(wmiquery("freespace","win32_logicalDisk"),"|")

for $loop=0 to ubound($name)
? $name[$loop]" "$fs[$loop]" "$size[$loop]" "$fsp[$loop]
next

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#29390 - 2002-09-23 06:39 PM Re: WMIQuery DriveType 3 list
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
code:
if exist ("s:\computing\scripts\wksta\" + @wksta + "_dr.txt")
goto "over"
else
$PC = @wksta
$outfile = "s:\computing\scripts\wksta\" + @wksta + "_dr.txt"
For Each $Disk In DiskInfo($PC)
if redirectoutput ("") = 0 endif; send it to the screen
$Disk ?
if redirectoutput ($outfile,1) = 0 endif; send it to the file
$Disk ?
Next
;return; return to what? why?
endif
; send it to the screen
if redirectoutput ("") = 0
? "stuff for the screen"
:over



[ 23. September 2002, 19:12: Message edited by: Waltz ]
_________________________
We all live in a Yellow Subroutine...

Top
#29391 - 2002-09-23 06:42 PM Re: WMIQuery DriveType 3 list
Anonymous
Unregistered


Radimus:

Well, I'm not sure. First off, I only need the local drives. I only have one local partition so I can't see if your script spits out info for more than one local parition.

If I could get that for all local partitions on a system then, yes, that's what I need.

Thanks!

Top
#29392 - 2002-09-23 06:47 PM Re: WMIQuery DriveType 3 list
Anonymous
Unregistered


Waltz:

Your latest offering fares no better against this vile problem. I get the disk info output to the screen, then text file is written, but the "Stuff for the screen" is never displayed on the screen.

My prompt:
code:
S:\computing\scripts>kix32 inventory
C: NTFS 79990847488 73850057728

S:\computing\scripts>_

Weird.

Top
#29393 - 2002-09-23 07:00 PM Re: WMIQuery DriveType 3 list
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
Shane...
I edited my last offering...
Try again...
_________________________
We all live in a Yellow Subroutine...

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Arend_) and 175 anonymous users online.
Newest Members
gkustra, emnipetro, Hirze, thequeen, ameliaryan
17642 Registered Users

Generated in 0.065 seconds in which 0.038 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org