【发布时间】: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 "& { &'.\ServiceStop.ps1' } "" 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
我在这个问题上花费的时间超出了我的实际承受能力,因此如果有任何帮助/指导/cmets 可能会有所帮助,我将不胜感激。
谢谢!
更新
我能够确认 Powershell 脚本正在接收来自 MSBuild 任务的信息,因为日志显示了 Powershell 输出。
但是,我没有时间找到解决方案,而是通过将更新的服务二进制文件放到目标服务器上并编写了一个从那里安装它们的 Powershell 脚本来解决这个问题。
非常感谢那些对此问题发表评论的人。
【问题讨论】:
-
我不确定它是否有帮助,但我将从初始诊断开始 - 启动不带名称参数的 get-service(仅使用 ComputerName)并确保(使用手动检查)此命令确实有效到达您的服务器,并且不在本地执行
-
试试SC.exe
-
或
gwmi win32_service -credential。
标签: powershell service msbuild continuous-integration topshelf