自定义操作以管理员权限运行并设置会话变量

Custom action runs with admin privilege and set session variables

我想运行自定义操作并搜索注册表,然后设置一些会话变量

听说是我的自定义动作函数

[CustomAction]
public static ActionResult RegistryDetails(Session session)
{
    try
    {
        CurrentSession = session;

        string registryPath = @"SOFTWARE\XXX\Printers";
        int printerIndex = 1;
        RegistryKey prnKey = Registry.LocalMachine;

        prnKey = prnKey.OpenSubKey(registryPath);

        List<string> subKeyList = new List<string>();
        subKeyList.AddRange(prnKey.GetSubKeyNames());

        while(subKeyList.Contains(printerIndex.ToString()))
        {
            printerIndex++;
        }

        string newRegistryPath = registryPath + "\" + printerIndex.ToString();
        session["UtillRegKey"] = newRegistryPath;
        session["PrinterNo"] = printerIndex.ToString();
    }
    catch (Exception ex)
    {

        CurrentSession.Log(ex.Message);
        Record exceptionRec = new Record(0);
        exceptionRec[0] = "Errors -" + ex.StackTrace.ToString();
        return ActionResult.Failure;
    }

    return ActionResult.Success;
}

但是对于 运行 这个自定义操作,我需要管理员权限 所以我设置了 Execute="deferred" & Impersonate="no" 我的自定义操作定义。 这使得 session["PrinterNo"]&session["UtillRegKey"] 无法访问。因为访问会话变量应该是 Execute="immediate"。 但我无法设置为 Execute="immediate",这将阻止 运行 自定义操作 运行 作为管理员。

任何人都可以帮助我克服这个问题。

您不能在安装的延迟(提升)阶段修改会话属性。您可以在延迟的自定义操作期间读取会话属性,它需要由另一个自定义操作设置的特殊 属性 并使用 session.CustomActionData

您似乎只是在从注册表中读取数据,因此立即执行此操作应该没有问题。