【问题标题】:Inno Setup run/execute code as another userInno Setup 以其他用户身份运行/执行代码
【发布时间】:2015-04-15 02:56:57
【问题描述】:

尽管可以使用ExecAsOriginalUser 来使用启动/登录用户的凭据运行某些东西,但是否有命令或方法可以以特定用户身份运行特定的一段代码?

为了进一步解释,我在安装结束时将计算机名称写入文件共享的日志中。只要不使用在服​​务器上没有匹配的本地管理员帐户的本地管理员帐户进行安装,就可以完美地工作。因此,我需要做的是让写入日志的特定代码位作为特定用户帐户执行,我知道该用户帐户存在于服务器上。

我使用的代码如下:

{ Auto audit the computer name }
procedure AutoAudit();
var
  strComputerName: TArrayOfString;
  strAutoAuditFile: String;
begin
  strAutoAuditFile := '\\' + ClientConnectionPage.Values[0] + '\Audit\Client Audit.txt';
  SetArrayLength(strComputerName, 1);
  strComputerName[0] :=
    ExpandConstant('{computername} ') + GetDateTimeString('dd/mm/yyyy hh:mm', '/', ':');
  SaveStringsToFile(strAutoAuditFile, strComputerName, True);
end;

所以,我需要一种以其他用户身份执行SaveStringsToFile 函数的方法。

我想到了一种相当麻烦的方法来做到这一点,这将涉及测试可用的驱动器号、使用帐户凭据映射驱动器、写入文件然后断开驱动器的连接。但是,我希望有一种更优雅的方式来做到这一点?

【问题讨论】:

    标签: inno-setup elevated-privileges


    【解决方案1】:

    您必须使用LogonUser WinAPI function 以其他帐户运行一段代码。

    很可能从 Inno Setup 代码中运行它,但我没有找到示例。


    或者,您可以开发一个单独的微型应用程序来模拟其他用户并将该应用程序嵌入到您的安装程序中。

    在 C#/VB.NET 中,您可以使用 Impersonator class(内部使用 LogonUser):

    using System;
    using System.IO;
    
    class Program
    {
        static void Main(string[] args)
        {
            using (new Impersonator("username", "domain", "password"))
            {
                string strAutoAuditFile = @"\\" + args[0] + @"\Audit\Client Audit.txt";
                string strComputerName =
                    Environment.MachineName + " " + DateTime.Now.ToString("dd/mm/yyyy hh:mm");
                File.WriteAllText(strAutoAuditFile, strComputerName);
            }
        }
    }
    

    从 Inno Setup 运行应用程序,例如:

    [Files]
    Source: "getcompname.exe"; Flags: dontcopy
    
    [Code]
    ...
    
    var
      ResultCode: Integer;
    begin
      ...
      ExtractTemporaryFile('getcompname.exe');
      ExecAsOriginalUser(
        ExpandConstant('{tmp}') + '\getcompname.exe ',
        ClientConnectionPage.Values[0],
        '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
      ...
    end;
    

    如果您没有控制台应用程序开发经验,请注意“小”应用程序甚至可以是使用runas command(不允许自动指定密码)或PsExec(它确实)。

    【讨论】:

    • 您建议的LogonUser 函数可能满足我的需要,但我的编程知识不足以确定我将如何在 Inno Setup 中实现此功能。
    猜你喜欢
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2018-06-06
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多