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%3EConfigure%20y%20obtenga%20credenciales%20en%20Credential%20Manager%20a%20trav%C3%A9s%20de%20PowerShell%20en%20Windows%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%3EProblema%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EDebe%20usar%20un%20nombre%20de%20usuario%20y%20una%20contrase%C3%B1a%20en%20su%20secuencia%20de%20comandos%20y%20usa%20Windows.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESoluci%C3%B3n%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ELlame%20a%20powershell%20a%20trav%C3%A9s%20de%20RunProgram%20para%20almacenar%20la%20contrase%C3%B1a%20en%20Credential%20Manager%20(b%C3%BAsquela%20en%20la%20barra%20de%20inicio)%20para%20que%20pueda%20recuperarla%20m%C3%A1s%20tarde.%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%3EPuede%20eliminar%20estas%20credenciales%20de%20prueba%20mediante%20el%20%22almac%C3%A9n%20de%20credenciales%20de%20Windows%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIdealmente%2C%20estas%20credenciales%20nunca%20se%20almacenar%C3%ADan%20en%20la%20memoria.%20Los%20llamar%C3%ADas%20en%20el%20momento%20en%20que%20se%20necesitan%2C%20as%C3%AD%3A%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%3EAqu%C3%AD%20est%C3%A1%20el%20comando%20de%20powershell%20que%20estas%20funciones%20realmente%20ejecutan%3A%3C%2FP%3E%0A%3CP%3EEstablecer%20Cred%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%3EObtener%20cr%C3%A9dito%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%3EAcceda%20a%20nombres%20de%20usuario%20y%20contrase%C3%B1as%20sin%20guardarlos%20en%20su%20script%20o%20c%C3%B3digo.%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%20Establecer%20y%20obtener%20credenciales%20en%20Credential%20Manager%20a%20trav%C3%A9s%20de%20PowerShell%20en%20Windows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-628242%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EGran%20soluci%C3%B3n.%20Gracias%20por%20compartir.%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.