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