Passing parameters to exe

后端 未结 2 1124
一个人的身影
一个人的身影 2021-01-14 15:19

I have a number of scenarios in which I need to pass parameters to command line exes.

I\'ve seen a number of these answered to some extent on this site, but so far I

2条回答
  •  甜味超标
    2021-01-14 16:21

    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}
    }
    

    That gives :

    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
    

    Second CD, DIR, MD works, but they are just aliases on CmdLets which takes different arguments.

    Third to get environment variable it's no longer %systemroot% it's $env:systemroot.

    Fourth to start an executable file from powershell you can just type the name of the exe :

    PS> notepad c:\temp\test.txt
    

    The command line is first interpreted by powerShell so now if you write :

    PS> "C:\Windows\System32\notepad.exe"
    C:\Windows\System32\notepad.exe
    

    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
    

    Fails and

    PS> $a = "C:\Windows\System32\notepad.exe c:\test.txt"
    PS> Invoke-Expression $a
    

    Works

提交回复
热议问题