我有一個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
}
}
你確定你聯繫的系統甚至在線嗎?大多數情況下,使用這種腳本時,我使用'Test-Connection'來驗證服務器甚至在嘗試WMI查詢之前做出響應。 –
我不是100%確定的,也許99.9%肯定:-)但會使用您的建議來測試連接。謝謝。 – Gringo
你好。你可以嘗試用這個'$ servers = Get-ADComputer -Filter {Name-Like「namehere *」} -property來替換這個'$ servers = Get-ADComputer -Filter {Name-Like'這裏*「} -property *' * |選擇名稱就像vrdse在回答 – Vitaly