简体   繁体   中英

Executing exe file with option from Powershell

How to run the below command in PowerShell. The below command will prompt for a password and user needs to enter it. I want to automate it.

mxexport [ -f <zip file name> ]

I tried saving password in a file and run the below script in PowerShell:

$password = get-content C:\cred.txt | convertto-securestring
$pass=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
& 'C:\Program Files\XX\XX\bin\mxexport.exe' -f file.zip, -p $pass

But I am getting the below Error:

mxexport.exe'
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Error while getting password from User.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

You are using single quotes, therefore the $pass variable doesn't get resolved. You also don't need to wrap the arguments within quotes, just use:

& "C:\Program Files\XX\XX\bin\mxexport.exe" -p $pass

Edit to your comment

Try to pass the parameters using splatting:

$arguments = @("-f file.zip", "-p $pass")
& "C:\Program Files\XX\XX\bin\mxexport.exe" @arguments

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.

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