Jump to content

Trying to SaveCred with Net Use


gcue
 Share

Recommended Posts

hello world!

i am trying to save credentials in windows 7 using net use /savecred.  however not sure why i am getting a wrong password error - password is correct, account is not locked and when i try the same username and same password manually (not via script - it works).  i also have a pass check function which also verifies the password is correct.

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Login()

Func Login()

    $username = @UserName
    $password = InputBox("", "", "", "*", 200, 130)

    If @error Then Exit

    $valid = _ValidUserPass($username, @LogonDomain, $password)

    If $valid = False Then
        MsgBox(0, "", "wrong password")
        Login()
    Else
        Example($username, $password)
    EndIf

EndFunc   ;==>Login

Func Example($username, $password)

    $pc_hostname = "PC2310"

    DriveMapDel("Z:")

    Local $iPID = Run(@ComSpec & " /c net use z: \\" & $pc_hostname & "\c$ /SAVECRED", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDIN_CHILD + $STDOUT_CHILD)
    Local $sOutput = ""
    While 1
        $sOutput = StdoutRead($iPID)
        If @error Then
            ExitLoop
        EndIf

        If $sOutput = "" Then ContinueLoop

        ConsoleWrite($sOutput & @CRLF)

        If StringInStr($sOutput, "name") <> 0 Then
            StdinWrite($iPID, $username)
            ConsoleWrite("user entered" & @CRLF)
        EndIf

        If StringInStr($sOutput, "password") <> 0 Then
            StdinWrite($iPID, $password)
            ConsoleWrite("pass entered" & @CRLF)
            StdinWrite($iPID)
        EndIf
    WEnd

    While 1
        $sOutput = StderrRead($iPID)
        If @error Then
            ExitLoop
        EndIf

        ConsoleWrite($sOutput & @CRLF)
        Exit
    WEnd

EndFunc   ;==>Example

Func _ValidUserPass($username, $computer, $password)

    Local $valid = True
    RunAs($username, $computer, $password, 0, @ComSpec & " /c  echo test", @SystemDir, @SW_HIDE)
    If @error Then $valid = False
    Return $valid

EndFunc   ;==>_ValidUserPass

any help is greatly appreciated

thanks

Link to comment
Share on other sites

I found something interesting on the web about using net use with the /user: and /savecred options.  Hit the link below for the full source, but the meat of the conversion is copied below in italics

From: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/8fc5d667-c1f9-4670-a6e1-3ba2c5e8603d/net-use-savcred-user-conflicting-switches?forum=ITCG

Quote

 

i spend my whole day looking for an answer to similar question and i finally found one, so i though it would be nice to share it with you.  /savecred option sure doesn`t work like it looks and it is a dead end if you want to do what you are writing about. The conflicting switches are /user and /savecred. What you really want to do is to add something to windows credential manager and then windows will automagicly use it to logon to a persistent share on next logon.

So first add credentials:
cmdkey /add:%Server% /User:%domain%\%user% /Pass:%PWD%

Then using same credentials mount the share
net use P: \\%Server%\share /user:%domain%\%user% %PWD%  /persistent:yes

You can check stored credentials by typing "control keymgr.dll" in RUN

And here you go, next reboot gives you nothing but still working shares.

 

Maybe that will help.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...