![](/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.