在 Windows Installer Commit 中运行批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/998703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run batch file in Windows Installer Commit
提问by Dour High Arch
I am having no success in modifying a Windows Installer MSI to run a batch file after the primary output has been installed. I have searched for this and found many suggestions, but nothing that works.
在安装主要输出后,我无法成功修改 Windows Installer MSI 以运行批处理文件。我已经搜索了这个并找到了很多建议,但没有任何效果。
- Add a Custom Action
- Custom actions can only be executable files. A batch file is not executable.
- Create a Windows Scripting Host executable that runs the batch file
- Many people have tried to do this, including on SO, and no one has been able to get this to work.
- Use WIX or InstallShield
- I do not have the time or authority to change company installer technology.
- Edit the .msi with Orca and add a custom property, then a custom action, then edit the InstallExecuteSequence, ...
- I have been trying this for hours and have only created installers that throw a system error when I run them.
- 添加自定义操作
- 自定义操作只能是可执行文件。批处理文件不可执行。
- 创建运行批处理文件的 Windows 脚本宿主可执行文件
- 许多人都尝试过这样做,包括在SO 上,但没有人能够做到这一点。
- 使用 WIX 或 InstallShield
- 我没有时间或权限更改公司安装程序技术。
- 使用 Orca 编辑 .msi 并添加自定义属性,然后添加自定义操作,然后编辑 InstallExecuteSequence,...
- 我已经尝试了几个小时,并且只创建了在运行时抛出系统错误的安装程序。
回答by William Leara
What you said above is incorrect:
你上面说的不正确:
Custom actions can only be executable files. A batch file is not executable.
自定义操作只能是可执行文件。批处理文件不可执行。
Custom Actions (even without tools like InstallShield) can be .EXE, VBScript, JScript, or DLL based. If you write a DLL, you can write whatever code you want to call a batch file or make any changes you want to the system -- there is no limit.
自定义操作(即使没有像 InstallShield 这样的工具)可以是基于 .EXE、VBScript、JScript 或 DLL 的。如果您编写一个 DLL,您可以编写任何您想要调用批处理文件的代码或对系统进行任何您想要的更改——没有限制。
Update: an example that worked for me: (entry in CustomAction table)
更新:一个对我有用的例子:(CustomAction 表中的条目)
Action Test
Type 34
Source SystemFolder
Target cmd.exe /c c:\test.bat
ExtendedType <blank>
回答by Dour High Arch
I was able to solve this by creating an EXE consisting essentially of:
我能够通过创建一个主要包含以下内容的 EXE 来解决这个问题:
System.Diagnostics.Process.Start(pathToBatchFile);
Adding the EXE to the MSI file then running it as a custom action.
将 EXE 添加到 MSI 文件,然后将其作为自定义操作运行。
回答by sumenigma
Make an EXE that makes a temporary .bat file to change to the whatever directory you want and CALL
another bat file that does what you need.
制作一个 EXE,使临时 .bat 文件更改为您想要的任何目录和CALL
另一个执行您需要的 bat 文件。
Tell the custom action to use that exe.
告诉自定义操作使用该 exe。
The first argument to a CPP exe is the path to the exe itself. This can be used to orient yourself, and that information could be used in the making of the temporary .bat
CPP exe 的第一个参数是 exe 本身的路径。这可用于定位自己,该信息可用于制作临时 .bat
回答by dashesy
While the answer suggested by "William Leara" is a very good start, I found a better solution hereworth mentioning.
虽然“William Leara”建议的答案是一个非常好的开始,但我在这里找到了一个更好的解决方案,值得一提。
Another advantage this method has is that you can put your batch file in the application install folder rather than root C folder, but we use special flags to defer the execution to after FileCopyaction, we also use noImpersonateto have higher privilege needed to run from Program Files
这种方法的另一个优点是您可以将批处理文件放在应用程序安装文件夹而不是根 C 文件夹中,但是我们使用特殊标志将执行推迟到FileCopy操作之后,我们还使用noImpersonate来获得更高的运行权限程序文件
A few notes:
一些注意事项:
- Use TARGETDIRinstead of INSTALLDIR
- Make sure to quote the paths.
- 使用TARGETDIR而不是INSTALLDIR
- 确保引用路径。
These are my two custom actions. In my case I want to force nativecommand processor to fix a few registry entries thus I do not use COMSPEC.
这是我的两个自定义操作。在我的情况下,我想强制本机命令处理器修复一些注册表项,因此我不使用COMSPEC。
First action (type 50) performs command in Source with input arguments of Target, because Type is 50+1024+2048 to it is deferred action with noImpersonateflag.
第一个操作(类型 50)使用 Target 的输入参数在 Source 中执行命令,因为 Type 是 50+1024+2048 到它是带有noImpersonate标志的延迟操作。
- Action: FinalAction
- Type: 3122
- Source: NCOM
- Target: /c "[TARGETDIR]Fixup.bat"
- 行动:最终行动
- 类型:3122
- 资料来源:NCOM
- 目标:/c "[TARGETDIR]Fixup.bat"
Second action defines NCOMproperty immediately:
第二个动作立即定义NCOM属性:
- Action: FinalNativeSys
- Type: 51
- Source: NCOM
- Target: [WindowsFolder]System32\cmd.exe
- 行动:FinalNativeSys
- 类型:51
- 资料来源:NCOM
- 目标:[WindowsFolder]System32\cmd.exe
Then you should add these actions to InstallExecuteSequence:
然后您应该将这些操作添加到InstallExecuteSequence:
- Action: FinalAction
- Condition: NOT Installed
- Sequence: 5002
- 行动:最终行动
- 条件:未安装
- 序列:5002
And this one:
和这个:
- Action: FinalNativeSys
- Condition:
- Sequence: 5001
- 行动:FinalNativeSys
- 健康)状况:
- 序列:5001
Note that in my case I could easily tweak the actions to call reg.exe import Fixup.regtoo. Finally one can use msitranas suggested herein a post build to automate the procedure.
请注意,在我的情况下,我也可以轻松调整操作以调用reg.exe import Fixup.reg。最后一个可以使用msitran的建议这里的后生成自动化的过程。
回答by eugene.sushilnikov
In my case, I added "Custom Actions".
On folder "Install" choose "Add custom action"
Choose cmd.exe (c:\windows\system32\cmd.exe)
Then in properties of cmd.exe edit "Arguments" to
"/Install /c C:\myApp.exe --exec-some-action"
就我而言,我添加了“自定义操作”。
在文件夹“安装”上选择“添加自定义操作”
选择 cmd.exe (c:\windows\system32\cmd.exe)
然后在 cmd.exe 的属性中将“参数”编辑为
“/Install /c C:\myApp.exe --exec-some-action"