4

作为我们持续集成构建过程的一部分,我有一项任务是将使用 Topshelf 创建的两个 Windows 服务部署到测试服务器。

我的 MSBuild 目标文件如下:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target Condition="'$(ConfigurationName)'=='Release'" Name="StopService">
    <Exec Command="powershell.exe -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; { &amp;&apos;.\ServiceStop.ps1&apos; } &quot;" ContinueOnError="true" />
  </Target>

</Project>

这将执行位于项目内名为 ServiceStop.ps 的同一文件夹中的 Powershell 脚本(好吧,几行):

$service = get-service -ComputerName MyServerName -Name 'MyServiceName'

stop-service -InputObject $service -Verbose

问题

当我从 TFS 中对新构建进行排队时,脚本确实成功执行;但是,该 get-service命令无法找到有问题的服务 - 尽管它确实存在并且正在运行。构建日志中的具体错误如下:

Get-Service : Cannot find any service with service name 'MyServiceName' (TaskId:198)

当脚本从我的机器本地运行时,远程机器上的服务被发现并成功停止,让我认为这是某种权限问题。

我试过的

我对 Powershell 的经验非常有限。我读到凭据可以存储在 Powershell 对象中,如下所示:

$pw = Read-Host -AsSecureString "Enter password"
$pw | ConvertFrom-SecureString | Out-File -Path .\storedPassword.txt

$password = get-content .\storedPassword.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myAdminAccountName",$password

但是,似乎get-service没有任何方法可以将凭据传递给它。

我还尝试使用 PSExec 远程启动和停止服务,但遇到了类似的问题。

我复习的一些问题

在不提示输入密码的情况下使用 PowerShell 凭据

Powershell停止服务错误:找不到具有服务名称的任何服务

Powershell Get-WmiObject 访问被拒绝

保存凭据以供 powershell 重用和错误 ConvertTo-SecureString : Key not valid for use in specified state

我在这个问题上花费的时间比我真正能承受的要多,所以如果有任何帮助/指导/评论可能会有所帮助,我将不胜感激。

谢谢!

更新

我能够确认 Powershell 脚本正在接收来自 MSBuild 任务的信息,因为日志显示了 Powershell 输出。

但是,我没时间找到解决方案,而是通过将更新的服务二进制文件放到目标服务器上并编写了一个从那里安装它们的 Powershell 脚本来解决这个问题。

非常感谢那些对这个问题发表评论的人。

4

2 回答 2

0

I'm making a complete stab in the dark, but your service name needs the TopShelf instance name including when you call get-service.

For example your service might be called "MyWindowsService" but what you need to programmatically look for is "MyWindowsService$default"

于 2014-09-01T15:49:29.003 回答
0

似乎没有任何东西从 MSBuild 目标文件传递到 powershell 脚本。您正在通过 ' 标记定义所涉及的服务的名称,因此它采用该名称而不是 MSBuild 目标文件正在执行的操作。

我建议您了解如何传递此变量,否则脚本将无法正确获取此变量。

于 2014-03-17T16:59:13.243 回答