Scan ad for new servers + if specific service is running on them

Hi everyone
I have script that is scanning my DC for new servers that has been added to my domain + sending an email with the Name, IP and OS of the new server.

What I am looking, is how can i add a line to this script that will check if those new servers has specific Service running on them?

The output that I’m receiving to the email now is:

Dear IT Team,
Weekly AD scan for new servers:

Name IPv4Address OperatingSystem
testsrv x.x.x.x Windows Server 2016 Standard

So i want somehow to add here a “Service” tab that will say “Servicename” running or no, in those servers.

The script:

#>
“Start: “+ (Get-Date)
try
{
$domain=””
if ($domain -eq””)
{
$domain = Get-ADDomain
}
else
{
$domain = Get-ADDomain -Identity $domain
}
$domain_name=$domain.name
$distinguished_name = $domain.DistinguishedName
$domain_controller = (Get-ADDomainController -server $domain_name).HostName
$logging_level=1
$search_base = “OU=my-ous,” + $distinguished_name
$days_to_search =-300 # this value needs to be negative integer
if ($days_to_search -ge 0) {throw “Value for variable $days_to_search must be a negative integer in days.”}
$date_filter = (get-date).adddays($days_to_search)
$computers=get-adcomputer -SearchBase $search_base -Properties * -Filter {Created -gt $date_filter -and operatingsystem -like “*windows*”} -server $domain_controller
$computers | Select-Object Name, Created,DistinguishedName,IPv4Address,OperatingSystem | ft -AutoSize

}
Catch
{
“Error occurred: ” + (Get-Date)
throw
}
$Body = @”
<html>
<body>
<p>Dear Team,
<br/>
<p>Weekly scan for new servers:</p>
$($computers | ConvertTo-Html -Property Name,IPv4Address,OperatingSystem)
<br/>
</body>
</html>
“@
Send-MailMessage -To “myemail@test.com” -From ‘do_not_reply@test.com’ -Subject ‘New Servers’ -Body $Body -SmtpServer ‘mysmtp server’ -BodyAsHtml -Priority High

After you instantiate $computers, add this loop to add a property that is true or false depending on if the service is running on each computer. Then you can just add the Service_Running property to your Select-Object and ConvertTo-Html cmdlets.

$servicename = "your service name"
foreach ($comp in $computers) {
    $splat = @{
        NotePropertyName  = "Service_Running"
        NotePropertyValue = ([bool](Get-Service -ComputerName $comp.Name -Name $servicename -ErrorAction SilentlyContinue | 
                                Where-Object {$_.status -eq "Running"}))
        Force             = $true
    }
    $comp | Add-Member @splat
} #foreach

 

Another option is using calculated expressions:

$computers = get-adcomputer -SearchBase $search_base -Properties Name,Created,DistinguishedName,IPv4Address,OperatingSystem -Filter {Created -gt $date_filter -and operatingsystem -like “*windows*”} -server $domain_controller |
             Select-Object -Property Name, 
                                     Created,
                                     DistinguishedName,
                                     IPv4Address,
                                     OperatingSystem
                                     @{Name='ServiceStatus';Expression={[bool](Get-Service -ComputerName $_.Name -Name WSearch  | Where-Object {$_.status -eq "Running"})}}

I like Rob’s better. I don’t know why I didn’t think of that. Really only one additional line of code to existing script.