Padmanabhan Vijendran
Padmanabhan Vijendran

Reputation: 316

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

Upvotes: 1

Views: 328

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 59031

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

Upvotes: 2

Related Questions