【问题标题】:Can I execute a particular line of a script in a 64 bit shell and return the output to the ongoing 32 bit shell session?我可以在 64 位 shell 中执行脚本的特定行并将输出返回到正在进行的 32 位 shell 会话吗?
【发布时间】:2020-04-28 14:14:00
【问题描述】:

我正在编写一个 Powershell 脚本,它将集成到为 32 位 Windows 机器设计的产品中。因此,在调用时,即使在 64 位机器上,它也会默认在 x86 Powershell 上运行。 其中一条线需要在 64 位 shell 上运行才能获得准确的结果。尝试这样做:

#Above this is the ongoing script in 32bit session
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
    #write-warning "Excecuting the script under 64 bit powershell"
    if ($myInvocation.Line) {
        [System.IntPtr]::Size
        &"$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile 
        #Statement block
    }else{
        &"$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile  $args
        #Statement block
    }

}
#Below this is the remaining Script in 32 bit session

我无法将控制流传递给新会话并将其取回。它所做的只是在已经运行的 32 位 shell 中启动 64 位 shell。有什么方法可以做到这一点并返回新外壳打印的值?

【问题讨论】:

  • 那么,在 32 位目标机器上会发生什么?他们都输出不准确的结果?
  • 这是凯斯吗?您可以只以 64 位运行脚本或命令,还是以 64 位运行整个程序?
  • @MathiasR.Jessen 我要获取的数据是系统上所有已安装的应用程序。这可以从注册表路径Get-Childitem "HKLM://SOFTWARE//Microsoft//Windows//CurrentVersion//Uninstall//*" 中获取,但是在 64 位机器上,如果上面是在 32 位 shell 上运行的,由于某种原因它不包含所有已安装的应用程序。
  • @vercetti 啊,明白了。也许query the 64-bit registry view explicitly 在 64 位上?
  • @js2010 脚本的另一部分需要在 32 位 shell 上运行。 这是 Kace 吗?您是在谈论特定的脚本吗?

标签: windows powershell


【解决方案1】:

我可以使用-Command 参数来做到这一点。根据您的用例调用相应的 shell(32 或 64)。直接在括号内传递命令或完整的脚本块。

#Invoking the 64-bit Powershell shell 
& "$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -Command 
{ 
# Statement Block  
}

如果需要,输出可以存储在变量中。 一旦控件返回,父 shell 就会恢复。

查看其他示例以在官方文档中使用 -Command 参数 here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 2020-06-22
    • 1970-01-01
    • 2012-06-17
    • 2011-02-03
    • 2011-11-26
    • 1970-01-01
    相关资源
    最近更新 更多