
基本上我想傳遞命令<command>
,就好像它們是在 powershell 會話中執行的一樣PS >
,由CreateProcess
powershell -Command <command>
上述方法並不適用於所有情況。例如,這不起作用
powershell -Command gci "C:\Program Files"
這可行
powershell -Command choco -v
答案1
powershell
檢查傳遞給使用腳本區塊語法的有效命令{}
,如下所示:
powershell -Command {gci "C:\Program Files"}
gci C:\Program Files
看來cmd
翻譯員吃掉 雙引號。因此,您需要逃脫字串內部的空格C:\Program Files
,例如如下:
powershell -Command gci "C:\Program` Files" # using backtick
或使用引號(以下任何命令列都應該有效):
powershell -Command gci """C:\Program Files""" # inner double quotes
powershell -Command gci "'C:\Program Files'" # inner single quotes
powershell -Command gci 'C:\Program Files' # single quotes