powershell - 尝试通过 Powershell/MSBuild 远程停止时找不到服务

标签 powershell service msbuild continuous-integration topshelf

我有一项任务是将使用 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

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

谢谢!

更新

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

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

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

最佳答案

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

我建议你找出如何传递这个变量,否则脚本将无法正确选择它。

关于powershell - 尝试通过 Powershell/MSBuild 远程停止时找不到服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22402028/

相关文章:

security - 使用 powershell Set-GPPermissions cmdlet 设置 GPO 安全过滤器

iis - 如何从 PowerShell 指定应用程序池身份用户和密码

grails - 在脚本中使用 Grails 服务

git - 从 MSBuild 获取 Git Long Hash

msbuild - 如何使用 IIS 6 Web Deploy 部署到非默认虚拟路径?

regex - 为什么\p{Lu} 匹配小写字母?

powershell - 找不到 AzureRunAsConnection

java - 使用 Spring Controller 列出数据库中的对象

javascript - Sql 报告服务和 Javascript

msbuild - *.targets 无法再次导入。已经导入了