【问题标题】:TFS Build using PowerShell x86 to runTFS Build 使用 PowerShell x86 运行
【发布时间】:2014-06-20 13:47:29
【问题描述】:
我目前有一个构建定义设置,我在其中调用 PowerShell 脚本来执行一些“额外的事情”,例如使用自定义版本号和 DLL 签名。我遇到的一个问题是,在我的 PowerShell 脚本中,我正在尝试加载程序集,以便我可以创建某种类型的对象,并且在尝试加载程序集时出现错误。我发现我需要加载的程序集需要脚本作为 x86 进程运行。
当我将 PowerShell 脚本作为 Windows Powershell x86 而不是常规的 Windows PowerShell 进程运行时,我发现了这一点。在我的构建定义中有没有一种方法可以说明我可以运行哪个进程?比如构建过程模板甚至脚本本身?
【问题讨论】:
标签:
powershell
tfs
tfsbuild
【解决方案1】:
我过去做过一次,所以你自己看看它是否还在工作。
# Get the path where powershell resides. If the caller passes -use32 then
# make sure we are returning back a 32 bit version of powershell regardless
# of the current machine architecture
function Get-PowerShellPath() {
param ( [switch]$use32=$false,
[string]$version="1.0" )
if ( $use32 -and (test-win64machine) ) {
return (join-path $env:windir "syswow64\WindowsPowerShell\v$version\powershell.exe")
}
return (join-path $env:windir "System32\WindowsPowerShell\v$version\powershell.exe")
}
# Is this a Win64 machine regardless of whether or not we are currently
# running in a 64 bit mode
function Test-Win64Machine() {
return test-path (join-path $env:WinDir "SysWow64")
}
# Is this a Wow64 powershell host
function Test-Wow64() {
return (Test-Win32) -and (test-path env:\PROCESSOR_ARCHITEW6432)
}
# Is this a 64 bit process
function Test-Win64() {
return [IntPtr]::size -eq 8
}
# Is this a 32 bit process
function Test-Win32() {
return [IntPtr]::size -eq 4
}
function Get-ProgramFiles32() {
if (Test-Win64 ) {
return ${env:ProgramFiles(x86)}
}
return $env:ProgramFiles
}
function Exec-Script32
{
param(
[string] $scriptPath
)
$scriptName = Split-Path -Leaf $scriptPath
$innerLogFilename = Join-Path $env:TEMP $scriptName
$innerLogFilename += ".log"
$dataFilename = Join-Path $env:TEMP $scriptName
$dataFilename += ".data"
Export-Clixml -Path $dataFilename -InputObject $Args
$ps32 = Get-PowershellPath -use32
Write-Verbose "### Re-entering '$scriptPath' in 32-bit shell"
Write-Verbose "### Logging to '$innerLogFilename'"
# call this exact file
& $ps32 -File $scriptPath $dataFilename 2>&1 > $innerLogFilename
$succeeded = $?
Write-Output (Get-Content $innerLogFilename)
Remove-Item $innerLogFilename
if (!$succeeded) {
#forward
throw "$scriptPath failed"
}
}
【解决方案2】:
为什么不从 MSBuild 启动 x86 版本的 PowerShell?
<Exec Command="$(WinDir)\SysWOW64\WindowsPowerShell\v1.0\powershell.exe myscript.ps1"/>
如果您使用的是 TeamBuild 的工作流变体,只需从 SysWOW64 路径启动 PowerShell.exe。