TFS Build using PowerShell x86 to run

岁酱吖の 提交于 2019-12-13 01:29:21

问题


I currently have a build definition setup where I call a PowerShell script to do some "extra stuff" such as using a custom version number and DLL signing. A problem I'm having is that in my PowerShell script, I am trying to load an assembly so that I can create an object of a certain type and that I get an error when I try loading the assembly. I found out that the assembly that I need to load requires the script to run as a x86 process.

I found this out when I ran my PowerShell script as a Windows Powershell x86 instead of the regular Windows PowerShell process. Is there a way in my build definition where I can state which process I can run as? Such as the build process template or even in the script itself?


回答1:


I did it once in the past, so see for yourself if it is still working.

# 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:


Why don't you launch the x86 version of PowerShell from MSBuild?

<Exec Command="$(WinDir)\SysWOW64\WindowsPowerShell\v1.0\powershell.exe myscript.ps1"/>

If you're using the workflow variant of TeamBuild, just fire off PowerShell.exe from the SysWOW64 path.



来源:https://stackoverflow.com/questions/24328813/tfs-build-using-powershell-x86-to-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!