Jump to content

Finding Unique hardware ID and lock Windows OS


Recommended Posts

Hi,

Does anyone knows why this command doesn't work?

Run(@ComSpec & " /c " & "devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)", "", "")

This is the command in commandline:

devcon.exe hwids * | findstr  /R "1050&PID_0114&REV_0340&MI_01" && (echo USB Device is connected) || (rundll32.exe user32.dll, LockWorkStation)

This works, but i want to convert this to AutoIT, but it doesn't work.

 

In short what i am trying to accomplish is that a script checks every 1 sec. if there is an USB device plugged in and if not ( so it is removed ) it will lock Windows OS.

Can someone help me with this?

Link to post
Share on other sites
  • Replies 41
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

  • Moderators

@Iznogoud if you change the /c to /k it will keep the command line window open - what is the error telling you?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites
1 minute ago, JLogan3o13 said:

@Iznogoud if you change the /c to /k it will keep the command line window open - what is the error telling you?

I changed the code into

Run(@ComSpec & " /k " & "devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)", "", "")

But the same happens as /c

Link to post
Share on other sites
  • Moderators

@Iznogoud again, "it doesn't work" doesn't help us much. My guess is a quotes issue, but you need to help us with what error you are seeing, or what the behavior is when you run the script? Help us help you ;)

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites
1 minute ago, JLogan3o13 said:

@Iznogoud again, "it doesn't work" doesn't help us much. My guess is a quotes issue, but you need to help us with what error you are seeing, or what the behavior is when you run the script? Help us help you ;)

 

It is hard to tell you what the output is, because i don't see anything. Script runs ">Exit code: 0    Time: 0.3422" and that is it.

I think it is a problem then i am using findstr inside a @ComSpec or maybe the & characters, but i don't know.

I don't see any command window either.

Link to post
Share on other sites

Now it seems to work.... Or i am stupid or something with my variables are wrong, but entering the path to devcon.exe does helps alot.

Run(@ComSpec & " /c " & "C:\Test\devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)")

The only problem now is, it shows a command prompt window.

When i use this code:

Run(@ComSpec & " /c " & "C:\Test\devcon.exe hwids * | findstr  /R ""1050&PID_0114&REV_0340&MI_01"" || (rundll32.exe user32.dll, LockWorkStation)", @SW_HIDE)

It shows a Exit code: 0 and a time, but nothing happens.

 

Edit
Maybe it is not the best way to check if the hardware ID is available, maybe using WMI ? This because it runs every sec. to check if the USB device is connected.

Edited by Iznogoud
Link to post
Share on other sites

Well i had a problem shord time ago. 

 

This is probably same as me. 

 

The dos windows have some encoding issue. You probably run into one of them.

 

 

What happen if you manage the scite encoding like this : 

You write your code, Then in SCITE you change that setting : 

File/Encoding/Code Page Property

 

Then you run the code again 

If you got that issue it will now work.

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to post
Share on other sites

What are you expecting for an output? Because from just looking at the code, all it does is pipe the HWIDs to the Findstr program to look for one in particular, then locks the computer. There's no actual display output if nothing is found.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites
16 minutes ago, BrewManNH said:

What are you expecting for an output? Because from just looking at the code, all it does is pipe the HWIDs to the Findstr program to look for one in particular, then locks the computer. There's no actual display output if nothing is found.

What i want to accomplish is it to check if there is a HWID in the system, if yes, do nothing, if no then lock the computer / Windows. I needs to run in the background.

Link to post
Share on other sites

So what about the encoding ?

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to post
Share on other sites
47 minutes ago, caramen said:

Well i had a problem shord time ago. 

 

This is probably same as me. 

 

The dos windows have some encoding issue. You probably run into one of them.

 

 

What happen if you manage the scite encoding like this : 

You write your code, Then in SCITE you change that setting : 

File/Encoding/Code Page Property

 

Then you run the code again 

If you got that issue it will now work.

It was already on "File/Encoding/Code Page Property"

Link to post
Share on other sites

got it working

move devcon.exe to your script folder, compile your script as a CUI x86, run as an admin

script should contain 

Run(@comspec & " /c devcon.exe hwids * > test.txt")

Use autoit filereadline  to search for the string in test.txt

use autoit _WinAPI_LockWorkStation ( )

Link to post
Share on other sites
1 hour ago, Nine said:

got it working

move devcon.exe to your script folder, compile your script as a CUI x86, run as an admin

script should contain 

Run(@comspec & " /c devcon.exe hwids * > test.txt")

Use autoit filereadline  to search for the string in test.txt

use autoit _WinAPI_LockWorkStation ( )

Sending it to a file every second and doing a filereadline is more resourcefull then it should be.

If i search for more info on getting hardware ID's, i think there should be a better solution, but then again, i am not that good with it yet to tell how to get it to work.

Link to post
Share on other sites

How about piping all of the devcon output to an array and then searching inside the array? That is what I do... I can't remember where I found this code though, so I can't credit the original author.

#include <File.au3>

Global $path_Devcon = @ScriptDir & '\devcon.exe'

$find = _GetFromDevcon('"' & $path_Devcon & '" findall *') ; the findall * means we will see "non present devices"

_arraydisplay($find)

; --------------------------------------------------
;~ Function Name:   _GetFromDevcon(path and string to use for devcon)
;~ Description:     Makes an array of devices according to the devcon string
; --------------------------------------------------
Func _GetFromDevcon($strRun)
    Local $htreere = 'HTREE\'
    Local $legacyre = 'ROOT\LEGACY'
    Local $vssre = 'STORAGE\VOLUMESNAPSHOT'
    Local $swre = 'SW\'
    Local $data, $dvcn = Run("cmd.exe", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    StdinWrite($dvcn, $strRun & @CRLF)
    StdinWrite($dvcn)
    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd
    Local $aData = StringSplit($data, @CRLF, 3)

    Local $outdata[1]
    For $i = 0 To UBound($aData, 1) - 1
        $chkstring = $aData[$i]
        If Not StringInStr($chkstring, $legacyre) Then ;
            If Not StringInStr($chkstring, $htreere) Then
                If Not StringInStr($chkstring, $vssre) Then
                    If Not StringInStr($chkstring, $swre) Then
                        _ArrayAdd($outdata, $aData[$i])
                    EndIf
                EndIf
            EndIf
        EndIf
    Next
    Return $outdata
EndFunc   ;==>_GetFromDevcon

Also have a look at what Kylomas helped me with a long time back.

 

 

Always carry a towel.

Link to post
Share on other sites

Alright, that should be closer of what you want : tested with a string in my devcon

Read my previous post, for compiling and stuff....

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <WinAPISys.au3>

Opt ("MustDeclareVars", 1)

   Local $data
   Local $dvcn = Run(@comspec & ' /c devcon.exe hwids * | findstr "PCI\VEN_8086&DEV_0C01&SUBSYS_85341043&REV_06"', _ 
          @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd


  ;  Msgbox (0,"",$data)
    

  if $data <> "" then _WinAPI_LockWorkStation()

 

Link to post
Share on other sites
1 hour ago, ModemJunki said:

I wonder if we can do this with WMI. I don't have a Yubikey so I don't know where the key enumerates in the WMI data.

It looks like it was done before with older Yubikeys. https://forum.yubico.com/viewtopice4bc.html?f=35&t=260

 

Regardless if it is a yubikey, you can even do it with a USB mouse or USB Storage device. Everything has its unique ID's.

Link to post
Share on other sites
2 minutes ago, Iznogoud said:

Regardless if it is a yubikey, you can even do it with a USB mouse or USB Storage device. Everything has its unique ID's.

What to select FROM in the query (for a Yubikey), is what I don't know. The only reason I think of WMI is to eliminate need for devcon executable.

Always carry a towel.

Link to post
Share on other sites
1 minute ago, ModemJunki said:

What to select FROM in the query (for a Yubikey), is what I don't know. The only reason I think of WMI is to eliminate need for devcon executable.

If you can use the hardware ID, then it doesn't matter if i am correct?

To be using devcon.exe is not a problem. I could embed that with the fileinstall function.

Link to post
Share on other sites
2 hours ago, Nine said:

Alright, that should be closer of what you want : tested with a string in my devcon

Read my previous post, for compiling and stuff....

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <WinAPISys.au3>

Opt ("MustDeclareVars", 1)

   Local $data
   Local $dvcn = Run(@comspec & ' /c devcon.exe hwids * | findstr "PCI\VEN_8086&DEV_0C01&SUBSYS_85341043&REV_06"', _ 
          @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd


  ;  Msgbox (0,"",$data)
    

  if $data <> "" then _WinAPI_LockWorkStation()

 

I used your code and  changed the Hardware ID like this:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <WinAPISys.au3>

Opt ("MustDeclareVars", 1)

   Local $data
   Local $dvcn = Run(@comspec & ' /c devcon.exe hwids * | findstr "HID\VID_1050&PID_0114&REV_0340&MI_01"', _
          @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

    While True
        $data &= StdoutRead($dvcn)
        If @error Then ExitLoop
        Sleep(25)
    WEnd


    Msgbox (0,"",$data)


  ;if $data <> "" then _WinAPI_LockWorkStation()

The messagebox is showing the Example.png image

So if i am correct it should NOT lock my workstation, but it does.... Why is that?

<> "" this means, if i am correct is nothing then lockworkstation?

 

Example.PNG

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Vaiola
      We need to transfer from windows server 2012 to windows server 2016, we didn't find a way using to transfer the data automatically or to email us after tasks are finished.
      Is there a way to copy the data with NTFS/Shared permissions and timestamps without any loss?
    • By nacerbaaziz
      hello autoit team
      please i need your help today
      am trying to make the list of features in list view and control it
      am using the dism command line to read output
      i've made the code
      but i found some problems
      what i need is the list of features in 2d array
      $array[$n][0]= name $array[$i][1] = state
      when i tested the code
      it give me some results that i don't need to it e.g
      ------ ------  | --------
      or
      name       | state
      i need just the list of features and there state
      please help me to do that
      here is my example
      #RequireAdmin #NoTrayIcon #include <AutoItConstants.au3> Wow64EnableWow64FsRedirection(false) _Windows_Get_Features() func _Windows_Get_Features() local $a_FeaturesArray[1][2] $a_FeaturesArray[0][0] local $i_Dism_Run = Run("DISM /online /english /get-features /format:table", "", @sw_hide, BitOR($STDERR_CHILD, $STDOUT_CHILD)) local $s_OutputDism = "", $a_OutPutDism While 1 $s_OutputDism = StdoutRead($i_Dism_Run) If @error Then exitLoop if ($s_OutputDism = "") or (StringRegExp($s_OutputDism, "^((\-){1,})?(?:\s)\|?(?:\s)((\-){1,})$", 0) = 1) then ContinueLoop if (StringRegExp($s_OutputDism, "((([\s\d\-\+\_\,]{1})\|([\s\d\-\+\_\,]{1})){1,})", 0) = 1) then $a_OutPutDism = StringSplit($s_OutputDism, @lf) for $i = 1 to $a_OutPutDism[0] if msgBox(1, $i, $a_OutPutDism[$i]) = 1 then exitLoop next endIf Wend endFunc func Wow64EnableWow64FsRedirection($b_Enabled) local $h_OpenFS = DLLOpen("kernel32.dll") local $Return = DllCall($h_OpenFS, "boolean", "Wow64EnableWow64FsRedirection", "boolean", $b_Enabled) if @error then DLLClose($h_OpenFS) Return SetError(@error, @extended, -1) else DLLClose($h_OpenFS) Return $Return[0] endIf endFunc  
    • By nacerbaaziz
      question about _WinAPI_CreateWindowEx
      good morning
      welcome autoit team
      please i need your help
      i've searched a lot about how to use the _WinAPI_CreateWindowEx
      finally i found an example
      but i found some problem i hope you can help me
      firstly, i want to set the controls focussable with the keyboard input
      i already used the ws_tabStop but it did not work with me.
      secondly, i want to set some access keys linked with the window
      such as control+o enable the open button and control+f4 exit the app
      note: i need a local access keys and not a global hotkeys
      such as GUISetAccelerators
      finaly, before i will put the code here i must clarify a few things.
      1. you will ask me why you don't use the GUICreate function
      here i'll tell you that it as dialog and It is a little heavy in motion with screen readers.
      the screen readers for blind has some function that work with dialogs and others work with full windows style
      2. you will ask me why you didn't search the net for that?
      i will tell you that all examples that i found in the internet with pdfs and Picture books.
      i found some examples in microsoft but it with cpp.
      ok here is the code
      i hope you can help me to do what i want
      thank you in advance
       
      ; Small AutoIt Application that uses Windows API ; Written by Yuraj #NoTrayIcon #include <_RegisterClassEx.au3> #include <WinAPI.au3> #include <WindowsConstants.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <FontConstants.au3> AutoItSetOption("MustDeclareVars", 1) ; Window definitions Const $WinWidth = 370 Const $WinHeight = 350 Const $WinXPos = (@DesktopWidth / 2) - ($WinWidth / 2) Const $WinYPos = (@DesktopHeight / 2) - ($WinHeight / 2) Const $WinTitle = "Win32 Application - Text reader" Const $WinClass = "mainapp" Const $WinIcon = _WinAPI_LoadIcon(_WinAPI_GetModuleHandle("shell32.dll"), 13) ; Windows handles Global $hwnd, $edit1, $btn1, $btn2 ; Fonts Global $fnt1 ; Register class, Create the window Local $retVal = __WinAPI_RegisterClassEx($WinClass, "WindowCallback", $WinIcon, 0, _WinAPI_GetSysColor($COLOR_BTNFACE), BitOR($CS_DEFAULTSTYLE, $CS_DROPSHADOW)) ; If $retVal == 0 Then ; If registerclass fails MsgBox(16, "Error", "Error while registering window class!") Exit EndIf ; Create windows/controls $hwnd = _WinAPI_CreateWindowEx($WS_EX_STATICEDGE, $WinClass, $WinTitle, BitOR($WS_OVERLAPPED,$WS_SYSMENU, $WS_MINIMIZEBOX, $WS_GROUP, $WS_DLGFRAME), $WinXPos, $WinYPos, $WinWidth, $WinHeight, 0) $btn1 = _WinAPI_CreateWindowEx(0, "button", "Open file ...", BitOR($WS_VISIBLE, $WS_CHILD, $WS_TABSTOP, $WS_CLIPCHILDREN), 25, 270, 100, 30,$hwnd) $btn2 = _WinAPI_CreateWindowEx(0, "Button", "Exit", BitOR($WS_VISIBLE, $WS_CHILD, $WS_TABSTOP, $WS_CLIPCHILDREN), 235, 270, 100, 30, $hwnd) $edit1 = _WinAPI_CreateWindowEx(0, "edit", "text", BitOR($WS_VISIBLE, $WS_CHILD, $WS_VSCROLL, $ES_AUTOVSCROLL, $es_readOnly, $WS_TABSTOP), 5, 5, $WinWidth - 15, $WinHeight - 100, $hwnd) ; Set controls identifiers _WinAPI_SetWindowLong($btn1,$GWL_ID,150) _WinAPI_SetWindowLong($btn2,$GWL_ID,160) ; Set (controls) fonts $fnt1 = _CreateFont("MS Sans Serif", 15) _WinAPI_SetFont($btn1, $fnt1) _WinAPI_SetFont($btn2, $fnt1) _WinAPI_SetFont($edit1, $fnt1) ; Set focus to edit _WinAPI_SetFocus($edit1) ; Show window _WinAPI_ShowWindow($hwnd) _WinAPI_UpdateWindow($hwnd) ; Main loop that keep application opened While 1 Sleep(100) WEnd ;=================================================================; ; WINDOW CALLBACK ... ;=================================================================; Func WindowCallback($_hwnd, $iMsg, $wParam, $lParam) Local $iNC, $iID Switch $iMsg ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Case $WM_CLOSE ; Show message on closing If MsgBox(48 + 4, $WinTitle, "Do you want really exit?", 0, $hwnd) <> 6 Then Return 0 ; Call destructor and then exit main thread FinalizeApp() Exit ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Case $WM_COMMAND $iNC = _WinAPI_HiWord($wParam) $iID = _WinAPI_LoWord($lParam) Switch $iNC Case $BN_CLICKED ; When is control clicked Switch _WinAPI_GetDlgCtrlID($iID) Case _WinAPI_GetDlgCtrlID($btn1) BtnOpenFileClick() Case _WinAPI_GetDlgCtrlID($btn2) BtnExitClick() EndSwitch EndSwitch ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; EndSwitch Return _WinAPI_DefWindowProc($_hwnd, $iMsg, $wParam, $lParam) EndFunc ;==>WindowCallback Func FinalizeApp() _WinAPI_DeleteObject($fnt1) _WinAPI_DestroyWindow($hwnd) __WinAPI_UnregisterClass($WinClass) EndFunc ;==>FinalizeApp Func _CreateFont($fontName, $height = 16, $style = $FW_NORMAL, $italic = False, $underline = False, $strikeout = False) Local $hFont = _WinAPI_CreateFont($height, 0, 0, 0, $style, $italic, $underline, $strikeout, $DEFAULT_CHARSET, _ $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $fontName) Return $hFont EndFunc ;==>_CreateFont ;=================================================================; ; WINDOW EVENTS ;=================================================================; Func BtnOpenFileClick() Local $ret = _WinAPI_GetOpenFileName("", "Text files (*.txt)|All files (*.*)", ".", "", "", 1, 0, 0, $hwnd) If ($ret[0] > 0) Then Local $path = $ret[1] & "\" & $ret[2] Local $file = _WinAPI_CreateFile($path, 2, 2) Local $buf = DllStructCreate("byte[" & _WinAPI_GetFileSizeEx($file) & "]") Local $i = 0 _WinAPI_ReadFile($file, DllStructGetPtr($buf), _WinAPI_GetFileSizeEx($file), $i) ; Close file handle _WinAPI_CloseHandle($file) _WinAPI_SetWindowText($edit1, BinaryToString(DllStructGetData($buf, 1))) EndIf EndFunc ;==>BtnOpenFileClick Func BtnExitClick() FinalizeApp() Exit EndFunc ;==>BtnExitClick  
      _RegisterClassEx.au3
    • By Saravanan2213
      Hi everyone,
      i just need your help. i have script for cleaning browsing history and other unwanted file but the problem is i don't want to see the GUI i have attached below. It should not show anything on the screen. is it possible to do it in autoit. thanks in advance.
       
      So for i have tried flag
      Run("rundll32.exe InetCpl.cpl,ClearMyTracksByProcess 255", "", @SW_HIDE)
      @SW_DISABLE
       
      Looking forward your help.........

    • By Kelvin44
      WiFi Disconnects Automatically Windows 10 / 8 / 7 Laptop. This video guide very help to me
×
×
  • Create New...