C# Powershell snapin未能使用installutil注册

13

我有一个非常简单的PowerShell脚本(如下所示)。 在我的个人资料中,我使用以下内容对installutil进行了别名:

set-alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutil

在PowerShell中,我只需要:

installutil assemplylocation.dll

这个操作已经成功执行(安装/提交都已成功完成)。 然而,当我检查注册表或在PowerShell中使用get-pssnapin -registered命令时,它并没有显示我的程序集。前几天我做了同样的操作,一切正常,但现在我似乎无法重复这个过程...请给予建议。

using System;
using System.Management.Automation;
using System.ComponentModel;

namespace PSBook_2_1
{
    [RunInstaller(true)]
    public class PSBookChapter2MySnapIn : PSSnapIn
    {
        public PSBookChapter2MySnapIn()
            : base()
        { }

    // Name for the PowerShell snap-in.
    public override string Name
    {
        get
        {
            return "Wiley.PSProfessional.Chapter2";
        }
    }

    // Vendor information for the PowerShell snap-in.
    public override string Vendor
    {
        get
        {
            return "Wiley";
        }
    }

    // Description of the PowerShell snap-in
    public override string Description
    {
        get
        {
            return "This is a sample PowerShell snap-in";
        }
    }
}

// Code to implement cmdlet Write-Hi
[Cmdlet(VerbsCommunications.Write, "Hi")]
public class SayHi : Cmdlet
{
    protected override void ProcessRecord()
    {
        WriteObject("Hi, World!");
    }
}

// Code to implement cmdlet Write-Hello
[Cmdlet(VerbsCommunications.Write, "Hello")]
public class SayHello : Cmdlet
{
    protected override void ProcessRecord()
    {
        WriteObject("Hello, World!");
    }
}

}


问题出在在32位版本的PowerShell中运行,而不是64位版本... - downatone
7个回答

14

downatone的答案让我找到了正确的方向,但我的问题正好相反。我的项目设置为任何CPU,并且我在Win7 x64上运行,因此从我的代码启动的PowerShell,然后使用snapin安装dll是64位的。然而,我使用的安装命令指向32位的.Net运行时,即:

C:\Windows\Microsoft.net\Framework\V4.0.30319\installutil myDLL.dll

本应该是这样的

C:\Windows\Microsoft.net\Framework64\V4.0.30319\installutil myDLL.dll

注意框架路径中的64。


11

结果发现问题是我有一个32位的cmdlet - 但只检查了64位版本的powershell ...


1
我也遇到了这个问题。Snapin列表存储在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns中,但如果您使用32位进程访问它,则注册表会被操作系统虚拟化。您可以使用Sysinternals Process Monitor查看注册表调用的详细信息。 - Martin Hollingsworth
1
是的,命令应该是:set-alias installutil $env:windir\Microsoft.NET\Framework64\v2.0.50727\installutil。 - Mandrake

1
对我来说,这里的关键点是要记住Visual Studio 2010仍然是一个32位应用程序,这意味着当我使用命令提示符时,它默认使用32位的InstallUtil变体。在这种情况下,很难立即看出注册表键因此被写入Wow64位节点而不是64位注册表。

1

以管理员身份运行以运行ps


1
你以管理员身份运行了installutil吗?它会将信息写入受保护的注册表部分。如果你在Vista上以非管理员身份运行它,可能会产生奇怪的结果。

0

0

遇到了同样的问题 - 我试图使用命令

C:\Windows\Microsoft.net\Framework\V4.0.30319\installutil myDLL.dll 

而不是

C:\Windows\Microsoft.net\Framework64\V4.0.30319\installutil myDLL.dll 

在使用64位命令行工具(项目配置为Any CPU)在win2k8 x64操作系统上时...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接