安装程序项目 - 以提升的权限启动自定义操作

Installer Projects - Launching a custom action with elevated privileges

我有一个 Visual Studio 2017 c# 应用程序,其中附加了一个安装程序项目安装程序。我们使用 "custom action" 启动一个可执行文件,在 MSI 完成时 运行s,自定义操作位于自定义操作选项卡中的 "commit" 下。

当应用程序 运行s 时,其 windows 用户主体是 NT AUTHORITY\SYSTEM。

当我自己运行应用时,它的用户是我,MYDOMAIN\MYUSER

所以我试图让它提升这些权限,到目前为止,我没有主要搜索旧的 Whosebug 问题,而是找到了三个可能的解决方案,但 none 其中对我有用,在所有情况下 exe 是在 NT AUTHORITY\SYSTEM

下仍然 运行ning
  1. 添加包含
  2. 的应用程序清单
<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
  1. 在记事本中编辑 vdproj 文件以制作 RequiresElevation "true"
    "MsiBootstrapper"
    {
        "LangId" = "3:1033"
        "RequiresElevation" = "11:TRUE"
    }
  1. 尝试将 AdminUser 添加为启动条件,根据:How do I avoid UAC when my EXE file name contains the word "update"?

以上都试过,也分别试过,但始终是SYSTEM用户。

关于如何获得 运行 的自定义操作作为登录用户的权限而不是 SYSTEM 的任何想法?谢谢

EASY 答案被证明是该线程中被接受的答案:

[引用]

简短的回答是,您不能在 InstallAllUsers 设置的 Visual Studio 设置中执行此操作,因为所有 VS 安装程序都会生成自定义操作 运行 作为系统帐户。因此,您需要使用 Orca 等编辑工具更改 MSI 文件中的自定义操作设置。您会在 MSI 文件的 CustomAction table 中找到自定义操作,查看 Type 值(可能是类型 3074),然后关闭 msidbCustomActionTypeNoImpersonate 位,以便它 运行s 模拟为安装用户。

https://msdn.microsoft.com/en-us/library/windows/desktop/aa368069(v=vs.85).aspx

请注意,模拟为安装用户的 运行ning 有其自身的一系列问题,因为它与作为交互式用户的 运行ning 不同。未加载用户配置文件,因此与用户关联的对象(例如 HKCU、用户配置文件文件夹)非常不可靠。

许多人在应用程序首次启动时使用单独的程序填充数据库,以便它 运行 可以作为交互式用户正常使用,并且可以作为独立程序进行开发和调试。如果在安装过程中填充失败,您要么放弃安装并回滚,要么继续安装并最终得到一个空数据库,为此您可能需要一个程序来填充它。 [/quote]

不是那么简单的答案但优秀的解决方案如下:

编辑 MSI 文件以从自定义操作记录中删除类型 msidbCustomActionTypeNoImpersonate。

我在 VB.NET 程序中以编程方式执行此操作,如下所示:

    Dim o As WindowsInstaller.Installer = CType(CreateObject("WindowsInstaller.Installer"), WindowsInstaller.Installer)

    Dim db As WindowsInstaller.Database
    db = o.OpenDatabase(fil.FullName, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeDirect)

    Dim record As WindowsInstaller.Record = Nothing
    view = db.OpenView("select File.File, FileName From File")
    view.Execute(record)
    record = view.Fetch
    Dim bFound As Boolean = False
    While record IsNot Nothing
        Dim sFileName As String = record.StringData(2)
        If sFileName.EndsWith("MYCUSTOMACTIONEXE.exe", StringComparison.CurrentCultureIgnoreCase) = True Then
            bFound = True
            Exit While
        End If
        record = view.Fetch
    End While

    If bFound = True Then
        Dim sGUID As String = record.StringData(1)

        '   At time of writing this was changing a 3602 into a 1554, so removing msidbCustomActionTypeNoImpersonate 
        '   The record key was _65BF5279_D2EA_42C1_AC66_90A684817EE5 which is the custom action for MYCUSTOMACTIONEXE.exe


        view = db.OpenView("select Action, Type From CustomAction Where Source = '" & sGUID & "'")
        view.Execute(record)
        record = view.Fetch
        If record IsNot Nothing Then
            Dim sActionGUID As String = record.StringData(1)
            Dim sType As String = record.StringData(2)
            If sActionGUID IsNot Nothing AndAlso sActionGUID <> "" Then
                '   Take off Hex 800 which means noimpersonation
                Dim lType As Integer = CInt(sType)
                If lType And 2048 = 2048 Then
                    Dim sNewType As String = CStr(lType - 2048)
                    Dim v As WindowsInstaller.View = db.OpenView(
                        "update CustomAction set Type=" & sNewType & " Where CustomAction.Action = '" & sActionGUID & "'")
                    v.Execute()
                End If
            End If

        End If
    End If