![](/img/trans.png)
[英]Is there a simple method for executing command line statements, in windows, from within python?
[英]Executing a simple powershell command on the command line
我嘗試通過設置變量並打印來運行簡單的powershell命令。
這就是我想做的:
powershell -command "& {$name=\"hi\"; echo $name}"
但是它失敗了:
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
invoke操作符( &
)可以很好地與以下命令配合使用:
powershell -command "& {&echo hi}"
我看了一下調用操作,以及如何執行與命令-command
和執行腳本使用選項-File
選項等,它們是可以預期工作。 但是我嘗試執行相同的操作來設置變量並如上所述打印它不起作用。 我懷疑-command
僅適用於command 。 任何想法如何實現我在上面的工作?
從DOS外殼中可以正常工作:
powershell -command "& {$name='hi'; echo $name}"
而且您的代碼也可以。
在Powershell控制台中使用以下命令:
powershell -command {$name='hi'; echo $name}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.