2017-08-15 43 views
1

我有一个Powershell脚本来查找特定的服务器及其相应的服务帐户。如果我修改脚本以使用单个服务器和单个服务帐户,结果就是我所期望的。如果我环通的服务器和帐号,我收到以下错误:为什么在循环时我收到RPC服务器不可用错误?

################################################################# 
# Find Service Account(s) used to start Services on a Server(s) # 
################################################################# 

$accounts = (Get-Content C:\Users\location\Scripts\Service_Accounts.txt) 

Remove-Item -path C:\Users\location\Scripts\ServiceAccountFnd.txt -force -erroraction silentlycontinue 

Import-Module ActiveDirectory # Imports the Active Directory PowerShell module # 

## Retrieves servers in the domain based on the search criteria ## 

$servers=Get-ADComputer -Filter {Name -Like "namehere*"} -property * 

## For Each Server, find the services running under the user specified in $account ## 
ForEach ($server in $servers) { 
    Write-Host $server 
    ForEach ($account in $accounts) { 
     Write-Host $account 
     Get-WmiObject Win32_Service -ComputerName $server | Where-Object {$_.StartName -like "*$account*"} | Format-Table -HideTableHeaders -property @{n='ServerName';e={$_.__SERVER}}, StartName, Name -AutoSize | Out-File -FilePath C:\Users\location\Scripts\ServiceAccountFnd.txt -append -Width 150 
    } 
} 
+0

你确定你联系的系统甚至在线吗?大多数情况下,使用这种脚本时,我使用'Test-Connection'来验证服务器甚至在尝试WMI查询之前做出响应。 –

+0

我不是100%确定的,也许99.9%肯定:-)但会使用您的建议来测试连接。谢谢。 – Gringo

+0

你好。你可以尝试用这个'$ servers = Get-ADComputer -Filter {Name-Like“namehere *”} -property来替换这个'$ servers = Get-ADComputer -Filter {Name-Like'这里*“} -property *' * |选择名称就像vrdse在回答 – Vitaly

回答

1

$server变量不仅包含主机名,也是AD计算机对象的所有属性。

尝试将ComputerName的值更改为$server.name

如果没有帮助:您是否可以确认,您是否在循环中使用了与循环中相同的计算机?我假设你试图访问另一台未按预期配置的计算机。

除此之外,我建议您使用Get-CimInstance而不是Get-WmiObject,因为它不使用RPC,而是默认使用WinRM。 WinRM防火墙更友好,更安全,速度更快。

+0

说,谢谢,@ vrdse,解决了这个问题。 – Gringo

相关问题