Inno Setup以另一个用户身份运行/执行代码 [英] Inno Setup run/execute code as another user
问题描述
尽可能使用ExecAsOriginalUser
通过用户的登录/登录凭据来运行某些内容,是否有命令或方法以特定用户身份运行特定代码?
Much as it is possible to use ExecAsOriginalUser
to run something using the starting/logged in user's credentials, is there a command or way to run a particular piece of code as a specific user?
为进一步说明,我在安装结束时将计算机名称写到文件共享的日志中.只要不使用服务器上没有匹配的本地Administrator帐户的本地Administrator帐户进行安装,此方法就可以完美运行.因此,我需要做的是将写入日志的特定代码作为特定的用户帐户执行,该帐户具有我知道的凭据,并且我知道该凭据在服务器上存在.
To explain further, I am writing the computer name into a log on a file share at the end of installation. This works perfectly as long as not installing using a local Administrator account which does not have a matching local Administrator account on the server. Therefore, what I need to do is have the particular bit of code that writes to the log execute as a specific user account, which I have the credentials for, that I know exists on the server.
我正在使用的代码如下:
The code I am using is as follows:
{ 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
函数的方法.
So, what I need is a way of executing the SaveStringsToFile
function as another user.
我想到了一种相当混乱的方法,其中包括测试可用的驱动器号,使用帐户凭据映射驱动器,写入文件,然后断开驱动器连接.但是,我希望有一种更优雅的方法来做到这一点?
I have thought of a rather messy way to do this, which would involve testing for an available drive letter, mapping a drive using the account credentials, writing to the file and then disconnecting the drive. However, I am hoping there is a more elegant way to do this?
推荐答案
You have to use the LogonUser
WinAPI function to run a piece of code as a different account.
可能可以通过Inno Setup代码运行此代码,但是我还没有找到示例.
It's likely possible to run this from an Inno Setup code, but I haven't found an example.
或者,您可以开发一个单独的微型应用程序来模拟其他用户,并将该应用程序嵌入到安装程序中.
Alternatively, you can develop a separate tiny application that impersonates the other user and embed the application into your installer.
在C#/VB.NET中,您可以使用 Impersonator
类(内部使用LogonUser
):
In C#/VB.NET, you can use the Impersonator
class (which uses the LogonUser
internally):
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运行应用程序,例如:
Run the application from Inno Setup like:
[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;
如果您不熟悉控制台应用程序开发,请注意,使用 PsExec
(确实如此)
If you are not experienced with a console application development, note that the "tiny" application can be even a batch file with use of the runas
command (which does not allow specifying password automatically) or the PsExec
(which does).
这篇关于Inno Setup以另一个用户身份运行/执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!