Problem
Sie müssen einen Benutzernamen und ein Passwort in Ihrem Skript verwenden und Sie verwenden Windows.
Lösung
Rufen Sie Powershell über RunProgram auf, um das Passwort im Credential Manager zu speichern (in der Startleiste danach suchen), damit Sie es später abrufen können.
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"
)
);
Sie können diese Testanmeldeinformationen mithilfe des „Windows-Anmeldeinformationsspeichers“ löschen.
Idealerweise würden diese Berechtigungsnachweise nie im Arbeitsspeicher gespeichert werden. Sie würden sie in dem Moment anrufen, in dem sie benötigt werden, wie folgt:
request = New HTTP Request(
URL( "https://myserver" ),
Method("GET"),
Username( GetUser( undesc ) ),
Password( GetPass( undesc ) )
);
Hier ist der Powershell-Befehl, den diese Funktion tatsächlich ausführt:
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().PasswordTolle Lösung. Danke für das Teilen.