Antworten:
Von hier aus großzügig zu stehlen: http://allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html , konnte ich dies zum Laufen bringen. Ich habe folgendes zu meinem profile.ps1 hinzugefügt und alles ist gut mit der Welt.
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
Dies hat jahrelang gut funktioniert - bis Visual Studio 2015. vcvarsall.bat existiert nicht mehr. Stattdessen können Sie die Datei vsvars32.bat verwenden, die sich im Ordner Common7 \ Tools befindet.
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
Für Visual Studio 2017 vsvars32.bat
haben sich die Dinge erneut geändert VsDevCmd.bat
. Der genaue Pfad kann je nach verwendeter Edition von Visual Studio 2017 variieren.
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
echo $Profile
um den beabsichtigten Pfad für Ihr profile.ps1 zu sehen, falls Sie ihn noch nie erstellt haben
Am einfachsten ist es, die VS 2010-Eingabeaufforderung auszuführen und anschließend PowerShell.exe zu starten. Wenn Sie dies wirklich über Ihre PowerShell-Eingabeaufforderung "zu Hause" tun möchten, ist der von Ihnen gezeigte Ansatz der richtige Weg. Ich benutze ein Skript, das Lee Holmes vor einiger Zeit geschrieben hat:
<#
.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
}
Hinweis: Diese Funktion wird in Kürze in der modulbasierten Version von PowerShell Community Extensions 2.0 verfügbar sein .
Ich habe hier eine einfache Methode gefunden : Ändern Sie die Verknüpfung.
Die ursprüngliche Verknüpfung sieht ungefähr so aus:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat""
Fügen Sie & powershell
vor dem letzten Zitat Folgendes hinzu:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" & powershell"
Wenn Sie möchten, dass es eher wie PS aussieht, wechseln Sie zur Registerkarte Farben der Verknüpfungseigenschaften und setzen Sie die Werte für Rot, Grün und Blau auf 1, 36 bzw. 86.
Eine alte Frage, die aber eine andere Antwort wert ist, um (a) VS2013-Unterstützung bereitzustellen; (b) das Beste aus zwei vorherigen Antworten kombinieren; und (c) einen Funktionswickler bereitstellen.
Dies baut auf @ Andys Technik auf (die auf Allen Macks Technik aufbaut, wie Andy angegeben hat (die wiederum auf Robert Andersons Technik aufbaut, wie Allen angegeben hat) (alle hatten einen leichten Fehler, wie auf dieser Seite vom Benutzer angegeben, der nur als "me-" bekannt ist. - ", also habe ich das auch berücksichtigt))).
Hier ist mein endgültiger Code - beachten Sie die Verwendung des nicht gierigen Quantifizierers in der Regex, um mögliche eingebettete Gleichheiten in den Werten zu behandeln. Dies vereinfacht auch den Code: eine einzelne Übereinstimmung anstelle einer Übereinstimmung, die dann wie in Andys Beispiel aufgeteilt wird, oder eine Übereinstimmung, dann indexof und Teilzeichenfolgen wie in "me -".
function Set-VsCmd
{
param(
[parameter(Mandatory, HelpMessage="Enter VS version as 2010, 2012, or 2013")]
[ValidateSet(2010,2012,2013)]
[int]$version
)
$VS_VERSION = @{ 2010 = "10.0"; 2012 = "11.0"; 2013 = "12.0" }
$targetDir = "c:\Program Files (x86)\Microsoft Visual Studio $($VS_VERSION[$version])\VC"
if (!(Test-Path (Join-Path $targetDir "vcvarsall.bat"))) {
"Error: Visual Studio $version not installed"
return
}
pushd $targetDir
cmd /c "vcvarsall.bat&set" |
foreach {
if ($_ -match "(.*?)=(.*)") {
Set-Item -force -path "ENV:\$($matches[1])" -value "$($matches[2])"
}
}
popd
write-host "`nVisual Studio $version Command Prompt variables set." -ForegroundColor Yellow
}
[parameter(Mandatory=$true,
...
cmd /c """$targetDir\vcvarsall.bat""&set"
Keith hat PowerShell Community Extensions (PSCX) bereits mit folgendem Invoke-BatchFile
Befehl erwähnt:
Invoke-BatchFile "${env:ProgramFiles(x86)}\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
Mir ist auch aufgefallen, dass PSCX auch eine Import-VisualStudioVars
Funktion hat:
Import-VisualStudioVars -VisualStudioVersion 2013
Ein großes Lob an Andy S für seine Antwort. Ich benutze seine Lösung schon eine Weile, bin aber heute auf ein Problem gestoßen. Jeder Wert mit einem Gleichheitszeichen wird am Gleichheitszeichen abgeschnitten. Zum Beispiel hatte ich:
JAVA_TOOL_OPTIONS=-Duser.home=C:\Users\Me
Aber meine PS-Sitzung berichtete:
PS C:\> $env:JAVA_TOOL_OPTIONS
-Duser.home
Ich habe dies behoben, indem ich mein Profilskript wie folgt geändert habe:
pushd 'c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC'
cmd /c "vcvarsall.bat&set" |
foreach {
if ($_ -match "=") {
$i = $_.indexof("=")
$k = $_.substring(0, $i)
$v = $_.substring($i + 1)
set-item -force -path "ENV:\$k" -value "$v"
}
}
popd
Für jemanden, der im Jahr 2020 und Visual Studio Code 1.41.1 immer noch damit zu kämpfen hat, also ein bisschen abseits des Themas.
Ich habe alle verschiedenen Teile des Codes von oben und aus dem Internet verwendet, z. B. von https://help.appveyor.com/discussions/questions/18777-how-to-use-vcvars64bat-from-powershell, und mit einem schrittweisen Ansatz, den ich geschafft habe Lassen Sie das folgende Skript funktionieren.
In VSCode "settings.json" gespeichert und mit der Code Runner-Erweiterung installiert.
Mit Microsoft (R) C / C ++ - Optimierung der Compilerversion "cl.exe" aus 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\""}
Mit Microsoft (R) C / C ++ - Optimierung der Compilerversion "cl.exe" aus 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
Ich möchte die Befehle wie folgt an eine untergeordnete Shell übergeben:
cmd /c "`"${env:VS140COMNTOOLS}vsvars32.bat`" && <someCommand>"
Oder alternativ
cmd /c "`"${env:VS140COMNTOOLS}..\..\VC\vcvarsall.bat`" amd64 && <someCommand> && <someOtherCommand>"