2010-12-08 35 views

回答

12

的想法來源於此博客文章:Nothing solves everything – PowerShell and other technologies

這裏是我的版本,這個腳本。它調用批處理文件(或任何本地命令),並傳播它的環境:


UPDATE:改進和更好的測試,這個腳本的版本是在這裏:Invoke-Environment.ps1

<# 
.SYNOPSIS 
    Invokes a command and imports its environment variables. 

.DESCRIPTION 
    It invokes any cmd shell command (normally a configuration batch file) and 
    imports its environment variables to the calling process. Command output is 
    discarded completely. It fails if the command exit code is not 0. To ignore 
    the exit code use the 'call' command. 

.PARAMETER Command 
     Any cmd shell command, normally a configuration batch file. 

.EXAMPLE 
    # Invokes Config.bat in the current directory or the system path 
    Invoke-Environment Config.bat 

.EXAMPLE 
    # Visual Studio environment: works even if exit code is not 0 
    Invoke-Environment 'call "%VS100COMNTOOLS%\vsvars32.bat"' 

.EXAMPLE 
    # This command fails if vsvars32.bat exit code is not 0 
    Invoke-Environment '"%VS100COMNTOOLS%\vsvars32.bat"' 
#> 

param 
(
    [Parameter(Mandatory=$true)] [string] 
    $Command 
) 

cmd /c "$Command > nul 2>&1 && set" | .{process{ 
    if ($_ -match '^([^=]+)=(.*)') { 
     [System.Environment]::SetEnvironmentVariable($matches[1], $matches[2]) 
    } 
}} 

if ($LASTEXITCODE) { 
    throw "Command '$Command': exit code: $LASTEXITCODE" 
} 

附:這裏是一個類似的功能添加到PowerShell中的建議:Extend dot sourcing concept to cmd files

+1

您可能只想使用`$ Command> nul 2>&1 && set`,以防``Command`具有非零退出代碼但仍會改變環境。 – Joey 2010-12-09 23:02:56

+0

鏈接'改進和更好測試'版本不適合我,但複製粘貼從這個答案的腳本。 – 2014-08-21 11:00:03

0

只需輸入名稱即可從Powershell運行批處理腳本,但這不會對您有所幫助。批處理腳本中設置的環境變量只能從批處理和批處理運行的任何內容中看到。一旦控件返回到Powershell,環境變量就消失了。不過,您可以讓批處理腳本在末尾運行set,然後將其輸出解析到PSH環境變量中。

2

是否可以將批處理腳本轉換爲PowerShell腳本?如果運行bat文件,它將在不修改PowerShell的env變量的單獨會話中執行。

你可以用ENV變量非常順利工作:

PS> Get-ChildItem env: 

Name       Value 
----       ----- 
ALLUSERSPROFILE    C:\ProgramData 
APPDATA      C:\Users\xyz\AppData\Roaming 
CommonProgramFiles    C:\Program Files (x86)\Common Files 
CommonProgramFiles(x86)  C:\Program Files (x86)\Common Files 
CommonProgramW6432    C:\Program Files\Common Files 
COMPUTERNAME     xyz 
ComSpec      C:\Windows\system32\cmd.exe 
DXSDK_DIR      D:\prgs\dev\Microsoft DirectX SDK (August 2009)\ 
FP_NO_HOST_CHECK    NO 
HOMEDRIVE      Z: 
HOMEPATH      \ 
... 

PS> Get-Item env:path 
Name Value 
---- ----- 
Path c:\dev\CollabNet\SubversionClient;C:\Windows\system32;... 

甚至(要短得多,只返回字符串):

PS> $env:path 
c:\dev\CollabNet\Subversion Client;C:\Windows\system32;... 

你可以改變這樣的環境路徑:

PS> $env:path += ";c:\mydir" 

你甚至可以像這樣在機器級設置環境變量:

# fist arg = env variable name, second = value, third = level, available are 'Process', 'User', 'Machine' 
PS> [Environment]::SetEnvironmentVariable('test', 'value', 'machine') 
21

如果你搶PowerShell Community Extensions,在它裏面的調用,批處理文件命令運行批處理文件,但更重要的是,它保留批處理所做的任何環境變量修改文件例如:

>Invoke-BatchFile 'C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat' 
Setting environment for using Microsoft Visual Studio 2008 x86 tools. 
相關問題