How can I use PowerShell with the Visual Studio Command Prompt?

前端 未结 9 1593
一向
一向 2020-12-02 03:44

I\'ve been using Beta 2 for a while now and it\'s been driving me nuts that I have to punt to cmd.exe when running the VS2010 Command Prompt. I used to have a nice vsvars200

相关标签:
9条回答
  • 2020-12-02 04:34

    For someone who is still struggling with it in 2020 and Visual Studio Code 1.41.1, so a bit off topic here.

    Using all different parts of code from above and the Internet e.g. from https://help.appveyor.com/discussions/questions/18777-how-to-use-vcvars64bat-from-powershell and with a step by step approach I managed to have the below script working.

    Saved into VSCode "settings.json" and with the Code Runner extension installed.

    With Microsoft (R) C/C++ Optimizing Compiler Version "cl.exe" from Visual Studio 2015/14.0:

    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {
      "cpp": "cd $dir; pushd \"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\"; cmd.exe /c \"call vcvarsall.bat x86_amd64 & set > %temp%\\vcvars.txt\"; Get-Content \"$env:temp\\vcvars.txt\" | Foreach-Object { if ($_ -match \"^(.*?)=(.*)$\") {   Set-Content \"env:\\$($matches[1])\" $matches[2]  }}; popd; cls; cl *.cpp; .\\\"$fileNameWithoutExt.exe\"; Write-Host -NoNewLine 'Press any key to continue...';  $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); del \"$fileNameWithoutExt.exe\"; del \"$fileNameWithoutExt.obj\""}
    

    With Microsoft (R) C/C++ Optimizing Compiler Version "cl.exe" from Visual Studio 2019/16.4.3:

    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {
      "cpp": "cd $dir; pushd \"c:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\"; cmd.exe /c \"call vcvarsall.bat x86_amd64 & set > %temp%\\vcvars.txt\"; Get-Content \"$env:temp\\vcvars.txt\" | Foreach-Object { if ($_ -match \"^(.*?)=(.*)$\") {   Set-Content \"env:\\$($matches[1])\" $matches[2]  }}; popd; cls; cl *.cpp; .\\\"$fileNameWithoutExt.exe\"; Write-Host -NoNewLine 'Press any key to continue...';  $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); del \"$fileNameWithoutExt.exe\"; del \"$fileNameWithoutExt.obj\""}
    

    HTH

    0 讨论(0)
  • 2020-12-02 04:37

    The simplest option is to run the VS 2010 command prompt and then start PowerShell.exe. If you really want to do this from your "home" PowerShell prompt, the approach you show is the way to go. I use a script that Lee Holmes wrote a while back:

    <#
    .SYNOPSIS
       Invokes the specified batch file and retains any environment variable changes
       it makes.
    .DESCRIPTION
       Invoke the specified batch file (and parameters), but also propagate any  
       environment variable changes back to the PowerShell environment that  
       called it.
    .PARAMETER Path
       Path to a .bat or .cmd file.
    .PARAMETER Parameters
       Parameters to pass to the batch file.
    .EXAMPLE
       C:\PS> Invoke-BatchFile "$env:VS90COMNTOOLS\..\..\vc\vcvarsall.bat"       
       Invokes the vcvarsall.bat file to set up a 32-bit dev environment.  All 
       environment variable changes it makes will be propagated to the current 
       PowerShell session.
    .EXAMPLE
       C:\PS> Invoke-BatchFile "$env:VS90COMNTOOLS\..\..\vc\vcvarsall.bat" amd64      
       Invokes the vcvarsall.bat file to set up a 64-bit dev environment.  All 
       environment variable changes it makes will be propagated to the current 
       PowerShell session.
    .NOTES
       Author: Lee Holmes    
    #>
    function Invoke-BatchFile
    {
       param([string]$Path, [string]$Parameters)  
    
       $tempFile = [IO.Path]::GetTempFileName()  
    
       ## Store the output of cmd.exe.  We also ask cmd.exe to output   
       ## the environment table after the batch file completes  
       cmd.exe /c " `"$Path`" $Parameters && set > `"$tempFile`" " 
    
       ## Go through the environment variables in the temp file.  
       ## For each of them, set the variable in our local environment.  
       Get-Content $tempFile | Foreach-Object {   
           if ($_ -match "^(.*?)=(.*)$")  
           { 
               Set-Content "env:\$($matches[1])" $matches[2]  
           } 
       }  
    
       Remove-Item $tempFile
    }
    

    Note: this function will be available in the PowerShell Community Extensions 2.0 module-based release coming soon.

    0 讨论(0)
  • 2020-12-02 04:39

    Stealing liberally from here: http://allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html, I was able to get this to work. I added the following to my profile.ps1 and all is well with the world.

    pushd 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC'
    cmd /c "vcvarsall.bat&set" |
    foreach {
      if ($_ -match "=") {
        $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
      }
    }
    popd
    write-host "`nVisual Studio 2010 Command Prompt variables set." -ForegroundColor Yellow
    

    This has worked well for years - until Visual Studio 2015. vcvarsall.bat no longer exists. Instead, you can use the vsvars32.bat file, which is located in the Common7\Tools folder.

    pushd 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools'    
    cmd /c "vsvars32.bat&set" |
    foreach {
      if ($_ -match "=") {
        $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
      }
    }
    popd
    write-host "`nVisual Studio 2015 Command Prompt variables set." -ForegroundColor Yellow
    

    Things have changed yet again for Visual Studio 2017. vsvars32.bat appears to have been dropped in favor of VsDevCmd.bat. The exact path may vary depending on which edition of Visual Studio 2017 you're using.

    pushd "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools"
    cmd /c "VsDevCmd.bat&set" |
    foreach {
      if ($_ -match "=") {
        $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
      }
    }
    popd
    Write-Host "`nVisual Studio 2017 Command Prompt variables set." -ForegroundColor Yellow
    
    0 讨论(0)
提交回复
热议问题