您的位置:首页 > Web前端 > JavaScript

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

2016-04-29 18:10 381 查看
最近在维护之前一些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:

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)
Open the project in Visual Studio 2005
Press F4 to display the Properties window
Click on the name of your setup/deployment project in the Solution Explorer
Click on the PostBuildEvent item in the Properties window to cause a button labeled “…” to appear
Click on the “…” button to display the Post-build Event Command Line dialog
Add the following command line in the Post-build event command line text box:

cscript.exe “$(ProjectDir)CustomAction_NoImpersonate.js” “$(BuiltOuputPath)”
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息