MSI Custom Action用管理员权限去执行

最近在维护之前一些VS 2005自带的安装包工程做了msi的安装包

发现在Custom Action中执行一些exe等 没有获得Admin权限,导致操作失败。

经过网上搜索解决方案发现,需要修改属性,但是这些属性需要用Custom Action

msi 很多属性要靠js脚本来添加:


下面来看一段添加Custom Action使用admin权限的脚本

// Usage: CustomAction_NoImpersonate.js <msi-file>
// Performs a post-build fixup of an MSI to change all
// deferred custom actions to include NoImpersonate 

// Constant values from Windows Installer
var msiOpenDatabaseModeTransact = 1; 

var msiViewModifyInsert = 1
var msiViewModifyUpdate = 2
var msiViewModifyAssign = 3
var msiViewModifyReplace = 4
var msiViewModifyDelete = 6 

var msidbCustomActionTypeInScript = 0x00000400;
var msidbCustomActionTypeNoImpersonate = 0x00000800 

if (WScript.Arguments.Length != 1)
{
  WScript.StdErr.WriteLine(WScript.ScriptName + " file");
  WScript.Quit(1);
} 

var filespec = WScript.Arguments(0);
var installer = WScript.CreateObject("WindowsInstaller.Installer");
var database = installer.OpenDatabase(filespec, msiOpenDatabaseModeTransact); 

var sql
var view
var record 

try
{
  sql = "SELECT `Action`, `Type`, `Source`, `Target` FROM `CustomAction`"
  view = database.OpenView(sql);
  view.Execute();
  record = view.Fetch();
  while (record)
  { 

    if (record.IntegerData(2) & msidbCustomActionTypeInScript)
    {
      record.IntegerData(2) = record.IntegerData(2) | msidbCustomActionTypeNoImpersonate;
      view.Modify(msiViewModifyReplace, record);
    }
    record = view.Fetch();
  } 

  view.Close();
  database.Commit();
}
catch(e)
{
  WScript.StdErr.WriteLine(e);
  WScript.Quit(1);
} 

PS:如果你不知道如何使用Js,请参考:

Here are the instructions that can be used to modify an MSI built in VS 2005 to set the NoImpersonate attribute for all deferred custom actions:

  1. Copy and paste the code listed below and save it to the directory that contains the Visual Studio project you are working on with the name CustomAction_NoImpersonate.js (or download thesample script and extract the contents to the project directory)
  2. Open the project in Visual Studio 2005
  3. Press F4 to display the Properties window
  4. Click on the name of your setup/deployment project in the Solution Explorer
  5. Click on the PostBuildEvent item in the Properties window to cause a button labeled “…” to appear
  6. Click on the “…” button to display the Post-build Event Command Line dialog
  7. Add the following command line in the Post-build event command line text box:
    cscript.exe “$(ProjectDir)CustomAction_NoImpersonate.js” “$(BuiltOuputPath)”
  8. Build your project in Visual Studio 2005 – now all deferred custom actions will have the NoImpersonate bit set for them

下面是通过JS来实现 msi安装包安装完成后重启系统的功能

Option Explicit

Const msiOpenDatabaseModeTransact = 1

Dim msiPath : msiPath = Wscript.Arguments(0)

Dim installer
Set installer = Wscript.CreateObject("WindowsInstaller.Installer")
Dim database
Set database = installer.OpenDatabase(msiPath, msiOpenDatabaseModeTransact)

Dim query
query = "INSERT INTO Property(Property, Value) VALUES('REBOOT', 'Force')"
Dim view
Set view = database.OpenView(query)
view.Execute

database.Commit


让安装包安装后不会在控制面板中出现卸载的js脚本


Option Explicit

Const msiOpenDatabaseModeTransact = 1

Dim msiPath : msiPath = Wscript.Arguments(0)

Dim installer
Set installer = Wscript.CreateObject("WindowsInstaller.Installer")
Dim database
Set database = installer.OpenDatabase(msiPath, msiOpenDatabaseModeTransact)

Dim query
query = "INSERT INTO Property(Property, Value) VALUES('ARPSYSTEMCOMPONENT', '1')"
Dim view
Set view = database.OpenView(query)
view.Execute

database.Commit


常见MSI安装包中的属性:

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值