简体   繁体   English

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

[英]Pass a variable to a cmd.exe command in PowerShell

I'm trying to write a powershell script that will sign on to a server remotely and run a cmd.exe expression, but I want to pass user entered variables to the cmd.exe expression.我正在尝试编写一个 powershell 脚本,该脚本将远程登录服务器并运行 cmd.exe 表达式,但我想将用户输入的变量传递给 cmd.exe 表达式。 I'm a bit of a novice in powershell, mostly have learned via google fu, so it's hopefully something simple I'm missing.我是 powershell 的新手,主要是通过 google fu 学习的,所以希望我缺少一些简单的东西。 See script below:请参阅下面的脚本:

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

This is a reporting software I use that has command line parameters for scripting so I'm trying to pass the start date and end date desired so it will run everything without the user needing a sign on for the server (I have that bit worked out already).这是我使用的报告软件,它具有用于脚本的命令行参数,所以我试图传递所需的开始日期和结束日期,这样它就可以运行所有内容,而无需用户登录服务器(我已经解决了这个问题已经)。

When passing arguments to cmd from powershell, you should pass each argument 1 by 1. Like discussed in the comments of your question, the proper syntax with the scenario you provided would be:当从 powershell 将 arguments 传递给 cmd 时,您应该将每个参数 1 逐 1 传递。就像在问题的评论中讨论的那样,您提供的场景的正确语法是:

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"

and since this doesn't work, you probably have improper syntax with passing the argument to the file so like again, the comments, you should remove the brackets ( [] ).并且由于这不起作用,您可能在将参数传递给文件时语法不正确,因此再次像注释一样,您应该删除括号( [] )。 Your code would then be:您的代码将是:

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"

However, there is another syntax error in your code.但是,您的代码中还有另一个语法错误。 When you read the $pass variable, you read it as a secure string which then encrypts the password value.当您读取$pass变量时,您将其作为安全字符串读取,然后加密密码值。 You first need to decrypt it before passing it as an argument with:在将其作为参数传递之前,您首先需要对其进行解密:

$pass = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto($pass)

Which would then pass the actual input instead of the encrypted version which would've outputted:然后它将传递实际输入而不是输出的加密版本:

System.Security.SecureString

when echoing out the $pass variable.当回显$pass变量时。 Both commands are necessary for decryption since without the second one, the output of pass would've been a bunch of random numbers.这两个命令都是解密所必需的,因为如果没有第二个命令,则通过的 output 将是一堆随机数。

So in all, your code would be:所以总而言之,您的代码将是:

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

and if the password was the error, you could put the brackets back in.如果密码是错误的,你可以把括号放回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM