gpt4 book ai didi

powershell - 如何从32位Powershell实例访问64位注册表?

转载 作者:行者123 更新时间:2023-12-03 14:34:11 25 4
gpt4 key购买 nike

如果启动Powershell的32位实例(%SystemRoot%\ syswow64 \ WindowsPowerShell \ v1.0 \ powershell.exe),则注册表提供程序仅会看到注册表中有限的32位部分。

**32-bit console**
PS> (dir HKLM:\SOFTWARE | measure).count - (dir HKLM:\SOFTWARE\wow6432node | measure).count

0

**64-bit console**
PS> (dir HKLM:\SOFTWARE | measure).count - (dir HKLM:\SOFTWARE\wow6432node | measure).count

-5

有什么方法可以强制提供程序进入64位模式?我可以使用[Microsoft.Win32] .Net API,也可以使用WMI,但我不想这样做。我正在使用Powershell v2 CTP3,如果这完全扩展了可能性。

最佳答案

当Powershell以32位进程运行时,我不知道将其“切换”到64位模式的机制。 64位系统中虚拟化支持的全部目的是使32位进程相信它们生活在32位OS中...

但是,这表示我过去使用以下技术,并且对我来说非常有效(以下代码已在带有Powershell v1的Vista SP1 x64上进行了测试)。该技术依赖于这样一个事实:即使从32位进程中调用,.NET的“任何CPU”可执行文件也将作为64位进程运行。我们将执行的步骤:

  • 编译一个简短的C#程序,该程序将启动powershell(即一个非常简单的“fork”实现:-))
  • 运行已编译的C#程序
  • 编译的C#程序将启动Powershell,但是由于它是“Any CPU”,它将作为64bit进程运行,因此它将启动64bit Powershell(请注意,因为这只是概念验证,所以我希望Powershell可以在您的“路径”中)
  • 新的64位Powershell将运行我们选择的Commandlet

  • 这是上述操作的屏幕截图(注意进程的位):
    Process tree http://img3.imageshack.us/img3/3248/powershellfork.png

    以下程序希望列出的所有文件都位于同一目录中。我建议创建一个测试目录,例如C:\ Temp \ PowershellTest,并将所有文件存储在那里)。

    程序的入口点将是一个简单的命令行开关:
    # file "test.ps1"
    $basePath = Split-Path -resolve $myInvocation.MyCommand.Path
    $exe = Join-Path $basePath test.exe
    &"$env:SystemRoot\Microsoft.NET\Framework\v3.5\csc.exe" /nologo /target:exe /out:$exe (Join-Path $basePath test.cs)
    &$exe (Join-Path $basePath visibility.ps1)

    它运行csc(32位,但是没关系:-)),然后运行csc编译器的结果,并传递一个参数(能看到的完整路径)。ps1(这是我们要在64位Powershell中运行的Commandlet)。

    C#代码也非常简单:
    // file "test.cs"
    using System.Diagnostics;
    static class Program {
    static int Main(string[] args) {
    ProcessStartInfo i = new ProcessStartInfo("powershell", args[0]);
    i.UseShellExecute = false;
    using(Process p = Process.Start(i)) {
    p.WaitForExit();
    return p.ExitCode;
    }
    }
    }

    最后,您的“可见性”脚本:
    # file "visibility.ps1"
    (dir HKLM:\SOFTWARE).count - (dir HKLM:\SOFTWARE\wow6432node).count

    现在从32位Powershell运行输入脚本会产生预期的结果(只是为了表明我没有作弊,我首先直接运行可见性脚本,然后使用我们的fork技术):

    Program run http://img3.imageshack.us/img3/2766/powershellrunc.png

    关于powershell - 如何从32位Powershell实例访问64位注册表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/630382/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com