对PowerShell的x64与x86可变性进行编程的最佳方法是什么?

StackOverflow https://stackoverflow.com/questions/602060

  •  03-07-2019
  •  | 
  •  

我们有几个脚本用于安装和配置支持我们维护的系统的依赖项。我们在建立开发,测试,演示,培训,产品等环境时随时运行这些。我们经常发现我们必须处理x64与x86架构,特别是涉及powershell脚本的地方。

例如,我有一个脚本使用 Windows Installer PowerShell Extensions 来确定是否有程序/补丁已安装。如果没有显式调用PowerShell(x86),该脚本在x64环境中不起作用,默认情况下,该脚本不在路径中。当我们将这些脚本移植到x64平台时,维护一组脚本在两个体系结构的powershell中工作并且仅在需要时调用x86代码将是很棒的。

有没有人知道这样做的策略?

有帮助吗?

解决方案 2

msgoodies blog 这个确定架构的建议。当存在已知的不兼容性时,可以使用此方法来确定体系结构并调用x86 powershell。

其他提示

我的配置脚本经常遇到这个问题。我采取的基本方法是

  1. 使用多个函数来测试我是否处于64位环境中( http://blogs.msdn.com/jaredpar/archive/2008/10/16/powershell-and-64-bit-windows-helper-functions.aspx
  2. 根据特定脚本的需要调用x86 / x64 PowerShell
  3. 不幸的是,很多事情是以蛮力的方式完成的。 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
    }
    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top