2017-08-15 44 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

相關問題