cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
%3CLINGO-SUB%20id%3D%22lingo-sub-413797%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EWindows%EC%9D%98%20PowerShell%EC%9D%84%20%ED%86%B5%ED%95%B4%20Credential%20Manager%EC%97%90%EC%84%9C%20%EC%9E%90%EA%B2%A9%20%EC%A6%9D%EB%AA%85%20%EC%84%A4%EC%A0%95%20%EB%B0%8F%20%EA%B0%80%EC%A0%B8%EC%98%A4%EA%B8%B0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-413797%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%0A%3CP%3E%3CSTRONG%3E%EB%AC%B8%EC%A0%9C%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C%20%EC%82%AC%EC%9A%A9%EC%9E%90%20%EC%9D%B4%EB%A6%84%EA%B3%BC%20%EC%95%94%ED%98%B8%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%B4%EC%95%BC%20%ED%95%98%EB%A9%B0%20Windows%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%A9%EB%8B%88%EB%8B%A4.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%ED%95%B4%EA%B2%B0%EC%B1%85%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%EB%82%98%EC%A4%91%EC%97%90%20%EA%B2%80%EC%83%89%ED%95%A0%20%EC%88%98%20%EC%9E%88%EB%8F%84%EB%A1%9D%20RunProgram%EC%9D%84%20%ED%86%B5%ED%95%B4%20powershell%EC%9D%84%20%ED%98%B8%EC%B6%9C%ED%95%98%EC%97%AC%20Credential%20Manager%EC%97%90%20%EC%95%94%ED%98%B8%EB%A5%BC%20%EC%A0%80%EC%9E%A5%ED%95%A9%EB%8B%88%EB%8B%A4(%EC%8B%9C%EC%9E%91%20%ED%91%9C%EC%8B%9C%EC%A4%84%EC%97%90%EC%84%9C%20%EA%B2%80%EC%83%89).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20default%20to%20here(1)%3B%0A%0Aloadmodule%20%3D%20%22%0A%20%20if%20(!(Get-Module%20-ListAvailable%20-Name%20CredentialManager))%20%7B%0A%20%20%20Install-Module%20CredentialManager%20-force%20-Scope%20CurrentUser%0A%20%20%7D%0A%20%20%22%3B%0A%0A%0A%2F%2FFunction%20to%20run%20command%20in%20powershell%2C%20strips%20trailing%20line%20breaks%0ARunPowershell%20%3D%20Function(%20%7Bcommand%7D%2C%0A%20Regex(RunProgram(%0A%20%20Executable(%20%22powershell.exe%22%20)%2C%0A%20%20Options(%20%7B%22%2Fc%22%2C%20command%20%7D%20)%2C%0A%20%20ReadFunction(%20%22text%22%20)%0A%20)%2C%20%22%5E(.*%3F)%5B%5Cr%5Cn%5D%2B%24%22%2C%20%22%5C1%22)%0A)%3B%0A%0A%0A%2F%2F%20Set%20password%0ASetPass%20%3D%20Function(%20%7Btarget%2Cuser%2Cpass%7D%2C%0A%20RunPowershell(%0A%20%20%22%24target%20%3D%20'%22%20%7C%7C%20target%20%7C%7C%20%22'%0A%20%20%24usr%20%3D%20'%22%20%7C%7C%20user%20%7C%7C%20%22'%0A%20%20%24pswd%20%3D%20'%22%20%7C%7C%20pass%20%7C%7C%20%22'%0A%20%20%22%20%7C%7C%20loadmodule%20%7C%7C%20%22%0A%20%20New-StoredCredential%20-Target%20%24target%20-UserName%20%24usr%20-Password%20%24pswd%22%20-Persist%20LocalMachine%0A%20)%3B%0A%201%3B%20%2F%2Fpassword%20is%20returned%20as%20free%20text%2C%20don't%20return%20the%20response%0A)%3B%0A%0A%2F%2F%20Get%20password%0AGetPass%20%3D%20Function(%20%7Btarget%7D%2C%0A%20RunPowershell(%0A%20%20%22%24target%20%3D%20'%22%20%7C%7C%20target%20%7C%7C%20%22'%0A%20%20%22%20%7C%7C%20loadmodule%20%7C%7C%20%22%0A%20%20%24creds%20%3D%20Get-StoredCredential%20-Target%20%24target%0A%0A%20%20%24creds.GetNetworkCredential().Password%22%0A%20)%0A)%3B%0A%0A%2F%2F%20Get%20username%0AGetUser%20%3D%20Function(%20%7Btarget%7D%2C%0A%20RunPowershell(%0A%20%20%22%24target%20%3D%20'%22%20%7C%7C%20target%20%7C%7C%20%22'%0A%20%20%22%20%7C%7C%20loadmodule%20%7C%7C%20%22%0A%20%20%24creds%20%3D%20Get-StoredCredential%20-Target%20%24target%0A%0A%20%20%24creds.GetNetworkCredential().UserName%22%0A%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E'Windows%20%EC%9E%90%EA%B2%A9%20%EC%A6%9D%EB%AA%85%20%EC%A0%80%EC%9E%A5%EC%86%8C'%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%EC%9D%B4%EB%9F%AC%ED%95%9C%20%ED%85%8C%EC%8A%A4%ED%8A%B8%20%EC%9E%90%EA%B2%A9%20%EC%A6%9D%EB%AA%85%EC%9D%84%20%EC%82%AD%EC%A0%9C%ED%95%A0%20%EC%88%98%20%EC%9E%88%EC%8A%B5%EB%8B%88%EB%8B%A4.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%EC%9D%B4%EC%83%81%EC%A0%81%EC%9C%BC%EB%A1%9C%20%EC%9D%B4%EB%9F%AC%ED%95%9C%20%EC%9E%90%EA%B2%A9%20%EC%A6%9D%EB%AA%85%EC%9D%80%20%EB%A9%94%EB%AA%A8%EB%A6%AC%EC%97%90%20%EC%A0%80%EC%9E%A5%EB%90%98%EC%A7%80%20%EC%95%8A%EC%8A%B5%EB%8B%88%EB%8B%A4.%20%EB%8B%A4%EC%9D%8C%EA%B3%BC%20%EA%B0%99%EC%9D%B4%20%ED%95%84%EC%9A%94%ED%95%A0%20%EB%95%8C%20%ED%98%B8%EC%B6%9C%ED%95%A0%20%EC%88%98%20%EC%9E%88%EC%8A%B5%EB%8B%88%EB%8B%A4.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Erequest%20%3D%20New%20HTTP%20Request(%0A%20URL(%20%22https%3A%2F%2Fmyserver%22%20)%2C%0A%20Method(%22GET%22)%2C%0A%20Username(%20GetUser(%20undesc%20)%20)%2C%0A%20Password(%20GetPass(%20undesc%20)%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%EC%9D%B4%20%ED%95%A8%EC%88%98%EA%B0%80%20%EC%8B%A4%EC%A0%9C%EB%A1%9C%20%EC%8B%A4%ED%96%89%ED%95%98%EB%8A%94%20powershell%20%EB%AA%85%EB%A0%B9%EC%9D%80%20%EB%8B%A4%EC%9D%8C%EA%B3%BC%20%EA%B0%99%EC%8A%B5%EB%8B%88%EB%8B%A4.%3C%2FP%3E%0A%3CP%3ESetCred%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eparam(%20%24target%2C%20%24usr%2C%20%24pswd%20)%0A%0Aif%20(!Get-Module%20-ListAvailable%20-Name%20SomeModule)%20%7B%0A%20%20%20%20Install-Module%20CredentialManager%20-force%20-Scope%20CurrentUser%0A%7D%0A%0ANew-StoredCredential%20-Target%20%24target%20-UserName%20%24usr%20-Password%20%24pswd%20-Persist%20LocalMachine%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EGetCred%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eparam(%20%24target%20)%0A%0Aif%20(!Get-Module%20-ListAvailable%20-Name%20SomeModule)%20%7B%0A%20%20%20%20Install-Module%20CredentialManager%20-force%20-Scope%20CurrentUser%0A%7D%0A%0A%24creds%20%3D%20Get-StoredCredential%20-Target%20%24target%0A%0A%24creds.GetNetworkCredential().UserName%0A%24creds.GetNetworkCredential().Password%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-413797%22%20slang%3D%22en-US%22%3E%3CP%3E%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%82%98%20%EC%BD%94%EB%93%9C%EC%97%90%20%EC%A0%80%EC%9E%A5%ED%95%98%EC%A7%80%20%EC%95%8A%EA%B3%A0%20%EC%82%AC%EC%9A%A9%EC%9E%90%20%EC%9D%B4%EB%A6%84%EA%B3%BC%20%EC%95%94%ED%98%B8%EC%97%90%20%EC%95%A1%EC%84%B8%EC%8A%A4%ED%95%A9%EB%8B%88%EB%8B%A4.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-SUB%20id%3D%22lingo-sub-628242%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Windows%EC%97%90%EC%84%9C%20PowerShell%EC%9D%84%20%ED%86%B5%ED%95%B4%20Credential%20Manager%EC%97%90%EC%84%9C%20%EC%9E%90%EA%B2%A9%20%EC%A6%9D%EB%AA%85%20%EC%84%A4%EC%A0%95%20%EB%B0%8F%20%EA%B0%80%EC%A0%B8%EC%98%A4%EA%B8%B0%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-628242%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%ED%9B%8C%EB%A5%AD%ED%95%9C%20%EC%86%94%EB%A3%A8%EC%85%98%EC%9E%85%EB%8B%88%EB%8B%A4.%20%EA%B3%B5%EC%9C%A0%ED%95%B4%20%EC%A3%BC%EC%85%94%EC%84%9C%20%EA%B0%90%EC%82%AC%ED%95%A9%EB%8B%88%EB%8B%A4.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Set and get credentials in the Credential Manager via PowerShell on Windows

Problem

You need to use a username and password in your script and you use Windows.

 

Solution

Call powershell via RunProgram to store the password in the Credential Manager (search for it in the start bar) so you can retrieve it later.

 

Names default to here(1);

loadmodule = "
		if (!(Get-Module -ListAvailable -Name CredentialManager)) {
			Install-Module CredentialManager -force -Scope CurrentUser
		}
		";


//Function to run command in powershell, strips trailing line breaks
RunPowershell = Function( {command},
	Regex(RunProgram(
		Executable( "powershell.exe" ),
		Options( {"/c", command } ),
		ReadFunction( "text" )
	), "^(.*?)[\r\n]+$", "\1")
);


// Set password
SetPass = Function( {target,user,pass},
	RunPowershell(
		"$target = '" || target || "'
		$usr = '" || user || "'
		$pswd = '" || pass || "'
		" || loadmodule || "
		New-StoredCredential -Target $target -UserName $usr -Password $pswd" -Persist LocalMachine
	);
	1; //password is returned as free text, don't return the response
);

// Get password
GetPass = Function( {target},
	RunPowershell(
		"$target = '" || target || "'
		" || loadmodule || "
		$creds = Get-StoredCredential -Target $target

		$creds.GetNetworkCredential().Password"
	)
);

// Get username
GetUser = Function( {target},
	RunPowershell(
		"$target = '" || target || "'
		" || loadmodule || "
		$creds = Get-StoredCredential -Target $target

		$creds.GetNetworkCredential().UserName"
	)
);

You can delete these test credentials using the 'windows credential store'.

 

Ideally these credentials would never be stored in memory. You would call them at the moment they are needed, like this:

request = New HTTP Request(
	URL( "https://myserver" ),
	Method("GET"),
	Username( GetUser( undesc ) ),
	Password( GetPass( undesc ) )
);

 

Here is the powershell command these function actually execute:

SetCred:

param( $target, $usr, $pswd )

if (!Get-Module -ListAvailable -Name SomeModule) {
    Install-Module CredentialManager -force -Scope CurrentUser
}

New-StoredCredential -Target $target -UserName $usr -Password $pswd -Persist LocalMachine

 GetCred:

param( $target )

if (!Get-Module -ListAvailable -Name SomeModule) {
    Install-Module CredentialManager -force -Scope CurrentUser
}

$creds = Get-StoredCredential -Target $target

$creds.GetNetworkCredential().UserName
$creds.GetNetworkCredential().Password
Comments
FN

Great solution. Thanks for sharing.

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.