Reputation: 1139
How to get the specific services based on the exact specific service name listed on the array of strings on a remote server?
Status Name DisplayName
------ ---- -----------
Running DFS DFS Namespace
Running DFSR DFS Replication
Running DNS DNS Server
Running EventSystem COM+ Event System
Running IsmServ Intersite Messaging
Running KDC Kerberos Key Distribution Center
Running LANMANServer Server
Running LANMANWorkstation Workstation
Running Netlogon NETLOGON
Running NTDS Active Directory Domain Services
Running RPCSs Remote Procedure Call (RPC)
Running SAMSs Security Accounts Manager
Running W32Time Windows Time
Because the below script returns the wrong services from the supplied name from $ServiceList variable.
There was no mention about Afd service, but it still showing:
One of the required Services on the list, DNS is not even showing:
Note: the Powershell ISE has been executed as Administrator and has Domain Admins credentials. The script has been executed successfully with no issue, however, the results are not according to the specific lists of $ServiceList variable content.
This is the script I have so far:
$ServiceList = @(
'DFS'
'DFSR'
'DNS'
'EventSystem'
'IsmServ'
'KDC'
'LANMANServer'
'LANMANWorkstation'
'NETLOGON'
'NTDS'
'RPCSs'
'SAMSs'
'W32Time'
)
$input = Get-ADDomainController -Filter * | Select-Object -ExpandProperty HostName
$input | ForEach-Object `
{
$Servers = ($_ -split '\.')[0]
$Servers | ForEach-Object `
{
$Server = $_
Write-Host "Processing $($Server) ..." -ForegroundColor Cyan
if (Test-Connection -ComputerName $Server -Count 2 -Quiet)
{
foreach ($ServiceToCheck in $ServiceList)
{
Write-Host "`tChecking $($Server) - $($ServiceToCheck) ..." -ForegroundColor Yellow
Try
{
If ($services = Get-Service -ServiceName $ServiceToCheck -ComputerName $Server)
{
$services | Select-Object -ExpandProperty ServicesDependedOn | Select-Object StartType, Status, Name, DisplayName, Machinename, @{ N = 'Service Dependency'; E = { (Get-Service -Name $_.ServicesDependedOn -ComputerName $Server | ForEach-Object { "$($_.Name): $($_.Status)" }) -join '; ' } }
}
Else
{
"" | Select-Object @{ n = "Status"; e = { "Services not found" } },
@{ n = "DisplayName"; e = { "$($_.Services)" } },
@{ n = "Machinename"; e = { $Server } }
}
}
Catch
{
"" | Select-Object @{ n = "Status"; e = { "Error checking services" } },
@{ n = "DisplayName"; e = { "" } },
@{ n = "Machinename"; e = { $Server } }
}
}
}
else
{
"" | Select-Object @{ n = "Status"; e = { "Couldn't ping server" } },
@{ n = "DisplayName"; e = { "" } },
@{ n = "Machinename"; e = { $Server } }
}
}
} | Out-GridView
Upvotes: 0
Views: 249
Reputation: 61068
I just did a rewrite of your code.
I have removed variable $input
, as that is an Automatic variable in PowerShell.
I also got rid of the second $Servers | ForEach-Object
loop and made sure all parts of the code output a similar object to output in the GridView
$ServiceList = 'DFS','DFSR','DNS','EventSystem','IsmServ','KDC','LANMANServer',
'LANMANWorkstation','NETLOGON','NTDS','RPCSs','SAMSs','W32Time'
Get-ADDomainController -Filter * | Select-Object -ExpandProperty HostName | ForEach-Object {
$Server = ($_ -split '\.')[0] # get the server name from the HostName
Write-Host "Processing $($Server) ..." -ForegroundColor Cyan
if (Test-Connection -ComputerName $Server -Count 2 -Quiet) {
foreach ($ServiceToCheck in $ServiceList) {
Write-Host "`tChecking $($Server) - $($ServiceToCheck) ..." -ForegroundColor Yellow
Try {
$svc = Get-Service -ServiceName $ServiceToCheck -ComputerName $Server -ErrorAction Stop
if ($svc) {
# successfully found a service
# get basic info about services this service depends on (if any)
$dependingOn = (Get-Service -Name $_.ServicesDependedOn -ComputerName $Server |
ForEach-Object { "$($_.Name): $($_.Status)" }) -join '; '
$svc | Select-Object StartType, Status, Name, DisplayName, Machinename,
@{ n = 'Service Dependency'; e = { $dependingOn } }
}
else {
# service not found
"" | Select-Object StartType,
@{ n = "Status"; e = { "Service not found" } },
@{ n = "Name"; e = { $ServiceToCheck } },
DisplayName,
@{ n = "Machinename"; e = { $Server } },
'Service Dependency'
}
}
Catch {
# Get-Service returned an error in $_.Exception.Message
"" | Select-Object StartType,
@{ n = "Status"; e = { "Error checking services" } },
@{ n = "Name"; e = { $ServiceToCheck } },
DisplayName,
@{ n = "Machinename"; e = { $Server } },
'Service Dependency'
}
}
}
else {
# server is off-line
"" | Select-Object StartType,
@{ n = "Status"; e = { "Couldn't ping server" } },
@{ n = "Name"; e = { $ServiceToCheck } },
DisplayName,
@{ n = "Machinename"; e = { $Server } },
'Service Dependency'
}
} | Out-GridView
Please can you check this and let us know?
Upvotes: 1