I want to create a script that parses a log file (\\server\drive) and reports the total drive space, used space and free space. I'd like to mail the resulting html file in the script, but can use blat instead.
I'm not sure what I'm missing, but the script now outputs Select * From Win32_LogicalDisk Where DriveType=3 over and over again until I hit Ctrl+C. I changed the end Loop to Exit and the script ends, but only reads one line of the servers.txt file.
If it works on the first server in the list but not a subsequent one, I still contend it may be an issue with WMI on that particular server. Try hard coding each servername in a snippet of code as a test.
ja, if your still having problems after trying these suggestions, might want to re-post your latest code and include the "server list" file as well (or at least some of it anyway) ...
I commented out all lines and re-ran the script one line at a time. The completed normally, but only reported the disk space on the first server in servers.txt
Using $WMI = GetObject(“winmgmts\\” + $server + “\root\cimv2”) work locally when I substitute the servername in quotes, i.e. “server01”.
Most servers in my domain have hyphens.
I’m Domain Admin. If the script is executed from my system – it should run OK on all systems in the domain.
In your example, you have curly (smart) quotes. Please tell me you didn't copy paste that from your script! If the server names have hyphens, they should be in quotes. Just because the script works on one computer it doesn't mean it will work on all computers. If you have trailing spaces in your list, it will bite.
Break on ;this cmd is ok $ServerFile = "C:\admin\Servers.txt" ;this cmd is ok ;-------Information $ = RedirectOutput ("C:\admin\AvailDiskSpace.txt") ; file created in directory ;$nul = Open(1,"$ServerFile") ;nothing happened here ;$server = ReadLine(1) ;nothing happened here ;While $server <> "EndList" ; script froze here - had to Ctrl+C to escape ; $WMI = GetObject("winmgmts:\\" + $server + "\root\cimv2") @Error ; returned all zeros $WMI = GetObject("winmgmts:\\" + "dc-svr-01" + "\root\cimv2") @Error ; returned all zeros $HardDrives = $WMI.ExecQuery("Select * From Win32_LogicalDisk Where DriveType=3") ; returned all zeros For Each $Drive in $HardDrives ;returned all zeros ? " Drive Letter: " $Drive.Name ? " Drive Space: " $Drive.Size ? " Drive Free Space: " $Drive.FreeSpace ;looping on the first server in the list ? Next ;$server = ReadLine(1) Exit
$HardDrives = $WMI.ExecQuery("Select * From Win32_LogicalDisk Where DriveType=3") ; returned all zeros
For Each $Drive in $HardDrives ;returned all zeros
? " Drive Letter: " $Drive.Name
? " Drive Space: " $Drive.Size
? " Drive Free Space: " $Drive.FreeSpace ;looping on the first server in the list
?
Next
Well here is an overkill version that does the following.
1. Reads a list of systems from a file.
2. Pings it to determine if it is online or not
3. Checks if WMI is functioning correctly
4. Lists the hidden Admin shares excluding Admin$
5. Sorts the list of shares
6. Lists the free space of each hidden share
7. Does not limit to local hard drive as some SAN or other storage may not show as type 3 but the hidden Admin share still remains.
Example output dc-svr-01 using NetBIOS is not in WINS-DNS
fs-svr-01 using NetBIOS is not in WINS-DNS
ENIGMA C$ 188407332
Break On
Dim $SO,$Server,$Servers,$ValidSystem,$Share,$Item,$HS,$x,$File,$Report,$sComputer,$Send,$Results,$Results1,$Results2
Dim $Results3,$NB,$Arp,$Status
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
For Each $sComputer In $Servers
If $sComputer
$Send=0
$Results=Join(wshpipe('ping '+$sComputer+' -n 1',1),@CRLF)
If UBound(Split($sComputer,'.'))=3 ;IP was supplied from list
$NB='IP'
$Arp=0
$Results1=Trim(Split(Split(Split($Results,":")[4],"Received = ")[1],", Lost")[0])
Else ; NetBIOS name was supplied from list
$NB='NetBIOS'
$Arp=1
$Results1=Trim(Split(Split(Split($Results,":")[4],"Received = ")[1],", Lost")[0])
$Results2=Trim(Split(Split($Results,"[")[1],"]")[0])
$Results3=Trim(Split(Split($Results,"could")[1],"find")[0])
EndIf
If $Arp ; NetBIOS name was supplied from list
Select
Case $Results3
$Status=$sComputer+' using '+$NB+' is not in WINS-DNS'
? $Status
$Send=0
Case $Results2
If
$Results1 >0 $Status=$sComputer+' using '+$NB+' '+$Results2+' is responding to ping'
$Send=1
Else
$Status=$sComputer+' using '+$NB+' has WINS-DNS entry but is not responding.'
? $Status
$Send=0
EndIf
Case 1
$Status=$sComputer+' generated an unknown error '+ 'ERR: '+@ERROR ? $Status
$Send=0
EndSelect
Else ;IP was supplied from list
If $Results1 >0
$Status=$sComputer+' using '+$NB+' is responding to ping'
$Send=1
Else
$Status=$sComputer+' using '+$NB+' is NOT responding to ping'
? $Status
$Send=0
EndIf
EndIf
If $Send
If ConfirmWMI($sComputer)
$HS=ListHiddenShares($sComputer)
$HS=QS(Split(Join(Split($HS,CHR(42)),CHR(32)),CHR(47))) ;Sort the list using QS UDF
For $x=0 To UBound($HS)
If $x
? $sComputer + CHR(32) + $HS[$x]
EndIf
Next
EndIf
EndIf
EndIf
Next
$Report = RedirectOutput("")
Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
Dim $oExec, $Output
$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
If Not $NoEcho $Output Endif
$WshPipe=Split(Join(Split($Output,CHR(13)),CHR(32)),CHR(10))
Exit($oExec.ExitCode)
EndFunction
Function ListHiddenShares($sComputer)
Dim $objWMIService,$colShares,$objShare,$HiddenShare
If Not $sComputer $sComputer = @WKSTA EndIf
$sComputer = '\\' + $sComputer + '\'
$objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!" + $sComputer + "root\cimv2")
$colShares = $objWMIService.ExecQuery("Select * from Win32_Share")
For each $objShare In $colShares
If $objShare.Type = '-2147483648' And Not InStr($objShare.Name,'ADMIN$')
$HiddenShare = $HiddenShare + $objShare.Name + CHR(42) + Diskspace($sComputer+ $objShare.Name) + CHR(47)
EndIf
Next
$ListHiddenShares = $HiddenShare
EndFunction
Function Diskspace(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}!\\'+@WKSTA+'\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 ConfirmWMI(Optional $sComputer)
Dim $Computer,$Namespace,$WinDir,$Target,$WMIVer,$objWMIService,$WinDrive
If Not $sComputer $sComputer = @WKSTA EndIf
$sComputer = '\\' + $sComputer + '\'
$WinDir = $sComputer+Join(Split(ReadValue($sComputer+ 'HKLM\Software\Microsoft\Windows NT\CurrentVersion', 'SystemRoot'),':'),Chr(36))
If @ERROR Exit @ERROR EndIf
$Target = $WinDir + '\system32\wbem\wbemdisp.dll'
If Exist($Target)
$WMIVer = Trim(GetFileVersion($Target,'BinFileVersion'))
If @ERROR Exit @ERROR EndIf
Else
Exit 2
EndIf
$ConfirmWMI=$WMIVer
$Namespace = "root\cimv2"
If $WMIVer
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!" + $sComputer + $Namespace)
If @ERROR Exit Val("&"+Right(DecToHex(@ERROR),4))
Else
Exit 0
EndIf
EndIf
Exit 2
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 QS($a)
DIM $b[32],$c[32],$d,$e,$f,$g,$h,$i,$j,$k,$l
$b[0]=0
$c[0]=Ubound($a)
$d=0
While $d >=0
$e=$b[$d]
$f=$c[$d]
While $e < $f
$h=$e+($f-$e)/2
$k=$a[$e]
$A[$e]=$A[$h]
$A[$h]=$k
$i=$e+1
$j=$f
$l=0
Do
While ($i<$j) And $A[$e] > $A[$i]
$i=$i+1
Loop
While ($j>=$i) And $A[$j] > $A[$e]
$j=$j-1
Loop
IF $i>=$j
$l=1
Else
$k=$A[$i]
$A[$i]=$A[$j]
$A[$j]=$k
$j=$j-1
$i=$i+1
EndIf
Until $l=1
$k=$a[$e]
$a[$e]=$a[$j]
$a[$j]=$k
$g=$j
If $g-$e <= $f - $g
If $g+1 < $f
$b[$d]=$g+1
$c[$d]=$f
$d=$d+1
EndIf
$f=$g-1
Else
If $g-1 > $e
$b[$d]=$e
$c[$d]=$g-1
$d=$d+1
EndIf
$e=$g+1
EndIf
Loop
$d=$d-1
Loop
$qs=$a
EndFunction
Nice. I've come across another scripts that outputs data into an html format. It list server_name, disk_name, and free_space. I'd like to add a column that list the total disk space on each drive listed in disk_name. I've tried to use GetDiskSpace, but that kept returning an error. Any ideas?
I did see this code listed in the manual but this code already exists in the script. Code:
$WMI = GetObject("winmgmts:\\" + @WKSTA + "\root\cimv2") $HardDrives = $WMI.ExecQuery("Select * From Win32_LogicalDisk Where DriveType=3") For Each $Drive in $HardDrives
Can I have this in the script twice withour causing any errors?
ERROR : invalid method/function call: too many parameters! Script: C:\servspace_beta1.kix Line : 57
Here is the code -
Code:
For Each $Disk In $DiskSet $co=$co+1 $dname=$Disk.Name $percentfree=Round(DISKSPACE($strComputer,$dname,5)) $percentused=100-$percentfree $rest=$percentfree-5 html($strComputer,$dname,$percentused,$rest,$space)
I added the $space variable but am unsure how to get the script to recognize that variable.
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 html($strComputer,$dname,$percentused,$rest,$space)
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.
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
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
"+@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 , "
Server
"+@CRLF) $x = WriteLine( 3 , "
Used
"+@CRLF) $x = WriteLine( 3 , "
"+@CRLF) $x = WriteLine( 3 , "
Avail-Space
"+@CRLF) $x = WriteLine( 3 , "
"+@CRLF) $=Close (3)
EndFunction ;************************************************* Function html($server,$drive,$percentfree,$rest,$free)
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
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<>""
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.
Then to make your file write faster you can change your WriteLine to something like this (untested in working code)
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.
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.
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
"+@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' : (
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
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.
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.
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.
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.
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.
Multiple switches can be specified, separate each with a space.
/NTW will include NT/2000/XP workstations. /NTS will include NT/2000/.NET servers. /PDC will include NT/2000/.NET primary domain controllers. /BDC will include NT backup domain controllers. /PRINT will include Print queue servers. /RAS will include Remote Access Servers. /SQL will include Microsoft SQL Servers. /TIME will include all Time sources. /TS will include all Terminal Servers. /9x will include Windows 95/98/ME systems. (Only if sharing has been enabled.) /HEX: allows you to specify your own bit mask. Use /HEX: /? for more details.
/DOMAIN: allows you to specify a different domain or workgroup. /T also displays the primary machine type (Server, Workstation, PDC, etc.) /B displays the output in bare format (useful when scripting) /TABS separates columns with tabs instead of spaces (useful when importing)
An argument of /? or -? displays this syntax and always returns 1. A successful completion will return 0.
I have a problem with numbers under 1, i.e. 0.9. The script computes those numbers to 15 decimal places. I used the ROUND() command for numbers above 1.0 - seems to work pretty well. Any idea how to limit the amount of decimal spaces?
DOC - I haven't had any luck trying to re-write the script. I can't get numbers to reconcile. Not sure if I'm using the correct variables in the script. If you know of anywhere I can get html help let me know.