PowerShell의 X64 vs. X86 변동에 대해 프로그램하는 가장 좋은 방법은 무엇입니까?

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

  •  03-07-2019
  •  | 
  •  

문제

유지 관리 시스템을 뒷받침하는 종속성을 설치하고 구성하는 데 사용하는 몇 가지 스크립트가 있습니다. 우리는 Dev, Test, Demo, Train, Prod 등을 설정할 때마다 이것을 실행합니다. 우리는 종종 X64 vs. X86 아키텍처, 특히 PowerShell 스크립트가 관련된 경우를 다루어야한다는 것을 알게됩니다.

예를 들어, 나는 Windows Installer PowerShell Extensions 프로그램/패치가 설치되었는지 확인합니다. 스크립트는 기본적으로 경로가 아닌 PowerShell (x86)을 명시 적으로 호출하지 않고 X64 환경에서 작동하지 않습니다. 이 스크립트를 X64 플랫폼에 포트하면 두 아키텍처에서 PowerShell에서 작동하는 단일 스크립트 세트를 유지하고 필요할 때 X86 코드 만 호출하는 것이 좋습니다.

누구 든지이 작업을 수행하는 전략을 알고 있습니까?

도움이 되었습니까?

해결책 2

그만큼 msgoodies 블로그 가지다 아키텍처를 결정하기위한이 제안. 이 접근법을 사용하여 아키텍처를 결정하고 알려진 비 호환성이있을 때 X86 PowerShell을 호출 할 수 있습니다.

다른 팁

구성 스크립트 로이 문제를 많이 겪습니다. 내가 취하는 기본 접근법은

  1. 64 비트 환경에 있는지 테스트하기 위해 여러 기능을 사용하십시오 (http://blogs.msdn.com/jaredpar/archive/2008/10/16/powershell-and-64-bit-windows-functions.aspx)
  2. 특정 스크립트의 요구에 따라 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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top