从 Powershell 将参数传递给 InstallUtil [英] Pass parameters to InstallUtil from Powershell

查看:28
本文介绍了从 Powershell 将参数传递给 InstallUtil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Power-shell 安装 Windows 服务,如下所示.

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

我收到以下异常.

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..

但以下命令有效.

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

我正在尝试使用动态名称安装 Windows 服务,并使用 Power-shell 传递服务名称.任何帮助表示赞赏.

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.

答案在 VS 2008 中是正确的.但在 VS 2012 中会失败.因为 InstallUtil 被修改了.

你应该使用$sn = """" + $serviceName + " " + $exeName + """"

原因是,InstallUtil(2.0) 会自动添加引号,因此我们应该忽略它们(如答案中所示).但是 InstallUtil(4),如果字符串在任何位置包含引号(这是一个错误? - 他们应该检查字符串的开头和结尾是否有引号 - 目前这会破坏所有 2.0 代码).

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).

反射器是你的朋友.

推荐答案

你的问题在这一行:

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

如果你要用更简单的东西替换它,或者像这样:

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

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

它会起作用

这篇关于从 Powershell 将参数传递给 InstallUtil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆