1

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

$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 传递服务名称。任何帮助表示赞赏。

答案对于 VS 2008 是正确的。但在 VS 2012 中会失败。因为 InstallUtil 已修改。

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

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

反射器是你的朋友。

4

1 回答 1

3

您的问题出在这一行:

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

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

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

它会工作

于 2011-12-06T08:15:05.963 回答