Jump to content
ur

how to save credentials of network shares in windows

Recommended Posts

Whenever we try to access a network share it will prompt for credentials based on the shared folder settings.

Like the below prompt

d1.PNG

d2.PNG

Once you save them, they are saved in Windows Credentials of Credentials Manager in Control panel (run --> control keymgr.dll).

d3.PNG

 

But not able to found exact file or registry for this setting.

 

So, can anyone suggest how to do this process of adding network credentials using AutoIT.

 

Share this post


Link to post
Share on other sites

Easy... put a file .bat on C:\\ .... and execute it on windows load..."Autostart". This is an old trick

Edited by rootx

Share this post


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

Easy... put a file .bat on C:\\ .... and execute it on windows load..."Autostart". This is an old trick

But what we need to keep in the batch file is the question then.

Because I don't find any command line options to access the credential manager

Share this post


Link to post
Share on other sites
27 minutes ago, orbs said:

Thank you very much.

Don't know why it didn't came in Google search :)

To add credentials..
 

cmdkey /add:sharedmachine /user:domainname\username /pass:password

To remove credentials.

cmdkey /delete:sharedmachine

I will use them using run command to execute in my code.

Share this post


Link to post
Share on other sites
34 minutes ago, ur said:

But what we need to keep in the batch file is the question then.

Because I don't find any command line options to access the credential manager

First learn https://technet.microsoft.com/en-us/library/bb490717.aspx

Second....

local user net use d:\\server\share /user:Dan  password     or net use d:\\server\share /user:Accounts\Dan password

Domain net use d:\\server\share /user:DOMAIN\Dan password

Then to have persistent add /persistent:yes

Share this post


Link to post
Share on other sites
3 minutes ago, rootx said:

First learn https://technet.microsoft.com/en-us/library/bb490717.aspx

Second....

local user net use d:\\server\share /user:Dan  password     or net use d:\\server\share /user:Accounts\Dan password

Domain net use d:\\server\share /user:DOMAIN\Dan password

Then to have persistent add /persistent:yes

I am getting the below error when I try the net use

 

System error 67 has occurred.

The network name cannot be found.

Share this post


Link to post
Share on other sites
3 minutes ago, rootx said:

net use Z: \\ipserver\ /user:\username password

ok..for mapping the network drives right..

But I need to access the network share directly as the number of shares are more than 26 :)

The cmdkey is working.I am able to access the share once I add the credentials to credential manager.

Share this post


Link to post
Share on other sites

An AutoIt code answer (because it's a forum about AutoIt) :

DriveMapAdd("", "\\ukreddy-e7470\c$", 0, "username", "password")

 

Share this post


Link to post
Share on other sites
4 minutes ago, jguinch said:

An AutoIt code answer (because it's a forum about AutoIt) :

DriveMapAdd("", "\\ukreddy-e7470\c$", 0, "username", "password")

 

DriveMapAdd("", "\\ukreddy-e7470\c$", 1, "username", "password")

persistent. and require #RequireAdmin

Edited by rootx

Share this post


Link to post
Share on other sites

Hi ur,

I prefer not to rely on external programs/tools so let me share an alternative approach to cmdkey.exe. I didn't write this, but I modified it to work for my needs. In my script I actually encrypt the password prior to storing it. Hopefully, this will make it easier for you.

 

#include <Array.au3>

$User = "user1"
$Password = "password1"
$AppName = "My App"
$Comment = "Used For ABC"

_Cred_Write($User, $Password) ; Write to Crediential Manager

$aArray = _Cred_Read() ; Read from Crediential Manager
_ArrayDisplay($aArray)

_Cred_Delete() ; Delete Credientials


Func _Cred_Write($User, $Password, $iPersist = 1, $Target = $AppName, $Comm = $Comment)
    ; iPersist
    ; SESSION = 1
    ; LOCAL_MACHINE = 2
    ; ENTERPRISE = 3

    Local $Comment = DllStructCreate("wchar[100]")
    DllStructSetData($Comment, 1, $Comm)

    Local $targetName = DllStructCreate("wchar[100]")
    DllStructSetData($targetName, 1, $Target)

    Local $userName = DllStructCreate("wchar[100]")
    DllStructSetData($userName, 1, $User)

    Local $credentialBlob = DllStructCreate("wchar[100]")
    DllStructSetData($credentialBlob, 1, $Password)

    Local $structCREDENTIAL = "" & _
            "DWORD Flags;" & _
            "DWORD Type;" & _
            "Ptr TargetName;" & _
            "Ptr Comment;" & _
            "UINT64 LastWritten;" & _
            "DWORD CredintialBlobSize;" & _
            "Ptr CredentialBlob;" & _
            "DWORD Persist;" & _
            "DWORD AttributeCount;" & _
            "ptr Attributes;" & _
            "Ptr TargetAlias;" & _
            "Ptr Username"

    Local $NewCred = DllStructCreate($structCREDENTIAL)
    If @error Then
        MsgBox(0, "NewCred", "Error in DllStructCreate " & @error) ;
        Exit
    EndIf

    DllStructSetData($NewCred, "Flags", 0)
    DllStructSetData($NewCred, "Type", 1)
    DllStructSetData($NewCred, "TargetName", DllStructGetPtr($targetName))
    DllStructSetData($NewCred, "Persist", $iPersist)
    DllStructSetData($NewCred, "AttributeCount", 0)
    DllStructSetData($NewCred, "UserName", DllStructGetPtr($userName))
    DllStructSetData($NewCred, "CredentialBlob", DllStructGetPtr($credentialBlob))
    DllStructSetData($NewCred, "CredintialBlobSize", StringLen($Password) * 2)
    DllStructSetData($NewCred, "Comment", DllStructGetPtr($Comment))

    #comments-start
        MsgBox(0, "DllStruct", "Data:" & @CRLF & _
        "Flags: " & DllStructGetData($NewCred, "Flags") & @CRLF & _
        "Type: " & DllStructGetData($NewCred,"Type") & @CRLF & _
        "TargetName: " &  DllStructGetData($NewCred,"TargetName") & @CRLF & _
        "Persist: " & DllStructGetData($NewCred,"Persist") & @CRLF & _
        "AttributeCount: " &  DllStructGetData($NewCred,"AttributeCount") & @CRLF & _
        "UserName: " &  DllStructGetData($NewCred,"UserName") & @CRLF & _
        "CredentialBlob: " &  DllStructGetData($NewCred,"CredentialBlob") & @CRLF & _
        "CredintialBlobSize: " &  DllStructGetData($NewCred,"CredintialBlobSize") & @CRLF & _
        "Comment: " &  DllStructGetData($NewCred,"Comment"))
    #comments-end

    Local $hAdvapi32 = DllOpen("Advapi32.dll")
    If @error Then
        MsgBox(0, "Error", "Cannot open Advapi32.dll")
        Exit
    EndIf
    Local $aRet = DllCall($hAdvapi32, 'bool', 'CredWriteW', 'ptr', DllStructGetPtr($NewCred), 'dword', 0)

    $NewCred = 0
EndFunc

Func _Cred_Read($Target = $AppName)
    Local $aFuncRet[3]
    Local $iType = 1 ; 1 = Generic | 2 = Domain | 3 = Certificate

    Local $targetName = DllStructCreate("wchar[100]")
    DllStructSetData($targetName, 1, $Target)

    Local $hAdvapi32 = DllOpen("Advapi32.dll")
    Local $aRet = DllCall($hAdvapi32, 'bool', 'CredReadW', 'ptr', DllStructGetPtr($targetName), 'dword', $iType, 'dword', 0, 'ptr*', 0)

    If $aRet[0] = 0 Then Return SetError(1, 0)

    Local $structCREDENTIAL = "" & _
            "DWORD Flags;" & _
            "DWORD Type;" & _
            "Ptr TargetName;" & _
            "Ptr Comment;" & _
            "UINT64 LastWritten;" & _
            "DWORD CredintialBlobSize;" & _
            "Ptr CredentialBlob;" & _
            "DWORD Persist;" & _
            "DWORD AttributeCount;" & _
            "Ptr Attributes;" & _
            "Ptr TargetAlias;" & _
            "Ptr Username"

    Local $tdata = DllStructCreate($structCREDENTIAL, $aRet[4])

    Local $userName = DllStructCreate("wchar[100]", DllStructGetData($tdata, 'Username'))
    Local $sUser = DllStructGetData($userName, 1)

    Local $CredentialBlobSize = DllStructGetData($tdata, 'CredintialBlobSize')
    Local $credentialBlob = DllStructCreate("wchar[100]", DllStructGetData($tdata, 'CredentialBlob'))
    Local $sPassword = StringLeft(DllStructGetData($credentialBlob, 1), $CredentialBlobSize / 2)

    Local $Comment = DllStructCreate("wchar[100]", DllStructGetData($tdata, 'Comment'))
    Local $sComm = DllStructGetData($Comment, 1)

    Dim $aFuncRet[] = [$sUser, $sPassword, $sComm]
    Return $aFuncRet
EndFunc

Func _Cred_Delete($Target = $AppName)
    Local $aRet
    Local $targetName = DllStructCreate("wchar[100]")
    DllStructSetData($targetName, 1, $Target)

    Local $hAdvapi32 = DllOpen("Advapi32.dll")
    $aRet = DllCall($hAdvapi32, 'bool', 'CredDeleteW', 'ptr', DllStructGetPtr($targetName), 'dword', 1, 'dword', 0)
EndFunc

 

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

    • By rudi
      Hello,
      Either drag drop the opened file on the server and the temp file, Aut2Exe could not copy to the destination file, as that one is opened, to the edits of this GUI, or take the Aut2Exe error message line to your clipboard, the line will be split automatically.
      This script does not check
      For the required rights to the destination folder (to close open file handles) for correct input Use either clipboard (Aut2Exe message), drag & drop, or copy the full paths of source and destination file *IN ONE* (don't type, if you want to do so, modify the script, for me it's fine this way ;-)  
      Example Error Message:
      !>11:19:15 Problem copying file from: C:\Users\UserName\AppData\Local\AutoIt v3\Aut2exe\~AU98E6.tmp.exe To :z:\MyAutoitExeForTheUsers.exe
      #include <GUIConstantsEx.au3> #include <NetShare.au3> #include <WindowsConstants.au3> #include <Array.au3> #include <EditConstants.au3> ; Enumerate open files on the server $DragDropOpenFile = "<drag drop the opened file on a server share here>" $DragDropNewFile = "<drag drop the file supposed to replace the above one here>" $NewFile = "" $Gui_h = 250 $Gui_w = 800 $vDist = 7 ; GUICreate($GuiTitle, $w, $h, @DesktopWidth - $w - 100, @DesktopHeight - $h - 60, -1, $WS_EX_ACCEPTFILES) ; generally enable drag-drop for files into other GUI controls $myGui = GUICreate("Tool to forcibly close & replace open files on server shares", $Gui_w, $Gui_h, 100, 100, -1, $WS_EX_ACCEPTFILES) $InputFileToClose = GUICtrlCreateInput($DragDropOpenFile, 20, $vDist, $Gui_w - 40, 20) GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; allow drag-droping files for this control, $InputFile Opt("Guicoordmode", 2) $InputFileNew = GUICtrlCreateInput($DragDropNewFile, -1, $vDist) GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; allow drag-droping files for this control, $InputFile GUICtrlSetState(-1, $GUI_DISABLE) $lServer = GUICtrlCreateLabel("<server>", -1, $vDist) $lShare = GUICtrlCreateLabel("<share-mapping>", -1, $vDist) $lPath = GUICtrlCreateLabel("<sub-path>", -1, $vDist) $lFile = GUICtrlCreateLabel("<file>", -1, $vDist) $doit = GUICtrlCreateButton("Search and close for open file handles", -1, $vDist) GUICtrlSetState(-1, $GUI_DISABLE) $exit = GUICtrlCreateButton("Cancel", -1, $vDist) GUISetState() $FN = False $FNnew = "" $FNmatch = False $ToolTitle = "" $ToolTxt = "" $RegExA2E = "(?i)^(?:.*?Problem copying file from: )(.*?)(?: To :)(.*$)" ; $1 = compiled local TEMP file, $2 = not replacable destination file ; Examle Replacement failed output: !>11:19:15 Problem copying file from: C:\Users\USERNAME\AppData\Local\AutoIt v3\Aut2exe\~AU98E6.tmp.exe To :z:\MyAutoitExeForTheUsers.exe While 1 $Clip = ClipGet() If StringRegExp($Clip, $RegExA2E) Then $Src = StringRegExpReplace($Clip, $RegExA2E, "$2") GUICtrlSetData($InputFileToClose, $Src) $Dst = StringRegExpReplace($Clip, $RegExA2E, "$1") GUICtrlSetData($InputFileNew, $Dst) EndIf $input = GUICtrlRead($InputFileToClose) If $input <> $DragDropOpenFile Then ; da wurde was reingezogen $DragDropOpenFile = $input If StringLeft($input, 2) = "\\" Then $Type = "UNC" $ServerShareUNC = StringLeft($input, StringInStr($input, "\", 0, 4) - 1) $fRelPathFN = StringReplace($input, $ServerShareUNC, "") $fPath = StringLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) $FN = StringTrimLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) ElseIf StringMid($input, 2, 1) = ":" Then ; Pfad mit Laufwerksbuchstabe, evtl. Netzwerk Mapping (erforderlich) $drive = StringLeft($input, 2) $Type = DriveGetType($drive) If $Type = "Network" Then $ServerShareUNC = DriveMapGet($drive) $foo = StringReplace($input, $drive, $ServerShareUNC) ; Laufwerkspfad in UNC Pfad umwandeln $fRelPathFN = StringReplace($foo, $ServerShareUNC, "") $fPath = StringLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) $FN = StringTrimLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) Else MsgBox(48, @ScriptLineNumber, "This script can *ONLY* close remotely handles for files open on server shares!" & @CRLF & _ $input & @CRLF & _ $drive & " = " & $Type) ContinueLoop 2 EndIf EndIf $Server = StringLeft($ServerShareUNC, StringInStr($ServerShareUNC, "\", 0, 3) - 1) $Share = StringTrimLeft($ServerShareUNC, StringInStr($ServerShareUNC, "\", 0, 3) - 1) GUICtrlSetData($lServer,"Server = '" & $Server& "'") GUICtrlSetData($lShare, "Mapping = '" & $Share & "'") GUICtrlSetData($lPath, "SubPath = '" &$fPath & "'") GUICtrlSetData($lFile, "FileName= '" & $FN &"'") GUICtrlSetState($doit, $GUI_ENABLE) GUICtrlSetState($InputFileNew, $GUI_ENABLE) EndIf If $NewFile <> GUICtrlRead($InputFileNew) Then $NewFile = GUICtrlRead($InputFileNew) If $NewFile <> $DragDropNewFile Then If StringInStr($NewFile, $DragDropNewFile) Then $NewFile = StringReplace($NewFile, $DragDropNewFile, "") GUICtrlSetData($InputFileNew, $NewFile) EndIf $FNnew = StringTrimLeft($NewFile, StringInStr($NewFile, "\", 0, -1)) If $FN = $FNnew Then If $FNmatch = False Then $FNmatch = True GUICtrlSetData($doit, "Search for & Close open file handles, then replace file") EndIf Else If $FNmatch Then $FNmatch = False GUICtrlSetData($doit, "Search for & Close open file handles, then replace file") GUICtrlSetData($InputFileNew, $DragDropNewFile) EndIf EndIf EndIf EndIf Switch GUIGetMsg() Case $exit, $GUI_EVENT_CLOSE GUIDelete($myGui) Exit Case $doit AbArbeiten($Server, $Share, $fPath, $FN) EndSwitch WEnd Func AbArbeiten($_Srv, $_Shr, $_fPth, $_fNme) Local $iID = 0 Local $iRights = 1 Local $iLckCount = 2 Local $iFPFN = 3 Local $iUser = 4 ConsoleWrite($_fPth & $_fNme & @CRLF) Local $aFile = _Net_Share_FileEnum($_Srv) If IsArray($aFile) Then ; _ArrayDisplay($aFile) Local $x $ToolTxt = "Open File Handles:" $ToolTitle = "Handles to be checked total: " & $aFile[0][0] UpdateToolTip() AdlibRegister(UpdateToolTip, 1000) For $x = $aFile[0][0] To 1 Step -1 $ToolTitle = $x & " handles remaining for checking..." If Not StringInStr($aFile[$x][$iFPFN], $_fPth & $_fNme) Then ; ConsoleWrite("Nix Enthalten in: " & $aFile[$x][$iFPFN] & @CRLF) _ArrayDelete($aFile, $x) Else $ToolTxt &= @CRLF & $aFile[$x][$iFPFN] & ", User = " & $aFile[$x][$iUser] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToolTxt = ' & $ToolTxt & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndIf Next $aFile[0][0] = UBound($aFile) - 1 _ArraySort($aFile, 0, 1, 0, $iUser) ; _ArrayDisplay($aFile, $aFile[0][0] & " FileLocks found.") If $aFile[0][0] = 0 Then $ToolTitle = "Done, no open file handles were found" $ToolTxt &= @CRLF & ", no handles to be closed for this file!" Sleep(2000) $ToolTxt = "" ReplaceFile() Else $ToolTitle = $aFile[0][0] & " open file handles were found..." $CloseErr = 0 For $x = 1 To $aFile[0][0] If _Net_Share_FileClose($Server, $aFile[$x][$iID]) Then $ToolTxt &= @CRLF & @TAB & "Handle closed: " & $aFile[$x][$iID] Else $ToolTxt &= @CRLF & "ERROR: Handle Close Failed! --> " & $aFile[$x][$iFPFN] & ", User = " & $aFile[$x][$iUser] $CloseErr += 1 EndIf Next ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $CloseErr = ' & $CloseErr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console If $CloseErr = 0 Then $ToolTitle = "All found open file handles could be closed." ReplaceFile() Else $ToolTitle = $CloseErr & " Handles could *NOT* be closed!" Sleep(2000) EndIf Sleep(1000) $ToolTxt = "" $ToolTitle = "" EndIf Else MsgBox(0, "not an array", $aFile & @CRLF & @error & @CRLF & @extended & @CRLF & _ "Unable to retrieve the array of open file handles for " & $Share) EndIf EndFunc ;==>AbArbeiten Func UpdateToolTip() ToolTip($ToolTxt, MouseGetPos(0) + 20, MouseGetPos(1) + 20, $ToolTitle) ; ConsoleWrite( $ToolTxt & @CRLF & $ToolTitle & @CRLF & "-------------------" & @CRLF) EndFunc ;==>UpdateToolTip Func ReplaceFile() If FileExists($NewFile) Then If FileCopy($NewFile, $input, 1 + 8) Then $ToolTitle = "File successfully replaced." $ToolTxt = "Done" Else $ToolTitle = "File could *NOT* be replaced." $ToolTxt = "Possibly another open file handle spawned while this script was running." & @CRLF & _ "Simply start over again, please." MsgBox(48, $ToolTitle, $ToolTxt) EndIf EndIf EndFunc ;==>ReplaceFile  
    • By Drac89
      How do i pass the IE handle to an active windows authentication prompt when the IE shows it is " waiting " , right now it is nt detecting the windows security prompt
      I used the below to wait for it to load until the 60ms timeout but still it isnt working  :
       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
      #AutoIt3Wrapper_Outfile=ps_test.exe
      #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
      #include <Constants.au3>
      #include <IE_v1.au3>
      If $CmdLine[0] < 2 Then
         MsgBox($MB_OK, "Usage", "ps_WAN <username> <password>")
      Else
         wan($CmdLine[1], $CmdLine[2])
      EndIf
      Func wan($email, $password)
       $oIE = _IECreate()
      $ieo=_IENavigate($oIE, "https://cccc")
      _IELoadWait($ieo,0,60)
       Local $hWnd = WinExists("Windows Security")
      if $hWnd = 0 Then
         MsgBox($MB_OK, "Usage", "timeout")
         EndIf
      ControlFocus("Windows Security", "", "[CLASS:DirectUIHWND]")
      ControlFocus("Windows Security", "", "[CLASS:Edit; INSTANCE:1]")
       

    • By rudi
      Hello,
      When new versions e.g. of your compiled script cannot be replaced on the server, as they are in use, you can use this script to manage to close the open handles and to replace the old file with your new version.

      ; Autoit v3.3.14.5 #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_Description=Close OpenFileHandlles, replace file on Server with a new version #AutoIt3Wrapper_Res_Fileversion=1 #AutoIt3Wrapper_Res_LegalCopyright=(c) 2019 by Rudolf Thilo, IT-Beratung Rudolf Thilo #AutoIt3Wrapper_Res_SaveSource=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <GUIConstantsEx.au3> #include <NetShare.au3> #include <WindowsConstants.au3> #include <Array.au3> #include <EditConstants.au3> ; Enumerate open files on the server $DragDropOpenFile = "<drag-drop file locked on Server to be closed and replaced>" $DragDropNewFile = "<drag-drop file to replace the one on the server>" $NewFile = "" $Gui_h = 250 $Gui_w = 800 $vDist = 7 ; GUICreate($GuiTitle, $w, $h, @DesktopWidth - $w - 100, @DesktopHeight - $h - 60, -1, $WS_EX_ACCEPTFILES) ; generally enable drag-drop for files into other GUI controls $myGui = GUICreate("force close of all NETWORK opened file handles", $Gui_w, $Gui_h, 100, 100, -1, $WS_EX_ACCEPTFILES) $InputFileToClose = GUICtrlCreateInput($DragDropOpenFile, 20, $vDist, $Gui_w - 40, 20) GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; allow drag-droping files for this control, $InputFile Opt("Guicoordmode", 2) $InputFileNew = GUICtrlCreateInput($DragDropNewFile, -1, $vDist) GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; allow drag-droping files for this control, $InputFile GUICtrlSetState(-1, $GUI_DISABLE) $lServer = GUICtrlCreateLabel("<server>", -1, $vDist) $lShare = GUICtrlCreateLabel("<share>", -1, $vDist) $lPath = GUICtrlCreateLabel("<path>", -1, $vDist) $lFile = GUICtrlCreateLabel("<file>", -1, $vDist) $doit = GUICtrlCreateButton("search for and close open file handles", -1, $vDist) GUICtrlSetState(-1, $GUI_DISABLE) $exit = GUICtrlCreateButton("cancel", -1, $vDist) GUISetState() $FN = False $FNnew = "" $FNmatch = False $ToolTitle = "" $ToolTxt = "" While 1 $input = GUICtrlRead($InputFileToClose) If $input <> $DragDropOpenFile Then ; da wurde was reingezogen $DragDropOpenFile = $input If StringLeft($input, 2) = "\\" Then $Type = "UNC" $ServerShareUNC = StringLeft($input, StringInStr($input, "\", 0, 4) - 1) $fRelPathFN = StringReplace($input, $ServerShareUNC, "") $fPath = StringLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) $FN = StringTrimLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) ElseIf StringMid($input, 2, 1) = ":" Then ; Pfad mit Laufwerksbuchstabe, evtl. Netzwerk Mapping (erforderlich) $drive = StringLeft($input, 2) $Type = DriveGetType($drive) If $Type = "Network" Then $ServerShareUNC = DriveMapGet($drive) $foo = StringReplace($input, $drive, $ServerShareUNC) ; Laufwerkspfad in UNC Pfad umwandeln $fRelPathFN = StringReplace($foo, $ServerShareUNC, "") $fPath = StringLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) $FN = StringTrimLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1)) Else MsgBox(0, @ScriptLineNumber, "This script can *ONLY* close open file handles on NETWORK SHARES!" & @CRLF & _ $input & @CRLF & _ $drive & " = " & $Type) ContinueLoop 2 EndIf EndIf $Server = StringLeft($ServerShareUNC, StringInStr($ServerShareUNC, "\", 0, 3) - 1) $Share = StringTrimLeft($ServerShareUNC, StringInStr($ServerShareUNC, "\", 0, 3) - 1) GUICtrlSetData($lServer, $Server) GUICtrlSetData($lShare, $Share) GUICtrlSetData($lPath, $fPath) GUICtrlSetData($lFile, $FN) GUICtrlSetState($doit, $GUI_ENABLE) GUICtrlSetState($InputFileNew, $GUI_ENABLE) EndIf If $NewFile <> GUICtrlRead($InputFileNew) Then $NewFile = GUICtrlRead($InputFileNew) If $NewFile <> $DragDropNewFile Then If StringInStr($NewFile, $DragDropNewFile) Then $NewFile = StringReplace($NewFile, $DragDropNewFile, "") GUICtrlSetData($InputFileNew, $NewFile) EndIf $FNnew = StringTrimLeft($NewFile, StringInStr($NewFile, "\", 0, -1)) If $FN = $FNnew Then If $FNmatch = False Then $FNmatch = True GUICtrlSetData($doit, "Search for open file handles, close them, overwrite old file.") EndIf Else If $FNmatch Then $FNmatch = False GUICtrlSetData($doit, "Search for open file handles and close them.") GUICtrlSetData($InputFileNew,$DragDropNewFile) EndIf EndIf EndIf EndIf Switch GUIGetMsg() Case $exit, $GUI_EVENT_CLOSE GUIDelete($myGui) Exit Case $doit AbArbeiten($Server, $Share, $fPath, $FN) EndSwitch WEnd Func AbArbeiten($_Srv, $_Shr, $_fPth, $_fNme) Local $iID = 0 Local $iRights = 1 Local $iLckCount = 2 Local $iFPFN = 3 Local $iUser = 4 ConsoleWrite($_fPth & $_fNme & @CRLF) Local $aFile = _Net_Share_FileEnum($_Srv) If IsArray($aFile) Then ; _ArrayDisplay($aFile) Local $x $ToolTxt = "Open Handles:" $ToolTitle = $aFile[0][0] & " handles still waiting to be checkt..." UpdateToolTip() AdlibRegister(UpdateToolTip, 1000) For $x = $aFile[0][0] To 1 Step -1 $ToolTitle = $x & " handles waiting to be checked..." If Not StringInStr($aFile[$x][$iFPFN], $_fPth & $_fNme) Then ; ConsoleWrite("Nix Enthalten in: " & $aFile[$x][$iFPFN] & @CRLF) _ArrayDelete($aFile, $x) Else $ToolTxt &= @CRLF & $aFile[$x][$iFPFN] & ", User = " & $aFile[$x][$iUser] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToolTxt = ' & $ToolTxt & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndIf Next $aFile[0][0] = UBound($aFile) - 1 _ArraySort($aFile, 0, 1, 0, $iUser) ; _ArrayDisplay($aFile, $aFile[0][0] & " FileLocks found.") If $aFile[0][0] = 0 Then $ToolTitle = "Done. No open handles coud be found." $ToolTxt &= @CRLF & "Nothing to be closed!" Sleep(2000) $ToolTxt = "" Else $ToolTitle = $aFile[0][0] & " Handles found to be closed..." $CloseErr = 0 For $x = 1 To $aFile[0][0] If _Net_Share_FileClose($Server, $aFile[$x][$iID]) Then $ToolTxt &= @CRLF & @TAB & "Handle closed: " & $aFile[$x][$iID] Else $ToolTxt &= @CRLF & "ERROR: Handle Close Failed! --> " & $aFile[$x][$iFPFN] & ", User = " & $aFile[$x][$iUser] $CloseErr += 1 EndIf Next ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $CloseErr = ' & $CloseErr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console If $CloseErr = 0 Then $ToolTitle = "Alle open handles were closed successfully." If FileExists($NewFile) Then If FileCopy($NewFile, $input, 1 + 8) Then $ToolTitle = "File could be replaced." $ToolTxt = "done" Else $ToolTitle = "File could *NOT* be replaced" $ToolTxt = "Most propably a newly open file handle was created while this script was running." & @CRLF & _ "Just start over again." EndIf EndIf Else $ToolTitle = $CloseErr & " handles could *NOT* be closed!" Sleep(2000) EndIf Sleep(1000) $ToolTxt = "" $ToolTitle = "" EndIf Else MsgBox(0, "not an array", $aFile & @CRLF & @error & @CRLF & @extended) EndIf EndFunc ;==>AbArbeiten Func UpdateToolTip() ToolTip($ToolTxt, MouseGetPos(0) + 20, MouseGetPos(1) + 20, $ToolTitle) ; ConsoleWrite( $ToolTxt & @CRLF & $ToolTitle & @CRLF & "-------------------" & @CRLF) EndFunc ;==>UpdateToolTip Rudi.
    • By Raywando
      Hello,
      This is my first post. So I’ve worked on a script for a while and I’m planning to publish it but the problem is that it connects to an FTP server at some point, and as you probably know FTP credentials are easily captured by a MITM attack or Wireshark (not sure if Wireshark does). So I thought if i can detect data capturing in the user’s network the script would stop. Any idea?.
      If there’s another workaround I’m happy to hear it. 
    • By Iznogoud
      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?
×
×
  • Create New...