使用Powershell中的选项执行exe文件

时间:2016-06-13 05:39:24

标签: powershell

如何在PowerShell中运行以下命令。以下命令将提示输入密码,用户需要输入密码。我想自动化它。

mxexport [ -f <zip file name> ]

我尝试在文件中保存密码并在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

但我收到以下错误:

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

1 个答案:

答案 0 :(得分:2)

您使用的是单引号,因此$pass变量无法解析。您也不需要将参数包装在引号内,只需使用:

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

修改您的评论

尝试使用splatting传递参数:

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