Synapsee

Remote\Distant Drive Information

1 post in this topic

sample1.jpg.625e1779985bd660eb2346736a42

sample2.jpg.d08d57e5ae1f98f2f7806c88e18b

 

;RemoteDriveInfo
;v0.0.0.1

;Ressource
;Smart Temp : https://www.autoitscript.com/forum/topic/91067-drive-temperature/?do=findComment&comment=1091961
;GetPNPDeviceID From DriveLetter : https://www.autoitscript.com/forum/topic/57202-need-help-get-pnp-device-id-for-usb-drive/?do=findComment&comment=433346
;Get Disk Space Info : https://www.autoitscript.com/forum/topic/148390-wmi-query-how-to-get-data-from-object/?do=findComment&comment=1055088
;For A to Z : https://www.autoitscript.com/forum/topic/4804-fornext-loop-with-letters/?do=findComment&comment=30417

;Include
#include <Array.au3>
#include <String.au3>





;Sample 1

;$sComputerName
$sComputerName = "PC1"
;$sComputerName = "PC2"
;$sComputerName = @ComputerName

;$sDrive
;$sDrive = "system"
;$sDrive = "D:"
$sDrive = ""

$DriveInfo = _GetDriveInfo($sComputerName, $sDrive)
_ArrayDisplay($DriveInfo)





;Sample 2

$DriveInfo = _GetDriveInfo($sComputerName, "system")
If $DriveInfo[0][0] = "DriveLeter" Then
    If $DriveInfo[1][6] <> "" Then
        MsgBox(0, "", $DriveInfo[1][6] & "°  -  " & $DriveInfo[1][4] & "%  ( " & Round($DriveInfo[1][2]/1024^3) & "Go / " & Round($DriveInfo[1][3]/1024^3) & "Go )" & @CRLF)
    Else
        MsgBox(0, "", $DriveInfo[1][4] & "%  ( " & Round($DriveInfo[1][2]/1024^3) & "Go / " & Round($DriveInfo[1][3]/1024^3) & "Go )" & @CRLF)
    EndIf
Else
    MsgBox(0, "", $DriveInfo[0][0] & @CRLF)
EndIf





;Function
Func _GetSystemDriveLtr($sComputerName)

    $objWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputerName & "\root\cimv2")
    $colTemp = $objWMI.ExecQuery("SELECT SystemDrive FROM Win32_OperatingSystem")
    If IsObj($colTemp) then
        For $objItem In $colTemp
            $sSystemDriveLtr = $objItem.systemdrive
        Next
    EndIf
    Return $sSystemDriveLtr

EndFunc ;==> _GetSystemDriveLtr
Func _GetPNPDeviceID($drive_letter, $sComputerName)
    Local $drive_letter_found, $drive_letter_found
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""

    $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDiskToPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $LogicalDiskToPartitionAntecedent = _StringBetween($objItem.Antecedent, '"', '"')
            $LogicalDiskToPartitionDependent = _StringBetween($objItem.Dependent, '"', '"')
            ;ConsoleWrite(@CR & $LogicalDiskToPartitionAntecedent[0] & " - " & $LogicalDiskToPartitionDependent[0])
            $drive_statistics = $LogicalDiskToPartitionAntecedent[0]
            $drive_letter_found = $LogicalDiskToPartitionDependent[0]
            If $drive_letter = $drive_letter_found Then
                ExitLoop
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_LogicalDiskToPartition")
    EndIf
    If $drive_letter <> $drive_letter_found Then Return 0 ; If drive letter isn't function returns 0

    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDriveToDiskPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $DiskDriveToDiskPartitionAntecedent = _StringBetween($objItem.Antecedent, '"', '"')
            $DiskDriveToDiskPartitionDependent = _StringBetween($objItem.Dependent, '"', '"')
            ;ConsoleWrite(@CR & $DiskDriveToDiskPartitionAntecedent[0] & " - " & $DiskDriveToDiskPartitionDependent[0])
            $drive_statistics_found = $DiskDriveToDiskPartitionDependent[0]
            $drive_physical = StringTrimLeft($DiskDriveToDiskPartitionAntecedent[0], StringInStr($DiskDriveToDiskPartitionAntecedent[0], "\", 1, -1))
            ;MsgBox(0,"TEST", $drive_physical)
            If $drive_statistics = $drive_statistics_found Then
                ExitLoop
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_DiskDriveToDiskPartition")
    EndIf

    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            ;MsgBox(0,324234, $objItem.DeviceID)
            $DeviceID = StringTrimLeft($objItem.DeviceID, StringInStr($objItem.DeviceID, "\", 1, -1))
            $PNPDeviceID = $objItem.PNPDeviceID
            ;MsgBox(0,122, $DeviceID)
            If $drive_physical = $DeviceID Then
                Return $PNPDeviceID
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_DiskDrive")
    EndIf
EndFunc ;==> _GetPNPDeviceID
Func _GetSmartTemp($InstanceName, $sComputerName)

    $objWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputerName & "\root\WMI")
    $colTemp = $objWMI.ExecQuery("SELECT * FROM MSStorageDriver_ATAPISmartData")
    $s = ""

    If IsObj($colTemp) and $InstanceName <> "" then
        For $objItem In $colTemp
            If StringInStr(StringUpper($objItem.InstanceName), StringUpper($InstanceName)) <> 0 Then
                $strVendorSpecific = $objItem.VendorSpecific
                For $i = 2 To UBound($strVendorSpecific) - 1 Step 12
                    If $strVendorSpecific[$i] = 0xC2 Then; find Vendor Parameter #194 - Temperatur
                        $s &= " " & $strVendorSpecific[$i + 5]; Raw/Value celsuis
                        ContinueLoop 2
                    EndIf
                Next
                For $i = 2 To UBound($strVendorSpecific) - 1 Step 12
                    If $strVendorSpecific[$i] = 0xBE Then; find Vendor Parameter #190 - Airflow Temperatur (if no tempetature is found)
                        $s &= " " & $strVendorSpecific[$i + 5]; Raw/Value celsuis
                        ContinueLoop 2
                    EndIf
                Next
            EndIf
        Next
        $s = StringTrimLeft($s, 1)
    Else
        $s = ""
    EndIf

    Return $s

EndFunc ;==> _GetSmartTemp

;MainFunction
Func _GetDriveInfo($sComputerName, $sDrive = "")

    Local $DriveArray[1][7] = [["DriveLeter", "FreespaceByte", "UsedspaceByte", "SizeByte", "UsedPercent", "IsSystem", "Temp"]]

    If Ping($sComputerName) = 0 Then
        Local $DriveArray[1][1]
        $DriveArray[0][0] = $sComputerName & " seems not online"
        Return $DriveArray
    EndIf

    If $sDrive = "" Then
        For $i = 65 To 90;For A to Z
            $sDriveLtr = Chr($i)&":"
            $DriveArray = _GetOneDriveInfo($sComputerName, $sDriveLtr, $DriveArray)
        Next
    ElseIf $sDrive = "system" Then
        $DriveArray = _GetOneDriveInfo($sComputerName, _GetSystemDriveLtr($sComputerName), $DriveArray)
    Else
        $DriveArray = _GetOneDriveInfo($sComputerName, $sDrive, $DriveArray)
    EndIf

    Return $DriveArray

EndFunc ;==> _GetDriveInfo
Func _GetOneDriveInfo($sComputerName, $sDriveLtr, $DriveArray)

    $objWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputerName & '\root\cimv2:Win32_LogicalDisk="' & $sDriveLtr & '"')
    If IsObj($objWMI) Then

        $UsedPercent = Round(100 / $objWMI.Size * ($objWMI.Size - $objWMI.FreeSpace))

        If $UsedPercent <> -9223372036854775807 then

            $current_line = Ubound($DriveArray)+1
            ReDim $DriveArray[$current_line][7]

            $DriveArray[$current_line-1][0] = $sDriveLtr

            $FreespaceByte = $objWMI.FreeSpace
            $DriveArray[$current_line-1][1] = $FreespaceByte

            $SizeByte = $objWMI.Size
            $DriveArray[$current_line-1][3] = $SizeByte

            $UsedspaceByte = $objWMI.Size - $objWMI.FreeSpace
            $DriveArray[$current_line-1][2] = $UsedspaceByte

            $UsedPercent = Round(100 / $objWMI.Size * ($objWMI.Size - $objWMI.FreeSpace))
            $DriveArray[$current_line-1][4] = $UsedPercent

            If _GetSystemDriveLtr($sComputerName) = $sDriveLtr Then
                $IsSystem = 1
            Else
                $IsSystem = 0
            EndIf
            $DriveArray[$current_line-1][5] = $IsSystem

            $InstanceName = _GetPNPDeviceID($sDriveLtr, $sComputerName)
            $Temp = _GetSmartTemp($InstanceName, $sComputerName)
            $DriveArray[$current_line-1][6] = $Temp

        EndIf

    EndIf

    Return $DriveArray

EndFunc

 

Ressource


Smart Temp :

GetPNPDeviceID From DriveLetter :

Get Disk Space Info :

For A to Z :

 

Share this post


Link to post
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

  • Similar Content

    • ModemJunki
      By ModemJunki
      Hello,
      In Windows 10 PowerShell, one can do this to change the metric for a NIC in Windows 10:
      Get-NetAdapter | Where-Object -FilterScript {$_.InterfaceAlias -Eq "Ethernet 2"} | Set-NetIPInterface -InterfaceMetric 2 I know I can script the above PowerShell line (and it works!), but I wanted to try something I hadn't done before after looking into jguinch's most excellent Network configuration UDF. I wanted to make use of the SetIPConnectionMetric method in the WMI classes. There is an example VBscript here but this is not for Windows 10. Using AutoIT would also give better control over capturing error return codes than with PowerShell.
      But I cannot get my script to work! The return from SetIPConnectionMetric() is 0, which would indicate success. Yet the change does not happen. I also tried WMI methods using .put_ but this fails.
      Anyone more experienced than I have ideas to make this work?
      #RequireAdmin _SetNicInterfaceMetric2("Ethernet 2", "2") Func _SetNicInterfaceMetric2($NIC_NAME, $METRIC) Local $s_setIndx = 0 $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") $colNICItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & $NIC_NAME & "'", "WQL") If IsObj($colNICItems) Then For $objItem In $colNICItems $s_nicIndex = $objItem.Index Next ConsoleWrite("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'" & @CRLF) $colNAC = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'", "WQL") If IsObj($colNAC) Then For $objNetCard In $colNAC If $METRIC <> $objNetCard.IPConnectionMetric Then ConsoleWrite("Metric was set to " & $objNetCard.IPConnectionMetric & ". Setting to " & $METRIC & "." & @CRLF) $s_isSet = $objNetCard.SetIPConnectionMetric($METRIC) ConsoleWrite("SetIPConnectionMetric Result = " & $s_isSet & @CRLF) Else ConsoleWrite("Metric is already set to " & $METRIC & @CRLF) EndIf Next EndIf EndIf EndFunc ;==>_SetNicInterfaceMetric2  
    • AndyS19
      By AndyS19
      I have code that does a WMI SQL query to find all defined printers, and I want to parse the returned object in several places.  However, after parsing it the first time, all other times fail to find any printer objects.
      Here is my test code:
      test() Func test() Local $oPrinters, $oPrinter, $err, $cnt, $oP, $query $query = "SELECT * FROM Win32_Printer" $oPrinters = doQuery($query) $err = @error LogMsg("+++: $err = " & $err & ", isObj($oPrinters) = " & IsObj($oPrinters)) If ($err == 0) Then LogMsg("FIRST LOOP") ; <=== FIRST LOOP $cnt = 0 $oP = $oPrinters LogMsg("+++: isObj($oP) = " & IsObj($oP)) For $oPrinter In $oP $cnt += 1 LogMsg("+++: isObj($oPrinter): " & IsObj($oPrinter) & ", $oPrinter.Name ==>" & $oPrinter.Name & "<==") Next LogMsg("+++: Found " & $cnt & " printers") LogMsg("SECOND LOOP") ; <== SECOND LOOP $cnt = 0 $oP = $oPrinters LogMsg("+++: isObj($oP) = " & IsObj($oP)) For $oPrinter In $oP $cnt += 1 LogMsg("+++: isObj($oPrinter): " & IsObj($oPrinter) & ", $oPrinter.Name ==>" & $oPrinter.Name & "<==") Next LogMsg("+++: Found " & $cnt & " printers") EndIf EndFunc ;==>test Func doQuery($sQuery, $lnum = @ScriptLineNumber) #forceref $lnum LogMsg("+++:" & $lnum & ": doQuery(" & '"' & $sQuery & '"' & ") entered") Local $oWMIService, $oResults, $errstr Local $wbemFlags = BitOR(0x20, 0x10) ; $wbemFlagReturnImmediately and wbemFlagForwardOnly $oWMIService = ObjGet("winmgmts:\\" & "localhost" & "\root\CIMV2") If (IsObj($oWMIService)) Then $oResults = $oWMIService.ExecQuery($sQuery, "WQL", $wbemFlags) If (IsObj($oResults)) Then LogMsg("+++: doQuery() returns @error = 0, Good: returning the object") Return (SetError(0, 0, $oResults)) ;;; Good: return the object Else $errstr = "" _ & "WMI Query failed." & @CRLF _ & "This is the query:" & @CRLF _ & " " & $sQuery LogMsg("+++: ====>" & $errstr & "<===") LogMsg("+++: doQuery() returns @error = 1") Return (SetError(1, 0, $errstr)) ; Error: Query faled EndIf Else $errstr = "" _ & "WMI Output" & @CRLF _ & "No WMI Objects Found for class: " & @CRLF _ & "Win32_PrinterDriver" & @CRLF _ & "using this query:" & @CRLF _ & " " & $sQuery LogMsg("+++: ====>" & $errstr & "<===") MsgBox(0, "ERROR", $errstr) ; Error: Cannot get $oWMIService object Exit (1) EndIf EndFunc ;==>doQuery Func LogMsg($msg, $lnum = @ScriptLineNumber) ConsoleWrite("+++:" & $lnum & ": " & $msg & @CRLF) EndFunc ;==>LogMsg Parsing the returned $oPrinters object shows 5 printers:
      +++:15: FIRST LOOP +++:18: +++: isObj($oP) = 1 +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Microsoft XPS Document Writer<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Microsoft Office Document Image Writer<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Fax<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Canon MG7100 series Printer WS<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Canon MG6100 series Printer WS<== +++:24: +++: Found 5 printers Parsing it again, shows no printers:
      +++:26: SECOND LOOP +++:29: +++: isObj($oP) = 1 +++:35: +++: Found 0 printers  
    • jguinch
      By jguinch
      Hello.
      I did create these few functions several months ago. I post here, if it can interest someone.
      These functions based on WMI queries allow you to manage printers : add / delete printer, driver, port, or obtain configuration, set default printer ... I let you discover it with the code.

       
      Here is the list of the available functions :
      _Printmgr_AddLocalPort
      _Printmgr_AddLPRPort
      _PrintMgr_AddPrinter
      _PrintMgr_AddPrinterDriver
      _PrintMgr_AddTCPIPPrinterPort
      _PrintMgr_AddWindowsPrinterConnection
      _PrintMgr_CancelAllJobs
      _Printmgr_EnumPorts
      _PrintMgr_EnumPrinter
      _PrintMgr_EnumPrinterConfiguration
      _PrintMgr_EnumPrinterDriver
      _PrintMgr_EnumPrinterProperties
      _PrintMgr_EnumTCPIPPrinterPort
      _Printmgr_Pause
      _Printmgr_PortExists
      _Printmgr_PrinterExists
      _Printmgr_PrinterSetComment
      _Printmgr_PrinterSetDriver
      _Printmgr_PrinterSetPort
      _Printmgr_PrinterShare
      _Printmgr_PrintTestPage
      _PrintMgr_RemoveLocalPort
      _PrintMgr_RemoveLPRPort
      _PrintMgr_RemovePrinter
      _PrintMgr_RemovePrinterDriver
      _PrintMgr_RemoveTCPIPPrinterPort
      _PrintMgr_RenamePrinter
      _Printmgr_Resume
      _PrintMgr_SetDefaultPrinter
       
      And some examples :
      #Include "PrintMgr.au3" ; Remove a printer called "My old Lexmark printer" : _PrintMgr_RemovePrinter("My old Lexmark printer") ; Remove the driver called "Lexmark T640" : _PrintMgr_RemovePrinterDriver("Lexmark T640") ; Remove the TCP/IP printer port called "TCP/IP" _PrintMgr_RemoveTCPIPPrinterPort("MyOLDPrinterPort") ; Add a driver, called "Samsung ML-451x 501x Series", and driver inf file is ".\Samsung5010\sse2m.inf" _PrintMgr_AddPrinterDriver("Samsung ML-451x 501x Series", "Windows NT x86", @scriptDir & "\Samsung5010", @scriptDir & "\Samsung5010\sse2m.inf") ; Add a TCP/IP printer port, called "MyTCPIPPrinterPort", with IPAddress = 192.168.1.10 and Port = 9100 _PrintMgr_AddTCPIPPrinterPort("MyTCPIPPrinterPort", "192.168.1.10", 9100) ; Add a printer, give it the name "My Printer", use the driver called "Samsung ML-451x 501x Series" and the port called "MyTCPIPPrinterPort" _PrintMgr_AddPrinter("My Printer", "Samsung ML-451x 501x Series", "MyTCPIPPrinterPort") ; Set the printer called "My Printer" as default printer _PrintMgr_SetDefaultPrinter("My Printer") ; Connect to the shared printer "\\192.168.1.1\HPDeskjetColor") _PrintMgr_AddWindowsPrinterConnection("\\192.168.1.1\HPDeskjetColor") ; List all installed printers #Include <Array.au3> $aPrinterList = _PrintMgr_EnumPrinter() _ArrayDisplay($aPrinterList) ; List all printers configuration #Include <Array.au3> $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration() _ArrayDisplay($aPrinterConfig) ; List all installed printer drivers #Include <Array.au3> $aDriverList = _EnumPrinterDriver() _ArrayDisplay($aDriverList) ; Retrieve the printer configuration for the printer called "Lexmark T640" #Include <Array.au3> $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration("Lexmark T640") _ArrayDisplay($aPrinterConfig) ; Add a local printer port (for a file output) _AddLocalPrinterPort("c:\temp\output.pcl") ; Remove the local port _RemoveLocalPrinterPort("c:\temp\output.pcl") Download link :  PrintMgr.au3
    • jguinch
      By jguinch
      Here is an UDF for managing printers.
      Features are :
       - add, remove or rename a printer
       - add or remove a driver
       - add or remove a TCP/IP printer port
       - add or remove a LPR printer port
       - connect to a remote printer
       - enum printers and there configuration and properties
       - pause resume or cancel all jobs of a printer
       - checks if a printer exists
       - print a test page
       - set the default printer
    • ripdad
      By ripdad
      This function (version 1), was developed solely for applications I needed it for.
      It was only tested with Class names that was needed in those scripts.
      And so, I didn't want to release it by itself because I knew it needed some more
      work before I did that.
      Recently, I delved back into it after several years and am now releasing version 2,
      which was mostly rewritten over the past several weeks.
      What does it do? It retrieves the properties and values pertaining to your computer,
      operating system and devices. All you need is a valid Win32 Class Name.
      So, here it is -- let me know if you have any issues!
      Updated to v2.25, January 14, 2017
      Download: _WMI_InstancesOf_v2.25.au3