Visual Studio 명령 프롬프트에서 PowerShell을 사용하려면 어떻게해야합니까?
나는 잠시 동안 베타 2를 사용해 왔으며 VS2010 명령 프롬프트를 실행할 때 cmd.exe를 펀칭해야한다는 견해를 불러 일으켰습니다. 예전에는 Visual Studio 2008 용 vsvars2008.ps1 스크립트를 사용했습니다. vsvars2010.ps1 또는 이와 유사한 제품이 있습니까?
여기에서 자유롭게 도둑질 : http://allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html , 나는 이것을 작동시킬 수있었습니다. 나는 내 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가 더 이상 존재하지 않습니다. 대신 Common7 \ Tools 폴더에있는 vsvars32.bat 파일을 사용할 수 있습니다.
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
가장 간단한 옵션은 VS 2010 명령 프롬프트를 실행 한 다음 PowerShell.exe를 시작하는 것입니다. "홈"PowerShell 프롬프트에서 실제로이 작업을 수행하려는 경우 표시되는 접근 방법이 있습니다. 나는 Lee Holmes가 얼마 전에 쓴 스크립트를 사용합니다.
<#
.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
}
참고 :이 기능은 곧 PowerShell Community Extensions 2.0 모듈 기반 릴리스에서 사용할 수 있습니다 .
나는 간단한 방법을 발견 여기에 : 바로 가기를 수정합니다.
원래 바로 가기는 다음과 같습니다.
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat""
& powershell
마지막 인용문 앞에 다음과 같이 추가하십시오 .
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" & powershell"
If you want to make it look more like PS, go to the Colors tab of the shortcut properties and set the Red, Green and Blue values to 1, 36 and 86 respectively.
An old question but worth another answer to (a) provide VS2013 suppport; (b) combine the best of two previous answers; and (c) provide a function wrapper.
This builds on @Andy's technique (which builds on Allen Mack's technique as Andy indicated (which in turn builds on Robert Anderson's technique as Allen indicated (all of which had a slight glitch as indicated on this page by the user known only as "me--", so I took that into account as well))).
Here is my final code--note the use of the non-greedy quantifier in the regex to handle any possible embedded equals in the values. That also happens to simplify the code: a single match instead of a match then split as in Andy's example or a match then indexof and substrings as in "me--"'s example).
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
}
Keith has already mentioned PowerShell Community Extensions (PSCX), with its Invoke-BatchFile
command:
Invoke-BatchFile "${env:ProgramFiles(x86)}\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
I also noticed that PSCX also has an Import-VisualStudioVars
function:
Import-VisualStudioVars -VisualStudioVersion 2013
Kudos to Andy S for his answer. I've been using his solution for a while, but ran into a problem today. Any value that has an equals sign in it is truncated at the equals sign. For example, I had:
JAVA_TOOL_OPTIONS=-Duser.home=C:\Users\Me
But my PS session reported:
PS C:\> $env:JAVA_TOOL_OPTIONS
-Duser.home
I fixed this by modifying my profile script to the following:
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
I like to pass the commands into a child shell like so:
cmd /c "`"${env:VS140COMNTOOLS}vsvars32.bat`" && <someCommand>"
Or alternatively
cmd /c "`"${env:VS140COMNTOOLS}..\..\VC\vcvarsall.bat`" amd64 && <someCommand> && <someOtherCommand>"
'programing tip' 카테고리의 다른 글
의사 요소 내용 앞에 :: after 또는 ::에 줄 바꿈 추가 (0) | 2020.07.19 |
---|---|
Vim Regex 캡처 그룹 [bau-> byau : ceu-> cyeu] (0) | 2020.07.19 |
Rails 4 : 자산이 생산에 적재되지 않음 (0) | 2020.07.19 |
더 이상 사용되지 않는, 더 이상 사용되지 않는 및 더 이상 사용되지 않는 (0) | 2020.07.19 |
객체 속성으로 배열에서 객체 제거 (0) | 2020.07.19 |