Page 3 of 4 <1234>
Topic Options
#135076 - 2006-08-24 06:37 PM Re: Generate html report on free space - servers
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Well, I am surprised
Code:

DISKSPACE($strComputer,$dname,5)


is not giving an error because IMHO it only supports 2 optional parameters.
I also think you have to show more about the html() function as most probably it will not support 5 or more parameters.

Top
#135077 - 2006-08-24 07:10 PM Re: Generate html report on free space - servers
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
The 57th line is an unhandled function. What does it return to hte console?
Top
#135078 - 2006-08-25 05:20 PM Re: Generate html report on free space - servers
Jaide Offline
Fresh Scripter

Registered: 2003-03-05
Posts: 32
I tried to correct the variable and got this error -

ERROR : IDispatch pointers not allowed in expressions!

The code is below (lines 15 thru 69)

Code:
  

Shell "%COMSPEC% /e:1024 /c c:\windows\netview.exe /NTS /BDC /PDC >c:\admin\kix2010_451\servers.txt"
If @error<>0
? "Netview is not found !"
?
? " You have to download netview at (http://www.optimumx.com/download/#NetView)"
? " and place it in this script directory."
Quit
EndIf

$line=""
If Open(3, "c:\admin\kix2010_451\servers.txt") = 0
$x = ReadLine(3)
While @ERROR = 0
If $x<>""
$pos=InStr($x," ")
$xf=Left($x,$pos-1)
If InStr($x,"\\")<>0
$xf=Right($xf,Len($xf)-2)
$line=$line+"~~"+$xf
EndIf
EndIf
$x = ReadLine(3)
Loop
$=Close (3)

EndIf
$servarray=Split($line,"~~")
debhtml()
For Each $strComputer in $servarray
If $strComputer<>""
? "In progress for "+$strComputer+" ..."
$objWMIService = GetObject("winmgmts:\\"+$strComputer+"\root\cimv2")
If @ERROR=0
breakl()
$DiskSet = $objWMIService.ExecQuery("select * from Win32_LogicalDisk where DriveType=3")
$co=0
For Each $Disk In $DiskSet
$co=$co+1
$dname=$Disk.Name
$percentfree=Round(DISKSPACE($strComputer,$dname,5))
$percentused=100-$percentfree
$rest=$percentfree-5
$space=GetDiskSpace($DiskSet)
html($strComputer,$dname,$percentused,$rest,$space)
Next
ftabl()
Else
? $strcomputer+" WMI not installed."
breakl()
offline($strcomputer)
ftabl()
EndIf

EndIf
Next


Top
#135079 - 2006-08-27 10:03 PM Re: Generate html report on free space - servers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Your error is in line
Code:

$space=GetDiskSpace($DiskSet)


as you're supplying an array/object into the GETDISKSPACE() function.

Use DEBUG ON to properly debug your scripts.
_________________________
There are two types of vessels, submarines and targets.

Top
#135080 - 2006-08-28 05:06 AM Re: Generate html report on free space - servers
Jaide Offline
Fresh Scripter

Registered: 2003-03-05
Posts: 32
I can't figure this out. The script outputs

In progress for Server ...Select * from Win32_LogicalDisk where DeviceID=':'

on every line. If I remove the line I do not get the desired output from GetFreeSpace function. If you can tell me where I've gone wrong I'd appreciate the help.

Lines 007 - 274

Code:
  
;===============================================================================================
;
; DEPENDENCIES: WMI on servers, netview->(http://www.optimumx.com/download/#NetView)
;
;===============================================================================================
Break on


Shell "%COMSPEC% /e:1024 /c c:\windows\netview.exe /NTS /BDC /PDC >c:\admin\kix2010_451\servers.txt"
If @error<>0
? "Netview is not found !"
?
? " You have to download netview at (http://www.optimumx.com/download/#NetView)"
? " and place it in this script directory."
Quit
EndIf

$line=""
If Open(3, "c:\admin\kix2010_451\servers.txt") = 0
$x = ReadLine(3)
While @ERROR = 0
If $x<>""
$pos=InStr($x," ")
$xf=Left($x,$pos-1)
If InStr($x,"\\")<>0
$xf=Right($xf,Len($xf)-2)
$line=$line+"~~"+$xf
EndIf
EndIf
$x = ReadLine(3)
Loop
$=Close (3)

EndIf
$servarray=Split($line,"~~")
debhtml()
For Each $strComputer in $servarray
If $strComputer<>""
? "In progress for "+$strComputer+" ..."
$objWMIService = GetObject("winmgmts:\\"+$strComputer+"\root\cimv2")
If @ERROR=0
breakl()
$DiskSet = $objWMIService.ExecQuery("select * from Win32_LogicalDisk where DriveType=3")
$co=0
For Each $Disk In $DiskSet
$co=$co+1
$dname=$Disk.Name
$percentfree=Round(DISKSPACE($strComputer,$dname,5))
$percentused=100-$percentfree
$rest=$percentfree-5
$free-GetFreeSpace($WS,$Drive)
html($strComputer,$dname,$percentused,$rest,$free)
Next
ftabl()
Else
? $strcomputer+" WMI not installed."
breakl()
offline($strcomputer)
ftabl()
EndIf

EndIf
Next

finhtml()

$IE = CreateObject("InternetExplorer.Application")
$IE.Navigate(@SCRIPTDIR+"\resserv.html")
$IE.Visible = 1


;*************************************************
Function debhtml()

Del ".\resserv.html"
$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , "
"+@CRLF)
$x = WriteLine( 3 , "

Disk used for servers on domain "+@DOMAIN+"

"+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , "Unfortunately the non Windows servers could not be query
"+@CRLF)
$x = WriteLine( 3 , "and report the line 'No WMI or server offline' :(

"+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$=Close (3)

EndFunction
;*************************************************
Function html($server,$drive,$percentfree,$rest,$free)

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , "
ServerUsed Avail-Space
"+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ''+@CRLF)
$x = WriteLine( 3 , ''+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ''+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$=Close (3)

EndFunction
;*************************************************
Function offline($server)

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , "
'+$server+':Drive '+$drive+' '+$percentfree+' % '+$free+'
"+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$=Close (3)

EndFunction
;*************************************************
Function breakl()
$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , '
'+@CRLF)
$=Close (3)
EndFunction
;*************************************************
Function ftabl()

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , "
"+$server+":No WMI or server offline
"+@CRLF)
$=Close (3)

EndFunction
;*************************************************
Function finhtml()

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , '
'+@CRLF)
$x = WriteLine( 3 , "
"+@CRLF)
$x = WriteLine( 3 , "
"+@CRLF)
$x = WriteLine( 3 , "Edition: " + @DATE + " - at " + @TIME+" by "+@FULLNAME+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$=Close (3)

EndFunction
;*************************************************
;FUNCTION DiskSpace
;
;ACTION returns the available disk space on a drive
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;CONTRIBUTORS NTDOC, Radimus, kholm
;
;VERSION 1.4 (added PetaByte, Percentage as unit sizes)
; 1.3 (shortened code, included UNCs as valid input)
; 1.2 (added optional drive and optional units)
; 1.1
;
;DATE CREATED 2001/12/18
;
;DATE MODIFIED 2003/07/06
;
;KIXTART 4.21
;
;SYNTAX DISKSPACE([DRIVE, FORMAT])
;
;PARAMETERS DRIVE
; optional driveletter, directory, or UNC (defaults to system drive)
;
; FORMAT
; optional parameter indicating the units to be used when returning the
; available disk space
; 0 = KB (KiloByte, default)
; 1 = MB (MegaByte)
; 2 = GB (GigaByte)
; 3 = TB (TeraByte)
; 4 = PB (PetaByte)
; 5 = % (percentage total diskspace, does not support UNCs)
;
;RETURNS available diskspace
;
;REMARKS none
;
;DEPENDENCIES WMI (only for FORMAT = 5)
;
;EXAMPLE $rc=DISKSPACE()
;
;
Function diskspace($computer,optional $drive, optional $format)
Dim $objWMIService, $sWQL, $colDrives, $objDrive
If NOT $drive
$drive='%WINDIR%'
EndIf
$format=Val($format)
If $format<5
$format=IIf($format=1,1024,IIf($format=2,1024.0*1024,IIf($format=3,1024.0*1024*1024,IIf($format=4,1024.0*1024*1024*1024,1))))
$diskspace=CDbl(GetDiskSpace($drive))/$format
Else
$objWMIService=GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$computer+'\root\cimv2')
$sWQL = "SELECT Size, FreeSpace FROM Win32_LogicalDisk WHERE Name='"+Left($drive,2)+"'"
$colDrives=$objWMIService.ExecQuery($sWQL)
For Each $objDrive in $colDrives
$diskspace=CDbl($objDrive.FreeSpace)/CDbl($objDrive.Size)*100
Next
EndIf
Exit @ERROR
EndFunction

;Function:
; GetFreeSpace()
;
;Author:
; Kholm
;
;Version:
; 1.0 (17th of september 2003)
;
;Action: Returns available space on diskdrives on remote workstations (where the server service is NOT started)
;
;Syntax GetFreeSpace($WS,$Drive)
;
;Parameters:
; - $WS = Name of remote workstation
; - $Drive = Driveletter for drive to be checked
;
;Returns: Free space on selected ws/drive
;
;Dependencies: WMI
;
;Remarks:
; OBSERVE: You should use the KiXtart function: GetDiskSpace('\\' + $Computer + '\' + $Drive + '$$')
; If the server service is startet on the workstation you want to check,
;
;Example:
; $Free = GetFreeSpace('WS02','C')
; 'Free space on drive C: on workstation WS02 is: ' + $Free

Function GetFreeSpace($Computer,$Drive)
Dim $oComputer,$cItems,$oItem
$oComputer = GetObject("winmgmts:\\" + $Computer + "\root\cimv2")
$cItems = $oComputer.ExecQuery("Select * from Win32_LogicalDisk where DeviceID='$Drive:'")
For Each $oItem In $cItems
$GetFreeSpace = 0.0009765625 * $oItem.FreeSpace
Next
EndFunction


Top
#135081 - 2006-08-28 07:13 AM Re: Generate html report on free space - servers
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
See if this helps...

Code:
 
Function GetFreeSpace($Computer,$Drive)
Dim $oComputer,$cItems,$oItem
$oComputer = GetObject("winmgmts:\\" + $Computer + "\root\cimv2")
$cItems = $oComputer.ExecQuery("Select * from Win32_LogicalDisk where DeviceID='" + $Drive + ":'")
For Each $oItem In $cItems
$GetFreeSpace = 0.0009765625 * $oItem.FreeSpace
Next
EndFunction


Top
#135082 - 2006-08-28 10:58 AM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Quote:

In progress for Server ...Select * from Win32_LogicalDisk where DeviceID=':'




1. WMI is either broken or you don't have access to it on one or more systems.
2. Call using something like a var in a string or feeding an array instead of a string, or similar.

I would try adding this UDF to the top of your script right about after this:
If $strComputer<>""

WMIConfirm($strComputer)


WMIConfirm() - Confirm access to WMI
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=114011

Top
#135083 - 2006-08-28 01:28 PM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Okay I think you may be making a mistake in the file reading, or at least making it a bit more difficult.

Try modifying your script to fit into this structure.
Notice the /b which will give it a bare format
that is desired for scripting the names.

Also get and place the ReadFile UDF into your script.

Dim $ServerList, $strComputer, $Servers
Dim $WMI
$ServerList = @ScriptDir + '\' + 'servers.txt'
Shell '%COMSPEC% /c netview.exe /NTS /BDC /PDC /b > ' + $ServerList
If @ERROR
"Netview is not found !" ??
"You have to download netview at (http://www.optimumx.com/download/#NetView)" ?
"and place it in this script directory." ?
Quit @ERROR
EndIf
debhtml()
$Servers = ReadFile($ServerList)
For Each $strComputer In $Servers
$strComputer = Trim(Join(Split($strComputer,'\\'),''))
If $strComputer<>""
"In progress for " + $strComputer + " ..." ?
$WMI = WMIConfirm($strComputer)
If $WMI > 0
'WMI on: ' + $strComputer + ' working okay' ?
Else
'Unable to access the ' + $strComputer + ' due to WMI error' ?
EndIf
EndIf
Next


Then to make your file write faster you can change your WriteLine to something like this (untested in working code)

Function debhtml()
Dim $Open, $x, $Close
If Exist(".\resserv.html")
Del ".\resserv.html"
EndIf
$Open = Open(3,".\resserv.html",5)
$x = "<html>"+@CRLF
$x = $x + "<head>"+@CRLF
$x = $x + "<STYLE>"+@CRLF
$x = $x + "BODY {"+@CRLF
$x = $x + " font-size:10 pt;"+@CRLF
$x = $x + " font-family: Verdana;"+@CRLF
$x = $x + "}"+@CRLF
$x = $x + "H3 {"+@CRLF
$x = $x + " font-size:12 pt;"+@CRLF
$x = $x + " font-family: Verdana;"+@CRLF
$x = $x + "}"+@CRLF
$x = $x + "TH {"+@CRLF
$x = $x + " font-family : Verdana;"+@CRLF
$x = $x + " font-size : 12;"+@CRLF
$x = $x + "}"+@CRLF
$x = $x + "TD {"+@CRLF
$x = $x + " font-family : Verdana;"+@CRLF
$x = $x + " font-size : 12;"+@CRLF
$x = $x + "}"+@CRLF
$x = $x + "</STYLE>"+@CRLF
$x = $x + "</head>"+@CRLF
$x = $x + "<body>"+@CRLF
$x = $x + "<br>"+@CRLF
$x = $x + "<H3>Disk used for servers on domain "+@DOMAIN+"</H3>"+@CRLF
$x = $x + ""+@CRLF
$x = $x + "Unfortunately the non Windows servers could not be query<br>"+@CRLF
$x = $x + "and report the line 'No WMI or server offline' : (<br><br></font>"+@CRLF
$x = $x + "<Table border=0>"+@CRLF
$x = $x + "<TR>"+@CRLF
$x = $x + "<TD width=120>Server</TD>"+@CRLF
$x = $x + "<TD width=100>Used</TD>"+@CRLF
$x = $x + "<TD width=100 bgcolor=gray> </TD>"+@CRLF
$x = $x + "<TD width=120>Avail-Space</TD>"+@CRLF
$x = $x + "</TR>"+@CRLF
$x = WriteLine(3,$x)
$Close = Close(3)
EndFunction

 

I'm 99.9% sure that you're feeding a blank or non machine name to the rest of your functions and then the GetObject
is failing but since it doesn't know to end there it keeps
generating bogus output.

Use the above coding method and I'm sure you'll end up with working code.

Top
#135084 - 2006-08-28 08:55 PM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Oops... That code will not work for you either as NETVIEW also returns the description even with the /b switch.

Well you get the idea. There are many ways to get the list and pinging them first as in one of the original scripts I posted for you. That should speeds things up for systems that not online. Systems can show in the NETVIEW or NET VIEW list for 15 minutes or so after they have been shut down.

Anyways... get a list of computer names and edit the file so that ONLY the names are in it with nothing else and then use WMIConfirm as shown and you should be good to go.
You may want to add more logging to file or something to see when or where a system could not be contacted.

Top
#135085 - 2006-08-28 10:36 PM Re: Generate html report on free space - servers
Jaide Offline
Fresh Scripter

Registered: 2003-03-05
Posts: 32
NTDOC,

Thanks for the help with the script. I have one question - where do i replace the code ih the two sections above? I noticed the script that I posted did not post as expected. It looks like a few function sections got repeated. I'll try posting again.

Code:
  

Shell "%COMSPEC% /e:1024 /c c:\windows\netview.exe /NTS /BDC /PDC >c:\admin\kix2010_451\servers.txt"
If @error<>0
? "Netview is not found !"
?
? " You have to download netview at (http://www.optimumx.com/download/#NetView)"
? " and place it in this script directory."
Quit
EndIf

$line=""
If Open(3, "c:\admin\kix2010_451\servers.txt") = 0
$x = ReadLine(3)
While @ERROR = 0
If $x<>""
$pos=InStr($x," ")
$xf=Left($x,$pos-1)
If InStr($x,"\\")<>0
$xf=Right($xf,Len($xf)-2)
$line=$line+"~~"+$xf
EndIf
EndIf
$x = ReadLine(3)
Loop
$=Close (3)

EndIf
$servarray=Split($line,"~~")
debhtml()
For Each $strComputer in $servarray
If $strComputer<>""
? "In progress for "+$strComputer+" ..."
$objWMIService = GetObject("winmgmts:\\"+$strComputer+"\root\cimv2")
If @ERROR=0
breakl()
$DiskSet = $objWMIService.ExecQuery("select * from Win32_LogicalDisk where DriveType=3")
$co=0
For Each $Disk In $DiskSet
$co=$co+1
$dname=$Disk.Name
$percentfree=Round(DISKSPACE($strComputer,$dname,5))
$percentused=100-$percentfree
$rest=$percentfree-5
$free-GetFreeSpace($WS,$Drive)
html($strComputer,$dname,$percentused,$rest,$free)
Next
ftabl()
Else
? $strcomputer+" WMI not installed."
breakl()
offline($strcomputer)
ftabl()
EndIf

EndIf
Next

finhtml()

$IE = CreateObject("InternetExplorer.Application")
$IE.Navigate(@SCRIPTDIR+"\resserv.html")
$IE.Visible = 1
;*************************************************

Function debhtml()
Dim $Open, $x, $Close
If Exist(".\resserv.html")
Del ".\resserv.html"
EndIf
$Open = Open(3,".\resserv.html",5)
$x = ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + "
"+@CRLF
$x = $x + "

Disk used for servers on domain "+@DOMAIN+"

"+@CRLF
$x = $x + ""+@CRLF
$x = $x + "Unfortunately the non Windows servers could not be query
"+@CRLF
$x = $x + "and report the line 'No WMI or server offline' : (

"+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = $x + ""+@CRLF
$x = WriteLine(3,$x)
$Close = Close(3)
EndFunction

;*************************************************

Function html($server,$drive,$percentfree,$rest,$free)

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , "
ServerUsed Avail-Space
"+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ''+@CRLF)
$x = WriteLine( 3 , ''+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ''+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$=Close (3)

EndFunction
;*************************************************
Function offline($server)

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , "
'+$server+':Drive '+$drive+' '+$percentfree+' % '+$free+'
"+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$=Close (3)

EndFunction
;*************************************************
Function breakl()
$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , '
'+@CRLF)
$=Close (3)
EndFunction
;*************************************************
Function ftabl()

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , "
"+$server+":No WMI or server offline
"+@CRLF)
$=Close (3)

EndFunction
;*************************************************
Function finhtml()

$=Open( 3 , ".\resserv.html" , 5 )
$x = WriteLine( 3 , '
'+@CRLF)
$x = WriteLine( 3 , "
"+@CRLF)
$x = WriteLine( 3 , "
"+@CRLF)
$x = WriteLine( 3 , "Edition: " + @DATE + " - at " + @TIME+" by "+@FULLNAME+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$x = WriteLine( 3 , ""+@CRLF)
$=Close (3)

EndFunction
;*************************************************
;FUNCTION DiskSpace
;
;ACTION returns the available disk space on a drive
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;CONTRIBUTORS NTDOC, Radimus, kholm
;
;VERSION 1.4 (added PetaByte, Percentage as unit sizes)
; 1.3 (shortened code, included UNCs as valid input)
; 1.2 (added optional drive and optional units)
; 1.1
;
;DATE CREATED 2001/12/18
;
;DATE MODIFIED 2003/07/06
;
;KIXTART 4.21
;
;SYNTAX DISKSPACE([DRIVE, FORMAT])
;
;PARAMETERS DRIVE
; optional driveletter, directory, or UNC (defaults to system drive)
;
; FORMAT
; optional parameter indicating the units to be used when returning the
; available disk space
; 0 = KB (KiloByte, default)
; 1 = MB (MegaByte)
; 2 = GB (GigaByte)
; 3 = TB (TeraByte)
; 4 = PB (PetaByte)
; 5 = % (percentage total diskspace, does not support UNCs)
;
;RETURNS available diskspace
;
;REMARKS none
;
;DEPENDENCIES WMI (only for FORMAT = 5)
;
;EXAMPLE $rc=DISKSPACE()
;
;
Function diskspace($computer,optional $drive, optional $format)
Dim $objWMIService, $sWQL, $colDrives, $objDrive
If NOT $drive
$drive='%WINDIR%'
EndIf
$format=Val($format)
If $format<5
$format=IIf($format=1,1024,IIf($format=2,1024.0*1024,IIf($format=3,1024.0*1024*1024,IIf($format=4,1024.0*1024*1024*1024,1))))
$diskspace=CDbl(GetDiskSpace($drive))/$format
Else
$objWMIService=GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$computer+'\root\cimv2')
$sWQL = "SELECT Size, FreeSpace FROM Win32_LogicalDisk WHERE Name='"+Left($drive,2)+"'"
$colDrives=$objWMIService.ExecQuery($sWQL)
For Each $objDrive in $colDrives
$diskspace=CDbl($objDrive.FreeSpace)/CDbl($objDrive.Size)*100
Next
EndIf
Exit @ERROR
EndFunction

;Function:
; GetFreeSpace()
;
;Author:
; Kholm
;
;Version:
; 1.0 (17th of september 2003)
;
;Action: Returns available space on diskdrives on remote workstations (where the server service is NOT started)
;
;Syntax GetFreeSpace($WS,$Drive)
;
;Parameters:
; - $WS = Name of remote workstation
; - $Drive = Driveletter for drive to be checked
;
;Returns: Free space on selected ws/drive
;
;Dependencies: WMI
;
;Remarks:
; OBSERVE: You should use the KiXtart function: GetDiskSpace('\\' + $Computer + '\' + $Drive + '$$')
; If the server service is startet on the workstation you want to check,
;
;Example:
; $Free = GetFreeSpace('WS02','C')
; 'Free space on drive C: on workstation WS02 is: ' + $Free

Function GetFreeSpace($Computer,$Drive)
Dim $oComputer,$cItems,$oItem
$oComputer = GetObject("winmgmts:\\" + $Computer + "\root\cimv2")
$cItems = $oComputer.ExecQuery("Select * from Win32_LogicalDisk where DeviceID='" + $Drive + ":'")
For Each $oItem In $cItems
$GetFreeSpace = 0.0009765625 * $oItem.FreeSpace
Next
EndFunction

;Function GetFreeSpace($Computer,$Drive)
; Dim $oComputer,$cItems,$oItem
; $oComputer = GetObject("winmgmts:\\" + $Computer + "\root\cimv2")
; $cItems = $oComputer.ExecQuery("Select * from Win32_LogicalDisk where DeviceID='$Drive:'")
; For Each $oItem In $cItems
; $GetFreeSpace = 0.0009765625 * $oItem.FreeSpace
; Next
;EndFunction



Top
#135086 - 2006-08-29 12:00 AM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
You can't post it as is because UBB won't allow you to post the html portion of your code as is. It converts it on the fly.

Regardless. I think you need to get rid of this code
Code:
If Open(3, "c:\admin\kix2010_451\servers.txt") = 0
$x = ReadLine(3)
While @ERROR = 0
If $x<>""
$pos=InStr($x," ")
$xf=Left($x,$pos-1)
If InStr($x,"\\")<>0
$xf=Right($xf,Len($xf)-2)
$line=$line+"~~"+$xf
EndIf
EndIf
$x = ReadLine(3)
Loop
$=Close (3)

EndIf



and modify a text file that has ALL the names of the Servers in
it already in a bare format like this:

SERVER1
SERVER2
SERVER3
etc...

What NETVIEW is giving you is something more like this
\\SERVER1 The main webserver
\\SERVER2 Corporate Finance
etc...

So the comments section screws up the rest of the code.
If you print to screen you'd see some garbage which is what the rest of the code sees and can't contact a server named
Corporate or Finance etc...

Try just a ReadFile UDF as shown in a test script and make sure the output on screen just gives back your Server names.
Or if you don't have that many you could even list them in the actual code
Code:
$Servers = Split('server1,server2,server3,server4',',')
For Each $Server In $Servers
If $Server
$Server ?
EndIf
Next



Then along with the WMIConfirm you should be good to go or at least see where other issues might be.

Top
#135087 - 2006-08-29 05:25 AM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Okay, so many errors so little time to fix.

I've restructured and re-written most of this script except your HTML sections.
It should run right out the gate now, but won't be what you're looking for.

I'm guessing that when/where/how you're opening/writing/closing the HTML file is not
optimal and is causing improperly coded information.

I've re-written how it get's disk space but I'm not 100% sure of how you're wanting it displayed.

So you can take it from here, or maybe if you're lucky Jooel will help out on it as he seems to
like doing the HTML type code as well.
Then fix that portion and it should actually be a nice little snippet of code.

If you can't get it going or if no one else will help I'll try to get it working, just so
I can run it myself once in a while.

I might have a scope issue there as well, just don't have time to decipher the whole script to that level right now.



Break On
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')

Global $ServerList, $strComputer, $Servers
Dim $WMI, $objWMIService, $Disk, $DiskSet
Global $dname
Global $Free, $Free0, $Free1, $Free2
Dim $IE
$ServerList = @ScriptDir + '\' + 'servers.txt'
Shell '%COMSPEC% /c netview.exe /NTS /BDC /PDC /b > ' + $ServerList
If @ERROR
"Netview is not found !" ??
"You have to download netview at (http://www.optimumx.com/download/#NetView)" ?
"and place it in this script directory." ?
Quit @ERROR
EndIf
debhtml()
$Servers = ReadFile($ServerList)
For Each $strComputer In $Servers
$strComputer = SubStr($strComputer,3,InStr($strComputer,' ')-3)
If $strComputer<>""
"In progress for " + $strComputer + " ..." ?
$WMI = WMIConfirm($strComputer)
If $WMI > 0
$objWMIService = GetObject("winmgmts:\\"+$strComputer+"\root\cimv2")
breakl()
$DiskSet = $objWMIService.ExecQuery("select * from Win32_LogicalDisk where DriveType=3")
For Each $Disk In $DiskSet
$dname = $Disk.Name
$Free0 = "" $Free1 = "" $Free2 = ""
$Free = GetDiskFreeSpace($strComputer,$dname)
$Free0 = $Free[0]
$Free1 = $Free[1]
$Free2 = $Free[2]
html($strComputer,$dname,$Free1,$Free0,$Free2)
Next
ftabl()
Else
'Unable to access ' + $strComputer + ' due to WMI error' ?
breakl()
offline($strcomputer)
ftabl()
EndIf
EndIf
Next
finhtml()
$IE = CreateObject("InternetExplorer.Application")
$IE.Navigate(@SCRIPTDIR+"\resserv.html")
$IE.Visible = 1
QUIT 1

Function WMIConfirm(optional $sComputer)
Dim $objWMIService, $objWMISetting, $colWMISettings
$sComputer = IIf(Not $sComputer,'','\\'+Join(Split($sComputer,'\'),'',3)+'\')
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!"+$sComputer+'root\cimv2')
; Failed - return 0 and exit with error value
If @ERROR
$WMIConfirm = 0
Exit Val('&' + Right(DecToHex(@ERROR), 4))
EndIf
$colWMISettings = $objWMIService.ExecQuery("Select * from Win32_WMISetting")
For Each $objWMISetting In $colWMISettings
$WMIConfirm = $objWMISetting.BuildVersion
Next
Exit 0
EndFunction

Function ReadFile($file)
Dim $lf, $f, $_, $t
$lf=CHR(10)
$f=FreeFileHandle
$_=Open($f,$file)
If @ERROR Exit @ERROR EndIf
Do $t=$t+$lf+ReadLine($f) Until @ERROR
$_=Close($f)
$ReadFile=Split(SubStr($t,2),$lf)
EndFunction

Function debhtml()
Dim $Handle, $Open, $Data, $Close
If Exist(".\resserv.html")
Del ".\resserv.html"
EndIf
$Handle = FreeFileHandle()
If $Handle > 0
$Open = Open($Handle,".\resserv.html",5)
$Data = "<html>"+@CRLF
$Data = $Data + "<head>"+@CRLF
$Data = $Data + "<STYLE>"+@CRLF
$Data = $Data + "BODY {"+@CRLF
$Data = $Data + " font-size:10 pt;"+@CRLF
$Data = $Data + " font-family: Verdana;"+@CRLF
$Data = $Data + "}"+@CRLF
$Data = $Data + "H3 {"+@CRLF
$Data = $Data + " font-size:12 pt;"+@CRLF
$Data = $Data + " font-family: Verdana;"+@CRLF
$Data = $Data + "}"+@CRLF
$Data = $Data + "TH {"+@CRLF
$Data = $Data + " font-family : Verdana;"+@CRLF
$Data = $Data + " font-size : 12;"+@CRLF
$Data = $Data + "}"+@CRLF
$Data = $Data + "TD {"+@CRLF
$Data = $Data + " font-family : Verdana;"+@CRLF
$Data = $Data + " font-size : 12;"+@CRLF
$Data = $Data + "}"+@CRLF
$Data = $Data + "</STYLE>"+@CRLF
$Data = $Data + "</head>"+@CRLF
$Data = $Data + "<body>"+@CRLF
$Data = $Data + "<br>"+@CRLF
$Data = $Data + "<H3>Disk space usage for servers on domain "+@DOMAIN+"</H3>"+@CRLF
$Data = $Data + ""+@CRLF
$Data = $Data + "Unfortunately the non Windows servers could not be queried<br>"+@CRLF
$Data = $Data + "and be reported on 'No WMI or server offline' : (<br><br></font>"+@CRLF
$Data = $Data + "<Table border=0>"+@CRLF
$Data = $Data + "<TR>"+@CRLF
$Data = $Data + "<TD width=120>Server</TD>"+@CRLF
$Data = $Data + "<TD width=100>Used</TD>"+@CRLF
$Data = $Data + "<TD width=100 bgcolor=gray> </TD>"+@CRLF
$Data = $Data + "<TD width=120>Avail-Space</TD>"+@CRLF
$Data = $Data + "</TR>"+@CRLF
$Data = WriteLine($Handle, $Data)
$Close = Close($Handle)
EndIf
EndFunction

Function html($server,$drive,$Free1,$Free0,$Free2)
Dim $Handle, $Open, $Data, $Close
$Handle = FreeFileHandle()
If $Handle > 0
$Open = Open($Handle,".\resserv.html",5)
$Data = "<Table border=0>"+@CRLF
$Data = $Data + "<TR>"+@CRLF
$Data = $Data + '<TD width=120>'+$server+':</TD>'+@CRLF
$Data = $Data + '<TD width=100>Drive '+$drive+' '+$free1+' %</TD>'+@CRLF
$Data = $Data + "<TD width="+$free0+" bgcolor=red> </TD>"+@CRLF
$Data = $Data + "<TD width="+$Free2+" bgcolor=green></TD>"+@CRLF
$Data = $Data + '<TD width=120>'+$Free1+'</TD>'+@CRLF
$Data = $Data + "<TR>"+@CRLF
$Data = WriteLine($Handle, $Data)
$Close = Close($Handle)
EndIf
EndFunction

Function offline($server)
Dim $Handle, $Open, $Data, $Close
$Handle = FreeFileHandle()
If $Handle > 0
$Open = Open($Handle,".\resserv.html",5)
$Data = "<Table border=0>"+@CRLF
$Data = $Data + "<TR>"+@CRLF
$Data = $Data + "<TD width=120>"+$server+":</TD>"+@CRLF
$Data = $Data + "<TD width=200>No WMI or server offline</font></TD>"+@CRLF
$Data = WriteLine($Handle, $Data)
$Close = Close($Handle)
EndIf
EndFunction

;*******
Function breakl()
Dim $Handle, $Open, $Data, $Close
$Handle = FreeFileHandle()
If $Handle > 0
$Open = Open($Handle,".\resserv.html",5)
$Data = WriteLine($Handle,'<hr width="100%" size="1" Color="#FFFFFF">'+@CRLF)
$Close = Close($Handle)
EndIf
EndFunction
;*************************************************
Function ftabl()
Dim $Handle, $Open, $Data, $Close
$Handle = FreeFileHandle()
If $Handle > 0
$Open = Open($Handle,".\resserv.html",5)
$Data = "</TR>"+@CRLF
$Data = $Data + "</Table>"+@CRLF
$Data = WriteLine($Handle, $Data)
$Close = Close($Handle)
EndIf
EndFunction
;*************************************************
Function finhtml()
Dim $Handle, $Open, $Data, $Close
$Handle = FreeFileHandle()
If $Handle > 0
$Open = Open($Handle,".\resserv.html",5)
$Data = '<hr width="100%" size="1" Color="#FFFFFF">'+@CRLF
$Data = $Data + "<br>"+@CRLF
$Data = $Data + "<br>"+@CRLF
$Data = $Data + "Edition: " + @DATE + " - at " + @TIME+" by "+@FULLNAME+@CRLF
$Data = $Data + "</body>"+@CRLF
$Data = $Data + "</html>"+@CRLF
$Data = WriteLine($Handle, $Data)
$Close = Close($Handle)
EndIf
EndFunction

Function GetDiskFreeSpace($sComputer,$Drive)
Dim $objWMIService, $colDisks, $objDisk
Dim $intFreeSpace, $intTotalSpace, $pctFreeSpace, $intUsed
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $sComputer + "\root\cimv2")
$colDisks = $objWMIService.ExecQuery('Select * from Win32_LogicalDisk Where DeviceID = '+"'"+$Drive+"'")
For Each $objDisk in $colDisks
$intFreeSpace = CDBL($objDisk.FreeSpace)
$intTotalSpace = CDBL($objDisk.Size)
$pctFreeSpace = FormatNumber(($intFreeSpace / $intTotalSpace * 100),2)
$intFreeSpace = FormatNumber($intFreeSpace,0,,-2)
$intUsed = CDBL(100) - $pctFreeSpace
Next
$GetDiskFreeSpace = $pctFreeSpace, $intUsed, $intFreeSpace
Exit @ERROR
EndFunction

Top
#135088 - 2006-08-29 06:18 AM Re: Generate html report on free space - servers
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Holly Crap Doc! You Rock!
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#135089 - 2006-08-29 06:22 AM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Thanks Benny,

Amazing what you can do while you're on hold with Microsoft on the phone.

Not 100% functional but at least you can run it and see what he's TRYING to do now.

Top
#135090 - 2006-08-29 03:59 PM Re: Generate html report on free space - servers
Jaide Offline
Fresh Scripter

Registered: 2003-03-05
Posts: 32
Thanks DOC - I appreciate the time and effort.
Top
#135091 - 2006-08-29 04:08 PM Re: Generate html report on free space - servers
Jaide Offline
Fresh Scripter

Registered: 2003-03-05
Posts: 32
WHOA!

The script runs much faster now. It used to take 4-5 minutes to process, now it's around a minute. The used space % and free space # reports the same values. The html code needs some work. Thanks for making this easier to read.

Top
#135092 - 2006-08-30 03:11 AM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
You're welcome. Yeah, it still needs work. If you do fix it please post your fixed version so we can all check it out.

If not, then maybe if I can find time, I'll look at fixing it - but no promises.

There were 3 big issues in the code overall (not including the html code)

1. Not reading the server names correctly
2. Not properly checking for WMI operation
3. Not calling UDFs with the correct parameters.

Glad you to be of assistance.

Top
#135093 - 2006-08-30 03:13 AM Re: Generate html report on free space - servers
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Oh... to give credit where credit is due.

This code snipet is from Glenn Barnas. I was trying to do it with Split but still be able to not call empty elements.
This code from Glenn is much easier to use in this situation I think.

Code:
$strComputer = SubStr($strComputer,3,InStr($strComputer,' ')-3)




And after all this my star value is still at 4 due to someone posting a low score. (only takes one, dumb board algorithm). But my rates are still only 34 - no one seems to use the rating even though it's in Shawn's signature.




Edited by NTDOC (2006-08-30 03:15 AM)

Top
#135094 - 2006-08-30 09:06 AM Re: Generate html report on free space - servers
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
If it is any consolation doc, I believe you deserve at least a 7 star rating ( *******)
Top
#135095 - 2006-08-31 12:45 AM Re: Generate html report on free space - servers
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, why does this thing require netview?
Top
Page 3 of 4 <1234>


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

Who's Online
0 registered and 918 anonymous users online.
Newest Members
rrosell, PatrickPinto, Raoul, Timothy, Jojo67
17877 Registered Users

Generated in 0.076 seconds in which 0.03 seconds were spent on a total of 12 queries. Zlib compression enabled.

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