我们有几个脚本用于安装和配置支持我们维护的系统的依赖项.我们在建立开发,测试,演示,培训,产品等环境时随时运行这些.我们经常发现我们必须处理x64与x86架构,特别是涉及powershell脚本的地方.
例如,我有一个脚本,它使用Windows Installer PowerShell Extensions来确定是否已安装程序/补丁.如果没有显式调用PowerShell(x86),该脚本在x64环境中不起作用,默认情况下,该脚本不在路径中.当我们将这些脚本移植到x64平台时,维护一组在两个体系结构上都在PowerShell中工作的脚本并且只在需要时调用x86代码会很棒.
有谁知道这样做的策略?
我的配置脚本遇到了很多问题.我采取的基本方法是
使用几个函数来测试我是否在64位环境中(http://blogs.msdn.com/jaredpar/archive/2008/10/16/powershell-and-64-bit-windows-helper-functions.aspx)
根据特定脚本的需要调用x86/x64 PowerShell
不幸的是,很多事情是以蛮力的方式完成的.x86/x64依赖的每个特定配置条目实际上具有2个代码路径(每个体系结构一个).
我能够做的唯一真正的例外是测试磁盘上某些程序的存在.我有一个方便的功能(Get-ProgramFiles32),可以很容易地测试程序.
if ( test-path (join-path Get-ProgramFiles32 "subversion") ) { ...
以下是我在公共库中处理32/64位差异的所有辅助函数.
# 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 Invoke-Admin() { param ( [string]$program = $(throw "Please specify a program" ), [string]$argumentString = "", [switch]$waitForExit ) $psi = new-object "Diagnostics.ProcessStartInfo" $psi.FileName = $program $psi.Arguments = $argumentString $psi.Verb = "runas" $proc = [Diagnostics.Process]::Start($psi) if ( $waitForExit ) { $proc.WaitForExit(); } } # Run the specified script as an administrator function Invoke-ScriptAdmin() { param ( [string]$scriptPath = $(throw "Please specify a script"), [switch]$waitForExit, [switch]$use32=$false ) $argString = "" for ( $i = 0; $i -lt $args.Length; $i++ ) { $argString += $args[$i] if ( ($i + 1) -lt $args.Length ) { $argString += " " } } $p = "-Command & " $p += resolve-path($scriptPath) $p += " $argString" $psPath = Get-PowershellPath -use32:$use32 write-debug ("Running: $psPath $p") Invoke-Admin $psPath $p -waitForExit:$waitForExit }