【问题标题】:Dynamically switch from 64bit to 32bit powershell动态从 64 位切换到 32 位 powershell
【发布时间】:2017-10-25 04:51:31
【问题描述】:

我有一个需要在 32 位 powershell 中运行的脚本。我想在运行时确定它是在 32 位还是 64 位 powershell 中运行,如果是 64 位,则动态切换到 32 位。

基本上与this.完全相反

if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
write-warning "Y'arg Matey, we're off to 64-bit land....."
if ($myInvocation.Line) {
    &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
}else{
    &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
}
exit $lastexitcode
}

但由于某种原因,我无法弄清楚如何扭转这种情况。请帮忙。

【问题讨论】:

  • 您希望我们离开这个站点并去其他地方看看您在问什么,这不是这个站点的工作方式。在问题本身中发布您尝试过但对您不起作用的代码,然后提出与该代码相关的特定问题。
  • 您可能会发现this StackOverflow question 很有用。
  • @KenWhite 我不知道这违反了规则。很抱歉给您带来如此大的不便。
  • 除此之外,您这样做是为了解决什么问题?
  • 我正在使用需要 32 位环境的 Microsoft.Jet.OLEDB.4.0。我正在脚本开头执行检查以自动切换到适当的环境(如果还没有的话)。

标签: powershell


【解决方案1】:

64 位系统上的 32 位版本的 PowerShell 驻留在

$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe  

所以测试一下是否在64位系统上运行:

if ([Environment]::Is64BitProcess) {
  write-warning "Switching to 32-bit.PowerShell."
  if ($myInvocation.Line) {
    &"$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -Non -NoP -NoL $myInvocation.Line
  }else{
    &"$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -Non -NoP -NoL -file "$($myInvocation.InvocationName)" $args
  }
  exit $lastexitcode
}

【讨论】:

    【解决方案2】:
    if([IntPtr]::size -eq 8) {
        Write-Host "Switching to 32bit..."
        if ($myInvocation.Line) {
            &"$env:WINDIR\SysWOW64\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
        }else{
            &"$env:WINDIR\SysWOW64\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
        }
        exit $lastexitcode
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 2013-08-22
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      相关资源
      最近更新 更多