Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量传递给PowerShell中的cmd.exe命令_Powershell_Cmd_Command Line_Expression_Command Line Arguments - Fatal编程技术网

将变量传递给PowerShell中的cmd.exe命令

将变量传递给PowerShell中的cmd.exe命令,powershell,cmd,command-line,expression,command-line-arguments,Powershell,Cmd,Command Line,Expression,Command Line Arguments,我正在尝试编写一个powershell脚本,该脚本将远程登录到服务器并运行cmd.exe表达式,但我希望将用户输入的变量传递给cmd.exe表达式。我是powershell的新手,主要是通过GoogleFu学习的,所以我希望这是我缺少的一些简单的东西。请参见下面的脚本: $cred = read-host "Enter Username" - AsString $pass = read-host "Enter Password" -AsSecureString $startdate = read

我正在尝试编写一个powershell脚本,该脚本将远程登录到服务器并运行cmd.exe表达式,但我希望将用户输入的变量传递给cmd.exe表达式。我是powershell的新手,主要是通过GoogleFu学习的,所以我希望这是我缺少的一些简单的东西。请参见下面的脚本:

$cred = read-host "Enter Username" - AsString
$pass = read-host "Enter Password" -AsSecureString
$startdate = read-host "Enter Start Date" -AsString
$enddate = read-host "Enter End Date" -AsString
cmd.exe /c "C:\users\mfinch\desktop\tms\repgen.exe name=mappayman user=$cred pass=$pass 
printmode=export selectall=y startdate=[$startdate] enddate=[$enddate] auto=c"

这是我使用的一个报告软件,它具有用于脚本编写的命令行参数,因此我尝试传递所需的开始日期和结束日期,这样它将运行所有操作,而无需用户登录服务器(我已经解决了这一点)。

当从powershell向cmd传递参数时,您应该逐个传递每个参数。正如您在问题的评论中所讨论的,您提供的场景的正确语法为:

saps cmd.exe -argumentlist "/c", "C:\users\mfinch\desktop\tms\repgen.exe", "name=mappayman", "user=$cred", "pass=$pass", "printmode=export", "selectall=y", "startdate=[$startdate]", "enddate=[$enddate]", "auto=c"
由于这不起作用,您可能有不正确的语法将参数传递到文件中,因此与注释类似,您应该删除括号(
[]
)。您的代码将是:

saps cmd.exe -argumentlist "/c", "C:\users\mfinch\desktop\tms\repgen.exe", "name=mappayman", "user=$cred", "pass=$pass", "printmode=export", "selectall=y", "startdate=$startdate", "enddate=$enddate", "auto=c"
但是,代码中还有另一个语法错误。读取
$pass
变量时,将其作为安全字符串读取,然后加密密码值。在将其作为参数传递之前,首先需要对其进行解密:

$pass = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto($pass)
然后将传递实际输入,而不是输出的加密版本:

System.Security.SecureString
回显
$pass
变量时。这两个命令都是解密所必需的,因为如果没有第二个命令,pass的输出将是一组随机数

总之,您的代码是:

$cred = read-host "Enter Username" - AsString
$pass = read-host "Enter Password" -AsSecureString
$startdate = read-host "Enter Start Date" -AsString
$enddate = read-host "Enter End Date" -AsString
$pass = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto($pass)
saps cmd.exe -argumentlist "/c", "C:\users\mfinch\desktop\tms\repgen.exe", "name=mappayman", "user=$cred", "pass=$pass", "printmode=export", "selectall=y", "startdate=$startdate", "enddate=$enddate", "auto=c"

如果密码是错误的,您可以将括号放回。

Hi Hunter,请更新您的问题,详细说明到底出了什么问题?e、 g.您是否看到任何错误消息,或者它只是挂起等?请尝试使用
saps cmd.exe-argumentlist”/c、“\c:\users\mfinch\desktop\tms\repgen.exe”、“name=mappayman”、“user=$cred”、“pass=$pass”、“printmode=export”、“selectall=y”、“startdate=[$startdate]、“enddate=[$enddate]、“auto=c”
@Bassie以上面写的方式运行它会导致它挂起。我假设它启动了应用程序,但由于变量没有传递日期,应用程序实际上没有运行报告。如果这是硬编码的方式,为什么要将
$startdate
$enddate
都括在括号中?您是否尝试过@NekoMusume的建议,但删除了这些括号?另外,您确定需要在
cmd.exe
中运行此命令吗?您的可执行文件可能在
powershell.exe
中直接运行良好,例如
启动进程-文件路径“C:\users\mfinch\desktop\tms\repgen.exe”-ArgumentList“name=mappayman”、“user=$cred”、“pass=$pass”、“printmode=export”、“selectall=y”、“startdate=$startdate”、“enddate=$enddate”、“auto=C”
。根据您的情况,您可能还需要使用
-WorkingDirectory
选项。要了解如何使用该命令及其选项,请在PowerShell提示符下键入
Get Help Start Process-Full
,然后按“ENTER”键。