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. I'm a bit of a novice in powershell, mostly have learned via google fu, so it's hopefully something simple I'm missing. 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:
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. 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. Both commands are necessary for decryption since without the second one, the output of pass would've been a bunch of random numbers.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.