1
votes

I am trying to get my head around Powershell and have the following:

$servers = Get-Content -Path C:\servers.txt

Get-Process -name AVProcess -cn $servers | % where {$_.Status -eq "Running"}

#Export-Csv -path "C:\server-process-sophos.csv"

I get an error, it looks like i cant use the $servers Variable within the parameter computername, also sending to the CSV file is way off. Ideally i'd like to be able to pipe each server name, status of the process into rows in a CSV.

I am trying to return a list of servers which do/dont have that process running or not - so i can see if they have AV installed essentially for an inventory report (this is not good if some dont have AV!!!). How can i best go about this?

Thanks in advance

1
AV normally runs as a Service so it might be better to look at the Service status rather than the process. stackoverflow.com/questions/12516574/…James C.

1 Answers

0
votes

Get-Process doesn't have a property "Status." It is better/easier to check service status on each machine using Get-Service. You just need to modify service name in this code so it matches your service name. Also output goes to two different files, one is used when service is available for checking, the other is used when there is no service with your needed name. It works with PowerShell V3.

$servers = Get-Content -Path C:\servers.txt
$ErrorActionPreference='stop'

ForEach ($server in $servers) {

   try
   {                                                 
   Get-Service -name <enterServiceNameHere> -cn $server | Export-Csv -append -Path "C:\server-process-sophos.csv" -noType -Force
   }

   catch [System.Management.Automation.ActionPreferenceStopException]
   {
   $psObject = $null
   $psObject = New-Object psobject
   $OutputString = "cannot find this service on: " + $server
   Add-Member -InputObject $psObject -MemberType noteproperty -Name "List" -Value $OutputString
   Export-Csv -InputObject $psObject -Append -path "C:\ServersWithNoService.csv" -NoType -Force
   }
}