【问题标题】:Pass a variable to a cmd.exe command in PowerShell将变量传递给 PowerShell 中的 cmd.exe 命令
【发布时间】:2020-09-08 07:19:04
【问题描述】:

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

$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"

这是我使用的报告软件,它具有用于编写脚本的命令行参数,因此我尝试传递所需的开始日期和结束日期,这样它就可以运行所有内容,而无需用户登录服务器(我有已经搞定了)。

【问题讨论】:

  • 嗨 Hunter,您能否更新您的问题,详细说明到底出了什么问题?例如您是否看到任何错误消息,或者它只是挂起等?
  • 尝试使用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 中工作,例如Start-Process -FilePath "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”键。

标签: powershell cmd command-line expression command-line-arguments


【解决方案1】:

从 powershell 向 cmd 传递参数时,您应该逐个传递每个参数。就像在问题的 cmets 中讨论的那样,您提供的场景的正确语法是:

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"

由于这不起作用,您可能在将参数传递给文件时使用了不正确的语法,就像 cmets 一样,您应该删除括号 ([])。您的代码将是:

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"

如果密码是错误的,你可以把括号放回去。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2022-12-13
    • 2013-05-01
    • 2014-02-19
    • 2019-02-16
    • 2017-12-15
    • 2012-06-24
    相关资源
    最近更新 更多