問題
您需要在腳本中使用用戶名和密碼,並且您使用的是 Windows。
解決方案
通過 RunProgram 調用 powershell 將密碼存儲在 Credential Manager 中(在開始欄中搜索),以便稍後檢索。
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"
)
);
您可以使用“windows 憑據存儲”刪除這些測試憑據。
理想情況下,這些憑據永遠不會存儲在內存中。 你會在需要的時候給他們打電話,就像這樣:
request = New HTTP Request(
URL( "https://myserver" ),
Method("GET"),
Username( GetUser( undesc ) ),
Password( GetPass( undesc ) )
);
這是這些函數實際執行的 powershell 命令:
設置信用:
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
獲取信譽:
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