Page 1 of 1 1
Topic Options
#154181 - 2005-12-28 05:23 PM Help with Output from For Each Loop
jwmac Offline
Fresh Scripter

Registered: 2005-12-22
Posts: 5
First quick question, Is there a way to prevent the script code results from being displayed in output whether it be on screen or in a file? My srcript below dumps output to files just fine but the output in the file is always followed by a "0" at the end. Is there a way to prevent this?

Now to my main question. I have written a simple inventory script and as of right now everything is working great but I have overlooked an issue with the Harddrive query through WMI. As you can see in the script below I have a FOR EACH loop grabbing hard drive info but the way I have my output set up the last drive is the only one being sent to output.

With an early version of this script, I was printing output within the FOR EACH loop after each varible which worked fine. Now Im guessing I will need a counter which would create a new variable each time through the loop(ie. $Var+1,Var+2,...) but I'm not quite sure how to do so.

Any help is appreciated and FYI, I am experimenting and searching with ways to do this, and not just coming here for a cheap, quick, lazy answer although my first question was kind of lazy. Thanks

Code:
 break on

$InvCheck = READVALUE ("HKEY_LOCAL_MACHINE\HARDWARE\IT Inventory", "Stamp")
If $InvCheck = 1
Goto End
Else
cls

? "Performing an inventory of this computer... please wait..."

sleep 2

$WMI = "WINMGMTS:{IMPERSONATIONLEVEL=IMPERSONATE}!//@WKSTA"
$ComputerModel = GetObject($WMI).ExecQuery("Select * FROM Win32_ComputerSystem")
$FindBIOSInfo = GetObject($WMI).ExecQuery("Select * FROM Win32_BIOS")
$DiskSet = GetObject($WMI).ExecQuery("SELECT * FROM WIN32_LOGICALDISK WHERE DriveType=3")
$mem = memorysize(0)
$netinfoip = ENUMIPINFO (0, 0, 1)
$netinfonic = ENUMIPINFO (0, 2, 1)

For Each $model in $ComputerModel
$CompModel = $model.model
Next

For Each $tag in $FindBIOSInfo
$servicetag = $tag.SerialNumber
Next

$TotalSpace = 0.0
$FreeSpace = 0.0


For Each $Disk in $DiskSet
$DriveName = $Disk.Name
$FreeSpace = $FreeSpace + $Disk.FreeSpace
$TotalSpace = $TotalSpace + $Disk.Size
$Format = $Disk.FileSystem
Next

$TotalSpaceGigs = $totalspace / 1073741824
$FreeSpaceGigs = $FreeSpace / 1073741824


REDIRECTOUTPUT ("@LServer\inventory$\inventory.csv")
If @error <> 0
Goto Error
Else
Endif

? @Date", "@Time", "@Site", "@LServer", "@WKsta", "$Servicetag", "@WUserID"
, "@Producttype", "@CSD", "@CPU", "@MHz", "$mem", "$DriveName", "Round
($FreeSpaceGigs,1)", "Round
($TotalSpaceGigs,1)", "$Format",
"$netinfoip", "@Address", "$netinfonic

REDIRECTOUTPUT ("@LServer\inventory$\inventory.log")
If @error <> 0
Goto Error
Else
Endif
? "**********Computer: "+@WKsta"**Site: "+@Site"*************"
? @Date
? @Time
? @Site
? @LServer ;logon server
? @WKsta
? $CompModel
? $Servicetag
? @WUserID ;username
? @Producttype" w/ "@CSD
? @CPU
? @MHz"MHz"
? $mem"MB"
? $DriveName
? Round ($FreeSpaceGigs,1)
? Round ($TotalSpaceGigs,1)
? $Format

? $netinfoip
? @Address
? $netinfonic
?
$WMI = 0

WRITEVALUE ("HKEY_LOCAL_MACHINE\HARDWARE\IT Inventory", "Stamp", "1", "REG_SZ")
WRITEVALUE ("HKEY_LOCAL_MACHINE\HARDWARE\IT Inventory", "Date", "@Date", "REG_SZ")

Redirectoutput ("")
cls
? "Inventory Complete! Thank You!"
sleep 2
:end
exit

:Error
cls
? "Share currently unavailable."
Sleep 2
exit


Top
#154182 - 2005-12-28 05:26 PM Re: Help with Output from For Each Loop
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$nul =WRITEVALUE(...) will supress the 0's

There is a FAQ on this
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#154183 - 2005-12-28 07:32 PM Re: Help with Output from For Each Loop
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Yep. And the use of writeline instead of redirectoutput will also prevent any unwanted info the end up in the output file. Using writeline you can just write the stuff you want instead of dumping all output to a file.

Putting an index in a for each loop is quite easy. Have a look at the example below.

Code:

Break on
$test = Split ("1~2~3~4~5~6~7~8~9~0","~")
$index = 0
For Each $number in $test
$index = $index + 1
?"Index: " + $index + " Number: " + $number
Next
Sleep 5



Edited by Mart (2005-12-28 07:33 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#154184 - 2005-12-28 07:35 PM Re: Help with Output from For Each Loop
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
and the url for the FAQ is http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=81592&an=0&page=0#81592

You may also want to peruse the FAQ on the use of '?'
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=81641&an=0&page=0#81641

For you loop, rather that create dynamic var names as you suggest, build an array.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#154185 - 2005-12-28 07:38 PM Re: Help with Output from For Each Loop
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
There is a FAQ as well on the pros and cons of WriteLine() vs. RedirectOutput() but this time I won't supply the URL cuz you should learn to find it yourself.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#154186 - 2005-12-31 02:20 PM Re: Help with Output from For Each Loop
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
And there are already UDFs that pull hardrive size infos.
_________________________
There are two types of vessels, submarines and targets.

Top
#154187 - 2006-01-01 07:30 PM Re: Help with Output from For Each Loop
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Instead of re-inventing the wheel with WMI, why not use WMIQuery()?

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#154188 - 2006-01-04 04:43 PM Re: Help with Output from For Each Loop
jwmac Offline
Fresh Scripter

Registered: 2005-12-22
Posts: 5
thanks to all for helping my newbie self. much apprieciated!
Top
Page 1 of 1 1


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

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

Generated in 0.14 seconds in which 0.107 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