Jump to content

Need to now how many elements returned


Datus
 Share

Recommended Posts

I have found a perfect bit of code example by GaryFrost shown here

$info = _DriveInfo(@ComputerName)
If IsArray($info) Then
    For $x = 1 To $info[0]
        MsgBox(0,"drive info",$info[$x])
    Next
EndIf


Func _DriveInfo($s_Machine = "localhost")
    Local Const $wbemFlagReturnImmediately = 0x10
    Local Const $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $objWMIService, $objItem, $SpaceTotal, $FreeSpace, $item
    Local $DriveType[7] = ["Unknown", "No Root Directory", "Removable Disk", "Local Disk", "Network Drive", "Compact Disc", "RAM Disk"]
    
    Local $Output = ""
    $objWMIService = ObjGet("winmgmts:\\" & $s_Machine & "\root\CIMV2")
    If @error Then
        MsgBox(16, "_DriveInfo", "ObjGet Error: winmgmts")
        Return
    EndIf
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If @error Then
        MsgBox(16, "_DriveInfo", "ExecQuery Error: SELECT * FROM Win32_LogicalDisk")
        Return
    EndIf
    If IsObj($colItems) Then
        For $objItem In $colItems
            $SpaceTotal = ($objItem.Size / 1024) / 1024
            If ($SpaceTotal > 1024) Then
                $SpaceTotal = Round($SpaceTotal / 1024, 0) & " GB"
            ElseIf ($SpaceTotal > 0) Then
                $SpaceTotal = Round($SpaceTotal, 0) & " MB"
            EndIf
            $FreeSpace = ($objItem.FreeSpace / 1024) / 1024
            If ($FreeSpace > 1024) Then
                $FreeSpace = Round($FreeSpace / 1024, 0) & " GB"
            ElseIf ($FreeSpace > 0) Then
                $FreeSpace = Round($FreeSpace, 0) & " MB"
            EndIf
            $item = "Drive: " & $objItem.Caption & "|" & "Type: " & $DriveType[$objItem.DriveType] & "|" & "Total Size: " & $SpaceTotal & "|" & "Free Space: " & $FreeSpace & "|" & "File System: " & $objItem.FileSystem
        Next
        Return StringSplit($item,"|")
    EndIf
EndFunc ;==>_DriveInfo

But only returns the last drive found.

I have tried to work out how you can find from

For $objItem In $colItems

how many items there are so i can create a dimension for

$item = "Drive: " & $objItem.Caption & "|" & "Type: " & $DriveType[$objItem.DriveType] & "|" & "Total Size: " & $SpaceTotal & "|" & "Free Space: " & $FreeSpace & "|" & "File System: " & $objItem.FileSystem

$item[] for example

Ive tried unbound but all i get is 0

Can anyone help please as this is something i want to use at work for servers from my desktop.

Here is a link to the original post.

http://www.autoitscript.com/forum/index.ph...n32_LogicalDisk

We live as we dream alone!

Link to comment
Share on other sites

Look here, all logical disks with a lot of informations, generated by Scriptomatic:

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Access: " & $objItem.Access & @CRLF
      $Output = $Output & "Availability: " & $objItem.Availability & @CRLF
      $Output = $Output & "BlockSize: " & $objItem.BlockSize & @CRLF
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $Output = $Output & "Compressed: " & $objItem.Compressed & @CRLF
      $Output = $Output & "ConfigManagerErrorCode: " & $objItem.ConfigManagerErrorCode & @CRLF
      $Output = $Output & "ConfigManagerUserConfig: " & $objItem.ConfigManagerUserConfig & @CRLF
      $Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF
      $Output = $Output & "DriveType: " & $objItem.DriveType & @CRLF
      $Output = $Output & "ErrorCleared: " & $objItem.ErrorCleared & @CRLF
      $Output = $Output & "ErrorDescription: " & $objItem.ErrorDescription & @CRLF
      $Output = $Output & "ErrorMethodology: " & $objItem.ErrorMethodology & @CRLF
      $Output = $Output & "FileSystem: " & $objItem.FileSystem & @CRLF
      $Output = $Output & "FreeSpace: " & $objItem.FreeSpace & @CRLF
      $Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
      $Output = $Output & "LastErrorCode: " & $objItem.LastErrorCode & @CRLF
      $Output = $Output & "MaximumComponentLength: " & $objItem.MaximumComponentLength & @CRLF
      $Output = $Output & "MediaType: " & $objItem.MediaType & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NumberOfBlocks: " & $objItem.NumberOfBlocks & @CRLF
      $Output = $Output & "PNPDeviceID: " & $objItem.PNPDeviceID & @CRLF
      $strPowerManagementCapabilities = $objItem.PowerManagementCapabilities(0)
      $Output = $Output & "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF
      $Output = $Output & "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF
      $Output = $Output & "ProviderName: " & $objItem.ProviderName & @CRLF
      $Output = $Output & "Purpose: " & $objItem.Purpose & @CRLF
      $Output = $Output & "QuotasDisabled: " & $objItem.QuotasDisabled & @CRLF
      $Output = $Output & "QuotasIncomplete: " & $objItem.QuotasIncomplete & @CRLF
      $Output = $Output & "QuotasRebuilding: " & $objItem.QuotasRebuilding & @CRLF
      $Output = $Output & "Size: " & $objItem.Size & @CRLF
      $Output = $Output & "Status: " & $objItem.Status & @CRLF
      $Output = $Output & "StatusInfo: " & $objItem.StatusInfo & @CRLF
      $Output = $Output & "SupportsDiskQuotas: " & $objItem.SupportsDiskQuotas & @CRLF
      $Output = $Output & "SupportsFileBasedCompression: " & $objItem.SupportsFileBasedCompression & @CRLF
      $Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      $Output = $Output & "VolumeDirty: " & $objItem.VolumeDirty & @CRLF
      $Output = $Output & "VolumeName: " & $objItem.VolumeName & @CRLF
      $Output = $Output & "VolumeSerialNumber: " & $objItem.VolumeSerialNumber & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_LogicalDisk" )
Endif


Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Thanks for reply, Ive seen the autoit scriptomatic etc and yes it looks good.

But I would still like to know how many items (drives) it has found.

Im an old school basic programer from the 80's rekindled the interest with autoit and dont understand windows handles etc.

The only way I can think of is either do an array of 25 (cant have anymore drives that that per server) or write the output to a file and read that.

Or is there another way.

We live as we dream alone!

Link to comment
Share on other sites

Ive looked at those commands and yes they are great but work on local drives only ie where the program is run.

I need to provide a name of a server which my program will ask it what servers its got.

I dont want to run an application on the servers.

We have about 400 or more of them atm.

Ouch.

We live as we dream alone!

Link to comment
Share on other sites

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


$DISK = 0
If IsObj($colItems) then
   For $objItem In $colItems
       $DISK += 1
      $Output = $Output & "Access: " & $objItem.Access & @CRLF
      $Output = $Output & "Availability: " & $objItem.Availability & @CRLF
      $Output = $Output & "BlockSize: " & $objItem.BlockSize & @CRLF
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $Output = $Output & "Compressed: " & $objItem.Compressed & @CRLF
      $Output = $Output & "ConfigManagerErrorCode: " & $objItem.ConfigManagerErrorCode & @CRLF
      $Output = $Output & "ConfigManagerUserConfig: " & $objItem.ConfigManagerUserConfig & @CRLF
      $Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF
      $Output = $Output & "DriveType: " & $objItem.DriveType & @CRLF
      $Output = $Output & "ErrorCleared: " & $objItem.ErrorCleared & @CRLF
      $Output = $Output & "ErrorDescription: " & $objItem.ErrorDescription & @CRLF
      $Output = $Output & "ErrorMethodology: " & $objItem.ErrorMethodology & @CRLF
      $Output = $Output & "FileSystem: " & $objItem.FileSystem & @CRLF
      $Output = $Output & "FreeSpace: " & $objItem.FreeSpace & @CRLF
      $Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
      $Output = $Output & "LastErrorCode: " & $objItem.LastErrorCode & @CRLF
      $Output = $Output & "MaximumComponentLength: " & $objItem.MaximumComponentLength & @CRLF
      $Output = $Output & "MediaType: " & $objItem.MediaType & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NumberOfBlocks: " & $objItem.NumberOfBlocks & @CRLF
      $Output = $Output & "PNPDeviceID: " & $objItem.PNPDeviceID & @CRLF
      $strPowerManagementCapabilities = $objItem.PowerManagementCapabilities(0)
      $Output = $Output & "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF
      $Output = $Output & "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF
      $Output = $Output & "ProviderName: " & $objItem.ProviderName & @CRLF
      $Output = $Output & "Purpose: " & $objItem.Purpose & @CRLF
      $Output = $Output & "QuotasDisabled: " & $objItem.QuotasDisabled & @CRLF
      $Output = $Output & "QuotasIncomplete: " & $objItem.QuotasIncomplete & @CRLF
      $Output = $Output & "QuotasRebuilding: " & $objItem.QuotasRebuilding & @CRLF
      $Output = $Output & "Size: " & $objItem.Size & @CRLF
      $Output = $Output & "Status: " & $objItem.Status & @CRLF
      $Output = $Output & "StatusInfo: " & $objItem.StatusInfo & @CRLF
      $Output = $Output & "SupportsDiskQuotas: " & $objItem.SupportsDiskQuotas & @CRLF
      $Output = $Output & "SupportsFileBasedCompression: " & $objItem.SupportsFileBasedCompression & @CRLF
      $Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      $Output = $Output & "VolumeDirty: " & $objItem.VolumeDirty & @CRLF
      $Output = $Output & "VolumeName: " & $objItem.VolumeName & @CRLF
      $Output = $Output & "VolumeSerialNumber: " & $objItem.VolumeSerialNumber & @CRLF
  Next
  MsgBox(0,"",$DISK)
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_LogicalDisk" )
Endif


Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

I like the $Disk += 1 not seen that before.

but ive worked it out run it twice.

$DISK = 0
If IsObj($colItems) then
   For $objItem In $colItems
       $DISK += 1
Next
Endif

$DISK = 0
Dim $Drive[$Disk]
If IsObj($colItems) then
   For $objItem In $colItems
       $DISK += 1
;code
;code
;code
$item[$DISK] = "Drive: " & $objItem.Caption & "|" & "Type: " & $DriveType[$objItem.DriveType] & "|" & "Total Size: " & $SpaceTotal & "|" & "Free Space: " & $FreeSpace & "|" & "File System: " & $objItem.FileSystem

Next
Endif

We live as we dream alone!

Link to comment
Share on other sites

I like the $Disk += 1 not seen that before.

but ive worked it out run it twice.

$DISK = 0
If IsObj($colItems) then
   For $objItem In $colItems
       $DISK += 1
Next
Endif

$DISK = 0
Dim $Drive[$Disk]
If IsObj($colItems) then
   For $objItem In $colItems
       $DISK += 1
;code
;code
;code
$item[$DISK] = "Drive: " & $objItem.Caption & "|" & "Type: " & $DriveType[$objItem.DriveType] & "|" & "Total Size: " & $SpaceTotal & "|" & "Free Space: " & $FreeSpace & "|" & "File System: " & $objItem.FileSystem

Next
Endif
It`s a good page at Language Reference - Operators in help file. :P

When the words fail... music speaks.

Link to comment
Share on other sites

never thought id program again after 1992 but i must say im finding more and more things I can do with autoit.

forum is very good and so is help (although for beginers could be a little better)

I will look at that Language Reference one day to see if there are any other snippets I could use for my main program I have at home to launch iso files.

We live as we dream alone!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...