Page 1 of 2 12>
Topic Options
#125211 - 2004-08-18 02:51 PM Help please with WMI query
bowhuntr Offline
Fresh Scripter

Registered: 2002-02-18
Posts: 33
I am trying to get the size value of each physical hard drive in a machine. I am able to get the value of the first drive but can't seem to figure out how to code it to get the value of each drive if there is more than one present. Here is the code I'm using please help if you can. Thanks!

Code:

$Dsks = GetObject("winmgmts://$computername").ExecQuery("select * from Win32_DiskDrive")
if @error <> 0
? @error + " / " @serror ? ? shell "%comspec% /c pause"
else
for each $Item in $Dsks
$TotalDsks = $Item.Size
If $TotalDsks = ""
$TotalDsks = "NULL"
else
endif
next
endif


Top
#125212 - 2004-08-18 02:55 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
to try out, start with this:
Code:

$Dsks = GetObject("winmgmts://$computername").ExecQuery("select * from Win32_DiskDrive")
if @error
"Error occured: " @error " " @serror ? "Press the anyKey to quit" get $key quit
endif
for each $Item in $Dsks
? $Item.Size
next

_________________________
!

download KiXnet

Top
#125213 - 2004-08-18 03:25 PM Re: Help please with WMI query
Anonymous
Unregistered


OK, that does echo all the drives, but I need to assign each one a variable so I can redirect the output to a file along with all the other info I'm capturing. How can I go about doing it that way?
Top
#125214 - 2004-08-18 05:01 PM Re: Help please with WMI query
bowhuntr Offline
Fresh Scripter

Registered: 2002-02-18
Posts: 33
I assume the best way to do this is create an array on the fly from the data I get back from $Item.Size. Then I could echo the array contents into my file. Can someone point me in the right direction? I am unsure how to create the array on the fly or to echo the array contents when I don't now how many drives exist. Thanks!
Top
#125215 - 2004-08-18 05:14 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
easiest to code the whole thing again:

Code:

$drives[0]
$Dsks = GetObject("winmgmts://$computername").ExecQuery("select * from Win32_DiskDrive")
if @error
"Error occured: " @error " " @serror ? "Press the anyKey to quit" get $key quit
endif
for each $Item in $Dsks
;dim again preserving the data that is in the array.
;redim with upper bound (the element amount) of array added by one.
redim preserve $drives[ubound($drives)+1]
$drives[ubound($drives)]= $Item.Size ;place the data to the last element
next



now to use it, remember that we created the array $drives[0] and never actually did place data to the first element, so to use it, don't reference invain the 0-element, which is empty but start from element 1.
_________________________
!

download KiXnet

Top
#125216 - 2004-08-18 06:07 PM Re: Help please with WMI query
bowhuntr Offline
Fresh Scripter

Registered: 2002-02-18
Posts: 33
Jooel,

I have tried to run your code as you wrote it (with the exception of replacing $computername with and actual computer name) and I get an error on the second line.
Code:
$Dsks = GetObject("winmgmts://$computername").ExecQuery("select * from Win32_DiskDrive")  



The error is: Error: unexpected command!

What am I doing wrong? Kix 4.21 if it matters.

Top
#125217 - 2004-08-18 08:47 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I copied your code directly, so how should I know?
_________________________
!

download KiXnet

Top
#125218 - 2004-08-18 09:02 PM Re: Help please with WMI query
bowhuntr Offline
Fresh Scripter

Registered: 2002-02-18
Posts: 33
This is the code I am trying to run in a script all by itself. There is no other code on the page and I replace $computername with a real machine name on my network I have full access to.

Code:

$drives[0]
$Dsks = GetObject("winmgmts://rlg012kd").ExecQuery("select * from Win32_DiskDrive")
if @error
"Error occured: " @error " " @serror ? "Press the anyKey to quit" get $key quit
endif
for each $Item in $Dsks
;dim again preserving the data that is in the array.
;redim with upper bound (the element amount) of array added by one.
redim preserve $drives[ubound($drives)+1]
$drives[ubound($drives)]= $Item.Size ;place the data to the last element
next




I get the error unexpected command! and its complaining abot the line thats starts with $Dsks = . If I comment out the first line the error goes away but then I wouldn't get the array either. Any ideas?

Top
#125219 - 2004-08-18 09:07 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
like said, it's the same that your original code in that part.
so if it's not working now, it never worked before either.
_________________________
!

download KiXnet

Top
#125220 - 2004-08-18 09:52 PM Re: Help please with WMI query
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I'e never seen it done that way. Try breaking it up into two separate lines.
untested
Code:
$Query = GetObject("winmgmts://rlg012kd")
$Dsks = $Query.ExecQuery("select * from Win32_DiskDrive")

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125221 - 2004-08-18 10:01 PM Re: Help please with WMI query
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Don't know what that line:
$drives[0]

is supposed to do, nor do I follow what you do with the array in the loop. Why not start simple and get the query to work before messing with arrays?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125222 - 2004-08-18 10:04 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
les, I love you!

$drives[0] line should actually be:
dim $drives[0]

obviously.
I thought it that way, just wrote and read it differently!
_________________________
!

download KiXnet

Top
#125223 - 2004-08-18 10:04 PM Re: Help please with WMI query
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
OK, took your way out for a spin and it fails. Try something like this:
$Query = GetObject("winmgmts:{impersonationLevel=impersonate}!\\rlg012kd\root\cimv2")
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125224 - 2004-08-18 11:16 PM Re: Help please with WMI query
bowhuntr Offline
Fresh Scripter

Registered: 2002-02-18
Posts: 33
OK, I don't get an error now since I put the dim statement before $drives[0] . If this is actually creating an array with the size of all the disk drives listed in it how do I access the results of the array? the ultimate goal is to be able to redirect the output to a file but for now I will settle for having it on screen. So far I have tried to do ? "$drives[1] , etc and get nothing. Also how would I handle echoing all the results out to the screen or a file when I don't know how many elements the array contains?

Thanks for everyones help. I really hate being such a pest with what are to you probably silly newbie questions.

Top
#125225 - 2004-08-18 11:30 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
that mentioned ubound() says how many elements it has.
_________________________
!

download KiXnet

Top
#125226 - 2004-08-19 01:19 PM Re: Help please with WMI query
bowhuntr Offline
Fresh Scripter

Registered: 2002-02-18
Posts: 33
and how do I go about seeing the results of that?
Top
#125227 - 2004-08-19 01:23 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
by looking from the manual.
_________________________
!

download KiXnet

Top
#125228 - 2004-08-19 01:58 PM Re: Help please with WMI query
bowhuntr Offline
Fresh Scripter

Registered: 2002-02-18
Posts: 33
I have read the manual and paid close attention to the section on arrays. If I found the answer there (or one I understood) I wouldn't be asking here. I offer my sincerest thanks for your help thus far, but it appears I have worn out my welcome.
Top
#125229 - 2004-08-19 02:05 PM Re: Help please with WMI query
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
no, that's not the case.
but showing somehow that you are actually trying to solve it by yourself would not hurt.
just throwing a question on each un-explained command, does make me suspicious.
_________________________
!

download KiXnet

Top
#125230 - 2004-08-19 02:50 PM Re: Help please with WMI query
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Code:
 
dim $drives
$Dsks = GetObject("winmgmts://rlg012kd").ExecQuery("select * from Win32_DiskDrive")
for each $Item in $Dsks
redim preserve $drives[ubound($drives)+1]
$drives[ubound($drives)]= $Item.Size ;place the data to the last element
next

for each $d in $drives
$d ?
next


_________________________
Eric

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 1574 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.085 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