【问题标题】:Cannot find service when attempting to remotely stop via Powershell / MSBuild尝试通过 Powershell / MSBuild 远程停止时找不到服务
【发布时间】:2014-03-14 10:13:32
【问题描述】:

我的任务是在我们的持续集成构建过程中将使用 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 远程启动和停止服务,但遇到了类似的问题。

我复习的一些问题

Using PowerShell credentials without being prompted for a password

Powershell stop-service error: cannot find any service with service name

Powershell Get-WmiObject Access is denied

Saving credentials for reuse by powershell and error ConvertTo-SecureString : Key not valid for use in specified state

我在这个问题上花费的时间超出了我的实际承受能力,因此如果有任何帮助/指导/cmets 可能会有所帮助,我将不胜感激。

谢谢!

更新

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

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

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

【问题讨论】:

  • 我不确定它是否有帮助,但我将从初始诊断开始 - 启动不带名称参数的 get-service(仅使用 ComputerName)并确保(使用手动检查)此命令确实有效到达您的服务器,并且不在本地执行
  • 试试SC.exe
  • gwmi win32_service -credential

标签: powershell service msbuild continuous-integration topshelf


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的 cmets。我将在今天晚些时候重新讨论这个问题并就结果提出建议。
  • MSBuild 目标文件肯定是在向 Powershell 传递参数。我能够确认这一点,因为完整的命令在日志文件中可见。但是,我没时间看这个了。非常感谢您的 cmets。
【解决方案2】:

我在黑暗中进行彻底的尝试,但是您的服务名称需要 TopShelf instance 名称,包括您调用 get-service 时的名称。

例如,您的服务可能称为“MyWindowsService”,但您需要以编程方式查找的是“MyWindowsService$default”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2012-09-06
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多