【发布时间】:2019-02-13 07:09:44
【问题描述】:
我正在 VSTS 中创建一个构建定义,它将构建 Windows 服务并将 Windows 服务部署到特定服务器。
我能够将所有必需的 Windows 服务文件复制到服务器中。我正在使用 powershell 脚本来安装和启动这些 Windows 服务。我的服务器中已经有 powershell 脚本。我的最后一个 VSTS 任务远程 Powershell 任务正在抛出以下内容
[错误]连接到远程服务器失败并显示以下错误消息:WinRM 无法完成操作。验证指定的计算机名称是否有效,该计算机可通过网络访问,并且 WinRM 服务的防火墙例外已启用并允许从该计算机进行访问。默认情况下,公共配置文件的 WinRM 防火墙例外限制对同一本地子网中的远程计算机的访问。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。
Powershel 脚本代码:
$serviceName = "Service"
$displayName = "Service name"
$sourceLocation = "C:\temp\Service\bin\Release\*"
$destinationLocation = "C:\TestService\"
$binaryName = $destinationLocation + "Service1.exe"
$serviceName = "Service1"
$serviceDef = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
If ($serviceDef -eq $null)
{
New-Item $destinationLocation -ItemType directory
Copy-Item $sourceLocation $destinationLocation -Force
New-Service -Name $serviceName -StartupType Automatic -DisplayName $displayName -BinaryPathName $binaryName
}
else
{
# has already been installed
if($serviceDef.Status -eq "Running")
{
Stop-Service -Name $serviceName
}
Copy-Item $sourceLocation $destinationLocation -Force
}
Start-Service -Name $serviceName
$serviceName = "Service2"
$displayName = "Service2"
$sourceLocation = "C:\temp\Service2\bin\Release\*"
$destinationLocation = "C:\Service2\"
$binaryName = $destinationLocation + "Service2.exe"
$serviceName = "Service2"
$serviceDef = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
If ($serviceDef -eq $null)
{
New-Item $destinationLocation -ItemType directory
Copy-Item $sourceLocation $destinationLocation -Force
New-Service -Name $serviceName -StartupType Automatic -DisplayName $displayName -BinaryPathName $binaryName
}
else
{
# has already been installed
if($serviceDef.Status -eq "Running")
{
Stop-Service -Name $serviceName
}
Copy-Item $sourceLocation $destinationLocation -Force
}
Start-Service -Name $serviceName
【问题讨论】:
-
您是否在目标计算机上启用了 Windows 远程处理(
Enable-PSRemoting或winrm quickconfig)? -
这不是脚本的问题。这是 WinRM / PowerShell 远程处理的问题。
-
您是否使用托管代理来运行构建?可以从公共互联网访问远程机器吗?
-
我正在使用自托管代理。我 ping 了我试图从构建服务器运行 poweshell 脚本的服务器,即我托管构建代理的服务器
-
ping 成功
标签: windows powershell azure-devops powershell-remoting azure-pipelines-build-task