Link to home
Start Free TrialLog in
Avatar of Affaan Abyaz
Affaan AbyazFlag for India

asked on

PowerShell form to use stored credentials and reuse

trying to prepare a form which can be reused for any project to re-validate the credential with one-time-saved credentials...

it should save the credentials locally and next time when same user try to login the credentials it should auto use the locally stored/cached credentials...

don't know where it is going wrong but really appreciate if you could help me fixing it.

Cheers
Aamer
Save-Creds.txt
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I believe you mean to say you need help with storing and recalling encrypted credentials with code that checks for them and allows re-use, correct?
 
This code will check if an encrypted password file exists for the session, and if it does, it will load the credential, otherwise it will request the user provide the information.

# =================================== #
# = Define Parameters			= #
param(
	#$DebugPreference="SilentlyContinue",
	$DebugPreference="Continue",
) 

Write-Debug "Debug-Preferences:`r"
Write-Debug "DebugPreference - $DebugPreference`r"

# =================================== #
# = Define Functions				= #

## Function: Get-IsISE

function Get-IsISE {
# Tests whether the current environment contains the $psISE Variable which is normally only set when running in ISE
    try {    
        return $psISE -ne $null;
    }
    catch {
        return $false;
    }
}
# =================================== #
# = Set Variables					= #


#######
## Set the script Path (If in ISE, set it to a manual value, otherwise use script location value)
$scriptPath = 'C:\Admin\Scripts\Experts-Exchange\29015543\Group-Members-to-a-csv-file-using-PowerShell'
if ( -not ( Get-IsISE ) ) {
	# Get Path for this script File and set it to a variable
	$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
}

# Setup Encrypted Key for Credentials
$key=(3,4,2,3,56,34,254,222,1,1,2,23,42,54,33,233,1,34,2,7,6,5,35,43) # Default example of 192 bit key from MS for this example


# Set Connection Details:
$TargetName = "Target_System"
$TargetUsername = "Domain\Username"
$DtTargetPasswordFile = "$ScriptPath\DT_Credential_$($DtTargetName)_$($DtTargetUserName -replace '\\','_').txt"

#Test if Target Credential Files already Exist
If ( -not( Test-Path -path $TargetPasswordFile ) ) {
	write-debug "Target Credential File not located at ""$TargetPasswordFile"""
	$TargetCredentialEncrypted = Get-Credential -Username $TargetUserName -message "Enter Target Password ($TargetName)"
	$TargetCredentialEncrypted.Password | ConvertFrom-SecureString -key $key | Set-Content $TargetPasswordFile
} Else {
	write-debug "Target Credentials Found, Continuing."
}


## Load target Target Credentials from the files
$TargetPasswordlEncrypted = Get-Content $TargetPasswordFile | ConvertTo-SecureString -key $key #Load encrypted password file for Target user
$TargetCredentialEncrypted = New-Object System.Management.Automation.PsCredential($TargetUserName, $TargetPasswordlEncrypted) 

## End

Open in new window

Avatar of Affaan Abyaz

ASKER

Thanks Ben.. that works well.. Now i have a solution until storing the creds... but let's say if I want to reuse.. how to do that ?

if I need to call the saved credentials using get-credentials then what would be the syntax ?

Regards,
Aamer
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great.. Thank You... let me give a try.

Cheers
Aamer
Thank You.
Hey Affaan, glad to help :)