How to create script that allows USB stick in Windows by Serial Number?

Asked

Viewed 59 times

-3

I would like to exchange some practical ideas with you to accomplish an achievement: Create a script for Windows that checks the Serial Number of a USB stick and can thus allow or deny that it mounts:

So far, I have the following:

     #CAPTURA SERIAL NUMBER
                    
        $serial = ((Get-Physicaldisk|Select SerialNumber,BusType|findstr /L USB)| %{"$($_.Split('USB'))"})
               
         #CONSULTA SE O SERIAL FOI CADASTRADO NO SISTEMA
       iwr http://FORMULARIO_WEB_COM_SERIAIS_DE_PENDRIVES_CADASTRADOS.html -OutFile C:\Users\%USER%\AppData\Local\serie.txt
                
                    
ForEach ($value in $serial){
                    
                     
 $compara = (Select-String -Path C:\Users\%USER%\AppData\Local\serie.txt “[$value]” -Quiet)
                 
$value
$compara
                
        if ($compara = "True"){
                
                        reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR /v Start /t REG_DWORD /D 3 /f
                    }
        else{
                
                        reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR /v Start /t REG_DWORD /D 4 /f
                
                    }
            
            }

The serie.txt file contains strings with serial number of disks allowed and as it is updated in the WEB form continuously, it updates the list.

In the above script, the $compare variable should compare the serial of the connected device via USB and then give the value True or False. But it only gives True, even though the value of the serial String is not in the file serie.txt.

If I do

Select-String -Path C:\Users\admin\AppData\Local\serie.txt “123456” -Quiet

and "123456" is in the file serie.txt, the value is True!

And then people suggest to me?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.