Pass parameters to InstallUtil from Powershell

你离开我真会死。 提交于 2019-12-24 08:59:52

问题


I am trying to install a windows service from Power-shell as follows.

$sn = """" + $serviceName + " " + $exeName + """"
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -i /ServiceName=[$sn]  $exeFilePath

I am getting the following exception.

Microsoft (R) .NET Framework Installation utility Version 2.0.50727.3053
Copyright (c) Microsoft Corporation.  All rights reserved.

Exception occurred while initializing the installation:
System.IO.FileNotFoundException: Could not load file or assembly 'file:///E:\Scheduled' or one of its dependencies. The system cannot f
ind the file specified..

But the following command work.

C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -i /ServiceName=["Scheduled Download Service"]  $exeFilePath

I am trying to install the Windows Service with a dynamic name and using Power-shell I pass the Service Name. Any help is appreciated.

The answer is correct for VS 2008. But will fail in VS 2012. Because InstallUtil is modified.

You should use $sn = """" + $serviceName + " " + $exeName + """"

The reason is, InstallUtil(2.0) automatically adds the quotes and so we should ignore them (as in the answer). But InstallUtil(4), this is skipped if the string contains quotes in any location (which is a bug? - they should have checked whether there are quotes in the beginning and ending of the string - Currently this breaks all 2.0 code).

Reflecter is your friend.


回答1:


Your problem is in this line:

$sn = """" + $serviceName + " " + $exeName + """"

If you will replace it with something simpler, or do it like this:

$sn = $serviceName.ToString() + " " + $exeName

It will work



来源:https://stackoverflow.com/questions/8396860/pass-parameters-to-installutil-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!