|
WMIQuery DriveType 3 list
|
|
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.
|
Waltz
|
(Seasoned Scripter)
|
2002-09-20 08:45 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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... [ 20. September 2002, 21:55: Message edited by: Waltz ]
|
kholm
|
(Korg Regular)
|
2002-09-20 08:55 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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 ]
|
|
Re: WMIQuery DriveType 3 list
|
|
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
Thanks.
|
Waltz
|
(Seasoned Scripter)
|
2002-09-20 09:26 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
Kholm... Nice Func() A more elegant solution than mine... Cheers
|
Waltz
|
(Seasoned Scripter)
|
2002-09-20 09:34 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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 , post reply.
Cheers...
|
kholm
|
(Korg Regular)
|
2002-09-20 09:50 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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
|
|
Re: WMIQuery DriveType 3 list
|
|
I must be doing something very wrong 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 ]
|
|
Re: WMIQuery DriveType 3 list
|
|
You could also try REDIRECTOUTPUT or WRITEPROFILESTRING if you can't get the WRITELINE to work.
|
Waltz
|
(Seasoned Scripter)
|
2002-09-20 10:38 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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 ]
|
Radimus
|
(KiX Supporter)
|
2002-09-23 02:43 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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....
|
Breaker
|
(Hey THIS is FUN)
|
2002-09-23 04:13 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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.
|
|
Re: WMIQuery DriveType 3 list
|
|
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?
|
|
Re: WMIQuery DriveType 3 list
|
|
Yes, but you commented them all out in your code.
|
|
Re: WMIQuery DriveType 3 list
|
|
Jens, I'm only showing where I've tried putting them. When I was trying it out one of them was uncommented.
|
Radimus
|
(KiX Supporter)
|
2002-09-23 06:36 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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
|
Waltz
|
(Seasoned Scripter)
|
2002-09-23 06:39 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
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 ]
|
|
Re: WMIQuery DriveType 3 list
|
|
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!
|
|
Re: WMIQuery DriveType 3 list
|
|
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.
|
Waltz
|
(Seasoned Scripter)
|
2002-09-23 07:00 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
Shane... I edited my last offering... Try again...
|
|
Re: WMIQuery DriveType 3 list
|
|
Waltz,
This worked:
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 ; send it to the screen $Disk ? endif if redirectoutput ($outfile,1) = 0 ; send it to the file $Disk ? endif Next endif ; send it to the screen if redirectoutput ("") = 0 ? "stuff for the screen" endif
If I didn't have the ENDIFs after the redirectoutput commands I'd get a FOR WITHOUT NEXT error.
I took out the RETURN. I have no idea why that was in there. It was in the code I pirated from Kholm so I left it there.
Thanks! [ 23. September 2002, 19:14: Message edited by: Shane ]
|
Waltz
|
(Seasoned Scripter)
|
2002-09-23 07:13 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
Bingo... My reworked one works also Cheers...
|
Radimus
|
(KiX Supporter)
|
2002-09-23 07:24 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
have you looked at?:
"Select * from Win32_DiskPartition"
Access: Availability: BlockSize: 512 Bootable: -1 BootPartition: -1 Caption: Disk #0, Partition #0 ConfigManagerErrorCode: ConfigManagerUserConfig: CreationClassName: Win32_DiskPartition Description: Installable File System DeviceID: Disk #0, Partition #0 DiskIndex: 0 ErrorCleared: ErrorDescription: ErrorMethodology: HiddenSectors: Index: 0 InstallDate: LastErrorCode: Name: Disk #0, Partition #0 NumberOfBlocks: 39085137 PNPDeviceID: PowerManagementCapabilities: PowerManagementSupported: PrimaryPartition: -1 Purpose: RewritePartition: Size: 20011590144 StartingOffset: 32256 Status: StatusInfo: SystemCreationClassName: Win32_ComputerSystem SystemName: RADIMUS Type: Installable File System [ 23. September 2002, 19:26: Message edited by: Radimus ]
|
|
Re: WMIQuery DriveType 3 list
|
|
Radimus:
Er, no, I haven't. Not that I would know what to do with it if I did, though. If you hadn't noticed I don't really know what I'm doing. I take scripts other people have done and modify them, if I can, to my own ends (Script Pirate?). If I can't modify it then I ask for help. But, thanks for the pointer; I'll file it away.
Anyhoo, I got your bit of code to do what I want (assuming it lists all local partitions, which I can't test yet):
code:
$name= split(wmiquery("name","win32_logicalDisk",,"DriveType","3"),"|") $fs= split(wmiquery("filesystem","win32_logicalDisk",,"DriveType","3"),"|") $size= split(wmiquery("size","win32_logicalDisk",,"DriveType","3"),"|") $fsp= split(wmiquery("freespace","win32_logicalDisk",,"DriveType","3"),"|")
if open (1,"s:\computing\scripts\wksta\" + @wksta + "_testdr.txt",5) = 0 for $loop=0 to ubound($name) ? @wksta + chr(9) + $name[$loop] + chr(9) + $fs[$loop] + chr(9) + $size[$loop] + chr(9) + $fsp[$loop] writeline(1, @wksta + chr(9) + $name[$loop] + chr(9) + $fs[$loop] + chr(9) + $size[$loop] + chr(9) + $fsp[$loop] + chr(13) + chr(10)) next close(1) endif
So, see, I learn a bit from the things folks here show me. The responses in this thread have been a great help.
|
Radimus
|
(KiX Supporter)
|
2002-09-23 07:51 PM
|
|
|
|
|
Re: WMIQuery DriveType 3 list
|
|
you could also do...
code:
for $loop=0 to ubound($name) $hd =$size[$loop] $hdGB =left($hd,len($hd)-9) $hdf =$fsp[$loop] $hdGBf =left($hdf,len($hdf)-9)
? $name[$loop]" "$fs[$loop]" "$hdGB"GB "$hdGBf"GB" next
|
|
Re: WMIQuery DriveType 3 list
|
|
I managed to test this:
code:
$name= split(wmiquery("name","win32_logicalDisk",,"DriveType","3"),"|") $fs= split(wmiquery("filesystem","win32_logicalDisk",,"DriveType","3"),"|") $size= split(wmiquery("size","win32_logicalDisk",,"DriveType","3"),"|") $fsp= split(wmiquery("freespace","win32_logicalDisk",,"DriveType","3"),"|")
if open (1,"s:\computing\scripts\wksta\" + @wksta + "_testdr.txt",5) = 0 for $loop=0 to ubound($name) ? @wksta + chr(9) + $name[$loop] + chr(9) + $fs[$loop] + chr(9) + $size[$loop] + chr(9) + $fsp[$loop] writeline(1, @wksta + chr(9) + $name[$loop] + chr(9) + $fs[$loop] + chr(9) + $size[$loop] + chr(9) + $fsp[$loop] + chr(13) + chr(10)) next close(1) endif
...on a system with lots of drives and partitions and it does indeed list each partition on its own line, just like I had hoped it would. Thanks, Radimus!
|