运行 来自 powershell 的 veracrypt

Run veracrypt from powershell

我正在尝试 运行 从 powershell 脚本执行以下命令。

"C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" /create "C:\test veracrypt file.hc" /password alongpasswordisagoodpassword /hash sha512 /encryption serpent /filesystem NTFS /size 100G /dynamic /force /silent

我试过使用

& cmd.exe ""C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" /create "C:\test veracrypt file.hc" /password alongpasswordisagoodpassword /hash sha512 /encryption serpent /filesystem NTFS /size 100G /dynamic /force /silent" 

$command = @'
     & cmd.exe ""C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" /create "C:\test veracrypt file.hc" /password
     alongpasswordisagoodpassword /hash sha512 /encryption serpent /filesystem NTFS /size 100G /dynamic /force /silent"  
          '@
Invoke-Expression -Command:$command

无论我做什么我都会得到错误:

cmd.exe : 'reate' is not recognized as an internal or external command,

'reate' 不是拼写错误,实际上在错误消息中删除了 create 中的 c。我试图转义创建,或用引号引起来,但它一直给我同样的错误。

我也试过将命令放入 bat 文件并调用它,但是即使 运行bat 文件按预期工作,它似乎也没有做任何事情就挂了。

我是 powershell 的新手,怀疑我遗漏了一些明显的东西。我错过了什么?

您应该可以直接使用调用运算符 & 来 运行 命令,而无需使用 cmd:

& "C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" /create "C:\test veracrypt file.hc" /password alongpasswordisagoodpassword /hash sha512 /encryption serpent /filesystem NTFS /size 100G /dynamic /force /silent

来自about_operators

& Call operator

Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters.

以防万一,另一种最终对我有用的方法:

start-process -FilePath "C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" -ArgumentList @("/create", "C:\test veracrypt file.hc", "/password alongpasswordisagoodpassword", "/hash sha512", "/encryption serpent", "/filesystem NTFS", "/size 100G", "/dynamic", "/force", "/silent")