我们有一个网站,用户必须输入密码。然后,必须将该密码作为安全字符串传递给 powershell 脚本。 我们可以将其作为字符串来执行,但是密码会显示在日志记录中,这是 Not Acceptable 。
我已经搜索过,但找不到如何在 javascript 中创建 secureString。
代码:
<%@ page import="java.io.BufferedReader,
java.io.IOException,
java.io.InputStreamReader"
%>
<%@ page language="java" %>
<%
String psScript=(String)request.getParameter("script");
String cmsnameline=(String)request.getParameter("cmsnameline");
String password=(String)request.getParameter("password");
String[] parts = cmsnameline.split(",");
String cmsname=parts[0];
String user=parts[1];
String creds=parts[2];
%>
<%
String param = ("O:\\SapMaintenanceBoard\\Remote_execute.ps1 -script_file " + (psScript.replaceAll("(\\r|\\n)", "")) + " -servers " + (cmsname.replaceAll("(\\r|\\n)", "")) + " -npa_user " + (user) + " -credFile " + (creds)+ " -password " + (password));
String command = "powershell.exe " + (param);
// Executing the command
Process powerShellProcess = Runtime.getRuntime().exec(command);
// Getting the results
powerShellProcess.getOutputStream().close();
-- Do stuf with the results
%>
和 PowerShell:
Param(
[Parameter(Mandatory=$true, Position=0)]
[String]$script_file
,
[Parameter(Mandatory=$true, Position=1)]
[String]$servers
,
[Parameter(Mandatory=$true, Position=2)]
[String]$user
,
[Parameter(Mandatory=$true, Position=3)]
[String]$credFile
,
[Parameter(Mandatory=$true, Position=4)]
[String]$password
)
在 PowerShell 中,我需要将 [String]$password
替换为 [secureString]$password
。
最佳答案
我可以想到两种方法来做到这一点...两者都会为您提供 $password
作为 System.Security.SecureString
对象。
在脚本/函数中获取字符串输入和 ConvertTo-SecureString
:
function Get-Password1 {
Param (
[Parameter(Mandatory=$True)]
[String]$password
)
$password = $password | ConvertTo-SecureString -AsPlainText -Force
$password
}
Get-Password1 -password "P@55w0rd"
在调用脚本/函数时使用ConvertTo-SecureString
:
function Get-Password2 {
Param (
[Parameter(Mandatory=$True)]
[Security.SecureString]$password
)
$password
}
Get-Password2 -password (ConvertTo-SecureString "P@55w0rd" -AsPlainText -Force)
关于javascript - 如何将密码从 javascript 传递到 powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48701861/