【发布时间】:2018-07-19 05:36:54
【问题描述】:
我有一个简单的 PowerShell,它接受一个文件作为参数,其中包含以秒为单位指定的服务器名称、服务名称和超时,并且 powershell 根据命令停止/启动服务。我有 10 台服务器,其中配置了与应用程序相关的服务,并且我正在使用工作负载自动化功能在所有 10 台服务器上使用相同的参数文件和 powershell 的任何服务器上运行此代码。
所有这些都适用于硬编码在参数文件中的服务器名称。我们有n+2 Infrastructure,我为所有 10 台服务器创建了 DNS 别名,以使 Active-Standby 切换变得非常容易。现在问题出在停止/启动脚本上。例如:如果脚本在服务器 7 中被调用,所有服务都将被关闭,除了服务器 7 中的服务,我得到以下错误:
Get-Service : 找不到任何具有服务名称的服务 '应用程序-Server7'。在行:1 字符:1 + Get-Service -ComputerName app7test -Name Application-Server7 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Application-Server7:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
我刚刚在计算机“appnewora79”上使用PowerShell ISE测试了单行命令,下面是结果。
这很好用:
Get-Service -ComputerName appnewora79 -Name Application-Server7
Status Name DisplayName
------ ---- -----------
Stopped Application-Ser... Application-Server7
这失败了:
Get-Service -ComputerName app7test -Name Application-Server7
Get-Service : Cannot find any service with service name 'Application-Server7'
在 line:1 char:1 + Get-Service -ComputerName app7test -Name Application-Server7 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Application-Server7:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
有什么建议可以解决这个问题吗?
我也在下面尝试过:
当服务器名称被硬编码时,这一切都有效。如果我将其更改为 DNS 别名,则 Get-Service 在执行它的服务器上失败,同时获取仅与同一服务器相关的服务详细信息。
在 Server-1 上,如果我运行以下命令,它们都可以正常工作
Get-Service -ComputerName servername1
Get-Service -ComputerName servername2
Get-Service -ComputerName servername3
如果我用服务器 dns 别名更新上述命令并在 Server-1 上执行它们,那么只有第一个命令失败,即用 DNS 屏蔽的具有相同服务器名称的命令失败,如果我在服务器上运行相同的命令2,那么单独第二个就失败了
Get-Service -ComputerName dnsalias1
Get-Service -ComputerName dnsalias2
Get-Service -ComputerName dnsalias3
我试过了,test-connection 使用 DNS 别名,它们运行良好。尝试Get-WmiObject -Class WIN32_service 将计算机名称屏蔽为 DNS 别名,它运行良好。我不知道可能是什么问题。
以下是单独使用 Get-Service 的错误消息:
Get-Service:无法在计算机“dnsalias1”上打开服务控制管理器。此操作可能需要其他权限。 在行:5 字符:1 + 获取服务 -ComputerName dnsalias1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-Service], InvalidOperationException + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand
【问题讨论】:
-
我只是在同一个实验中尝试了测试连接,它没有导致任何问题,所以只有在获取服务时???
标签: powershell