2

我正在尝试从 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' 未被识别为内部或外部命令,

'reate' 不是错字,来自 create 的 c 实际上已在错误消息中删除。我试图逃避创建,或者加上引号,但它一直给我同样的错误。

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

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

4

2 回答 2

5

您应该能够使用调用运算符&直接运行命令,而无需使用 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

&呼叫接线员

运行命令、脚本或脚本块。调用运算符,也称为“调用运算符”,可让您运行存储在变量中并由字符串表示的命令。因为调用操作符不解析命令,所以它不能解释命令参数。

于 2018-09-04T09:38:14.723 回答
0

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

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")

于 2020-12-20T08:15:10.407 回答