Pass parameters to InstallUtil from Powershell

2019-08-06 11:55发布

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条回答
叛逆
2楼-- · 2019-08-06 12:23

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

查看更多
登录 后发表回答