visual-studio-2010 - 如何将 PowerShell 与 Visual Studio 命令提示符结合使用?

标签 visual-studio-2010 visual-studio powershell

我已经使用 Beta 2 一段时间了,运行 Visual Studio 2010 命令提示符时必须输入 cmd.exe,这让我抓狂。我曾经有一个很好的 Visual Studio 2008 vsvars2008.ps1 脚本。是否有 vsvars2010.ps1 脚本或类似的脚本?

最佳答案

从博客文章中大量窃取 Replace Visual Studio Command Prompt with PowerShell ,我能够让它发挥作用。我将以下内容添加到我的 profile.ps1 文件中,一切顺利。

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

这多年来一直运行良好 - 直到 Visual Studio 2015。vcvarsall.bat 不再存在。相反,您可以使用 vsvars32.bat 文件,该文件位于 Common7\Tools 文件夹中。

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

Visual Studio 2017 的情况再次发生变化。vsvars32.bat 似乎已被放弃,取而代之的是 VsDevCmd.bat。确切的路径可能会有所不同,具体取决于您使用的 Visual Studio 2017 版本。

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

您还可以使拆分仅创建两个项目,以避免破坏包括等号在内的值,等号也是环境变量名称和值的分隔符:

$v = $_.split("=", 2); set-item -force -path "ENV:\$($v[0])"  -value 

Visual Studio 2022 的细微变化,现在是 64 位了。

pushd "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools"
cmd /c "VsDevCmd.bat&set" |
foreach {
  if ($_ -match "=") {
    $v = $_.split("=", 2); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])" 
  }
}
popd
Write-Host "`nVisual Studio 2022 Command Prompt variables set." -ForegroundColor Yellow

关于visual-studio-2010 - 如何将 PowerShell 与 Visual Studio 命令提示符结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2124753/

相关文章:

c# - Visual Studio,F11,只对特定线程正在做什么感兴趣?

c# - 为什么 Visual Studio 中的循环引用是一种不好的做法?

asp.net-mvc - 如何更改 jslint(VS 2010 扩展名)以忽略文件?

powershell - 通过 PowerShell 向 applicationHost.config 添加 <location/> 元素

c# - C# 中 Pipeline 和 PowerShell 类的区别

c++ - 外部符号上的静态链接 OpenCV CUDA 应用程序错误

c++ - 具有自定义哈希函数 (_bstr_t) 的无序映射仅适用于默认构造函数(重复键)

c# - 在开发期间管理多个应用程序配置文件

c# - 我可以使用 .NET Core 编写 PowerShell 二进制 cmdlet 吗?

c++ - Visual Studio 2010 Assimp 库不会链接