作为我们的持续集成构建过程的一部分,我有一个任务要部署使用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远程启动和停止服务,但遇到了类似的问题。
我查阅过的一些问题
Powershell stop-service 错误: 找不到任何服务与服务名称
Powershell Get-WmiObject 访问被拒绝
为Powershell保存凭据以便重复使用,出现错误 ConvertTo-SecureString : Key not valid for use in specified state
我已经在这个问题上花费了比我能承受的更多的时间,所以希望得到任何有用的帮助/指导/评论。
谢谢!
更新
我确认了Powershell脚本从MSBuild任务接收到了信息,因为日志显示了Powershell输出。
然而,由于时间不够,我没有找到解决办法,而是通过将更新的服务二进制文件放在目标服务器上,并编写了一个Powershell脚本来安装它们,绕过了这个问题。
非常感谢那些对此问题发表评论的人。
gwmi win32_service -credential
。 - noam