我有一些场景中,我需要传递的参数命令行前男友。
I have a number of scenarios in which I need to pass parameters to command line exes.
我已经看到了这些回答在本网站一定程度上,但到目前为止,我还没有看到实,通用如何PowerShell的交易,描述与参数 - 两者都通过了CMD或启动过程。这里有一个简单的例子,它的错误我:
I've seen a number of these answered to some extent on this site, but so far I have not seen solid, general purpose, description of how powershell deals with parameters - both being passed by cmd or start-process. Here's one trivial example which bugs me:
下面是蝙蝠脚本:
CD /D %ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Common7\IDE
devenv /command "File.BatchNewTeamProject C:\stuff\Project51.xml"
我不明白的是什么,是如何PowerShell是分析这些论点。这是什么考虑参数?
What I do not understand, is how powershell is parsing these arguments. What does it consider a "parameter"?
另外,是规则相同的启动过程,因为它们是cmd.exe的?
Also, are the rules the same for start-process as they are for cmd.exe ?
试试这个功能从 PowerShell.com未来 PowerTip它说明调用-Ex的pression 。
Try this function coming from PowerShell.com PowerTip it illustrate the usage of Invoke-Expression
.
function Call {
$command = $Args -join " "
$command += " 2>&1"
$result = Invoke-Expression($command)
$result |
%{$e=""}{ if( $_.WriteErrorStream ) {$e += $_ } else {$_} }{Write-Warning $e}
}
这给了:
cd "${env:ProgramFiles(x86)}\Microsoft Visual Studio 10.0\Common7\IDE"
call .\devenv.exe /command "`"File.BatchNewTeamProject C:\stuff\Project51.xml`"
---编辑---
--- Edit ---
有很多事情要在这里说。
There are many things to say here.
第一,你可以找到一个很好的帮助,关于文件试试:
First you can find a good help with "about" files try :
获取帮助约 - *
Get-help about-*
在这个问题你是你有兴趣:
On the subject you are interested you've got:
Get-help about_Quoting_Rules
Get-Help about_Special_Characters
Get-Help about_Escape_Characters
Get-Help about_Parameters
二 CD,DIR,MD
工作,但他们只是别名上的cmdlet这需要不同的参数。
Second CD, DIR, MD
works, but they are just aliases on CmdLets which takes different arguments.
第三,以获得环境变量,它不再是的%systemroot%
是 $ ENV:SYSTEMROOT
。
Third to get environment variable it's no longer %systemroot%
it's $env:systemroot
.
第四以从PowerShell中启动一个可执行文件,你可以只输入EXE的名称:
Fourth to start an executable file from powershell you can just type the name of the exe :
PS> notepad c:\temp\test.txt
该命令行是通过PowerShell的PTED所以现在,如果你写的第一间$ P $:
The command line is first interpreted by powerShell so now if you write :
PS> "C:\Windows\System32\notepad.exe"
C:\Windows\System32\notepad.exe
这只是跨preT它作为一个字符串。所以,你可以使用&放大器;运营商和写
It just interpret it as a string. So you can use the & operator and write
PS> & "C:\Windows\System32\notepad.exe" c:\test.txt
它的工作原理,但:
It works but :
PS> $a = "C:\Windows\System32\notepad.exe c:\test.txt"
PS> & $a
失败,
PS> $a = "C:\Windows\System32\notepad.exe c:\test.txt"
PS> Invoke-Expression $a
作品