Jump to content

Finding Unique hardware ID and lock Windows OS


Iznogoud
 Share

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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 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

×
×
  • Create New...