使用powershell,可以使用'&'字符运行另一个应用程序并传入参数。动态生成命令行命令,然后使用powershell调用
一个简单的例子。
$notepad = 'notepad'
$fileName = 'HelloWorld.txt'
# This will open HelloWorld.txt
& $notepad $fileName
这很好。但是如果我想使用业务逻辑来动态生成命令字符串呢?使用相同的简单的例子:
$commandString = @('notepad', 'HelloWorld.txt') -join ' ';
& $commandString
我得到的错误:
The term 'notepad HelloWorld.txt' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
在我实际的例子我想动态添加或删除选项,最后的命令行字符串。有什么办法可以解决这个问题吗?
是否在双引号(“”)帮助引用? – cristobalito