我有一個設置一堆環境變量的大批量腳本。 我想從powershell中調用該批處理腳本,這樣我就可以同時獲得由我的腳本和PowerShell設置的環境變量的好處。如何從Powershell調用批處理腳本?
22
A
回答
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
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.
相關問題
- 1. 從批處理腳本調用PowerShell腳本
- 2. 從另一個批處理腳本調用批處理腳本
- 3. 如何在批處理腳本中運行powershell腳本和批處理腳本?
- 4. 如何從批處理腳本中運行批處理腳本?
- 5. 運行批處理腳本,PowerShell腳本
- 6. 如果第一次調用批處理腳本給出任何錯誤,如何停止從主批處理腳本中調用另一批處理腳本。
- 7. 如何從批處理文件運行PowerShell腳本
- 8. 從批處理腳本傳遞參數到PowerShell腳本失敗
- 9. 從windows批處理腳本傳遞參數到powershell腳本
- 10. 批處理文件v PowerShell腳本
- 11. 通PowerShell的變量批處理腳本
- 12. VBScript,批處理或PowerShell腳本?
- 13. powershell命令invoke-sqlcmd批處理腳本
- 14. 批處理文件執行Powershell腳本
- 15. PowerShell調用後批處理腳本不會返回
- 16. 從批處理腳本
- 17. 從sh或bash腳本運行/調用Windows批處理腳本
- 18. 如何從另一個批處理腳本調用批處理腳本而不暫停?
- 19. 使用批處理腳本中的參數運行PowerShell腳本
- 20. 從powershell中調用批處理文件(其中批處理文件調用.exe)
- 21. 退出調用另一個批處理腳本的批處理腳本
- 22. 批處理腳本中調用批處理腳本 - 控制可能會錯過
- 23. 從批處理文件使用C#運行PowerShell腳本#
- 24. 在批處理腳本中調用宏
- 25. 批處理文件有錯誤,從Powershell腳本調用時不允許批處理文件完成
- 26. 如何使用批處理腳本
- 27. 如何使用批處理腳本
- 28. PowerShell:如何調用運行python腳本的批處理腳本並等待python腳本返回?
- 29. 如何從批處理文件中調用Powershell文件
- 30. 批處理腳本可以知道它是否從PowerShell中調用?
您可能只想使用`$ Command> nul 2>&1 && set`,以防``Command`具有非零退出代碼但仍會改變環境。 – Joey 2010-12-09 23:02:56
鏈接'改進和更好測試'版本不適合我,但複製粘貼從這個答案的腳本。 – 2014-08-21 11:00:03