cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
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.