cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-413797%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EWindows%20%E3%81%AE%20PowerShell%20%E3%82%92%E4%BB%8B%E3%81%97%E3%81%A6%20Credential%20Manager%20%E3%81%A7%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%8A%E3%82%88%E3%81%B3%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%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%E5%95%8F%E9%A1%8C%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%A7%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E5%90%8D%E3%81%A8%E3%83%91%E3%82%B9%E3%83%AF%E3%83%BC%E3%83%89%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B%E5%BF%85%E8%A6%81%E3%81%8C%E3%81%82%E3%82%8A%E3%80%81Windows%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E3%81%84%E3%81%BE%E3%81%99%E3%80%82%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%E8%A7%A3%E6%B1%BA%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ERunProgram%20%E3%82%92%E4%BB%8B%E3%81%97%E3%81%A6%20powershell%20%E3%82%92%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97%E3%80%81%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E3%83%9E%E3%83%8D%E3%83%BC%E3%82%B8%E3%83%A3%E3%83%BC%20(%E3%82%B9%E3%82%BF%E3%83%BC%E3%83%88%20%E3%83%90%E3%83%BC%E3%81%A7%E6%A4%9C%E7%B4%A2)%20%E3%81%AB%E3%83%91%E3%82%B9%E3%83%AF%E3%83%BC%E3%83%89%E3%82%92%E4%BF%9D%E5%AD%98%E3%81%97%E3%81%A6%E3%80%81%E5%BE%8C%E3%81%A7%E5%8F%96%E5%BE%97%E3%81%A7%E3%81%8D%E3%82%8B%E3%82%88%E3%81%86%E3%81%AB%E3%81%97%E3%81%BE%E3%81%99%E3%80%82%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%E3%81%93%E3%82%8C%E3%82%89%E3%81%AE%E3%83%86%E3%82%B9%E3%83%88%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E3%81%AF%E3%80%81%E3%80%8CWindows%20%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E3%82%B9%E3%83%88%E3%82%A2%E3%80%8D%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E5%89%8A%E9%99%A4%E3%81%A7%E3%81%8D%E3%81%BE%E3%81%99%E3%80%82%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%E7%90%86%E6%83%B3%E7%9A%84%E3%81%AB%E3%81%AF%E3%80%81%E3%81%93%E3%82%8C%E3%82%89%E3%81%AE%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E3%81%AF%E6%B1%BA%E3%81%97%E3%81%A6%E3%83%A1%E3%83%A2%E3%83%AA%E3%81%AB%E4%BF%9D%E5%AD%98%E3%81%95%E3%82%8C%E3%81%BE%E3%81%9B%E3%82%93%E3%80%82%20%E6%AC%A1%E3%81%AE%E3%82%88%E3%81%86%E3%81%AB%E3%80%81%E5%BF%85%E8%A6%81%E3%81%AA%E3%81%A8%E3%81%8D%E3%81%AB%E3%81%9D%E3%82%8C%E3%82%89%E3%82%92%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97%E3%81%BE%E3%81%99%E3%80%82%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%E3%81%93%E3%82%8C%E3%82%89%E3%81%AE%E9%96%A2%E6%95%B0%E3%81%8C%E5%AE%9F%E9%9A%9B%E3%81%AB%E5%AE%9F%E8%A1%8C%E3%81%99%E3%82%8Bpowershell%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AF%E6%AC%A1%E3%81%AE%E3%81%A8%E3%81%8A%E3%82%8A%E3%81%A7%E3%81%99%E3%80%82%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%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%84%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AB%E4%BF%9D%E5%AD%98%E3%81%9B%E3%81%9A%E3%81%AB%E3%80%81%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E5%90%8D%E3%81%A8%E3%83%91%E3%82%B9%E3%83%AF%E3%83%BC%E3%83%89%E3%81%AB%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%81%97%E3%81%BE%E3%81%99%E3%80%82%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%20%E3%81%AE%20PowerShell%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%20Credential%20Manager%20%E3%81%A7%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%8A%E3%82%88%E3%81%B3%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-628242%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E5%84%AA%E3%82%8C%E3%81%9F%E3%82%BD%E3%83%AA%E3%83%A5%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%80%82%20%E5%85%B1%E6%9C%89%E3%81%97%E3%81%A6%E3%81%8F%E3%82%8C%E3%81%A6%E3%81%82%E3%82%8A%E3%81%8C%E3%81%A8%E3%81%86%E3%80%82%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.