Problema
Devi usare un nome utente e una password nel tuo script e usi Windows.
Soluzione
Chiama PowerShell tramite RunProgram per memorizzare la password nel Credential Manager (cercala nella barra di avvio) in modo da poterla recuperare in seguito.
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"
)
);
È possibile eliminare queste credenziali di prova utilizzando l'archivio delle credenziali di Windows.
Idealmente queste credenziali non sarebbero mai archiviate in memoria. Li chiameresti nel momento in cui sono necessari, in questo modo:
request = New HTTP Request(
URL( "https://myserver" ),
Method("GET"),
Username( GetUser( undesc ) ),
Password( GetPass( undesc ) )
);
Ecco il comando PowerShell che queste funzioni effettivamente eseguono:
ImpostaCred:
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
Ottieni credito:
param( $target )
if (!Get-Module -ListAvailable -Name SomeModule) {
Install-Module CredentialManager -force -Scope CurrentUser
}
$creds = Get-StoredCredential -Target $target
$creds.GetNetworkCredential().UserName
$creds.GetNetworkCredential().PasswordOttima soluzione. Grazie per la condivisione.