0

I have a script that will query list of servers to find if specific service is running or not. Service name on servers have common prefix thus making use of "SAM*"

This works fine if its only for 1 server, however results no value if its more than 2 and the code runs same number time as of number of servers..some expertise on this ..I'm new to the script

$machineName = get-content -path "C:\serverlist.txt"


foreach ($machine in $machineName) { 

     $serviceStatus = get-service -ComputerName $machineName -Name "SAM*"

     if ($serviceStatus.status -eq "Running") {

         Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status 
         $svcName = $serviceStatus.name 
         $svcState = $serviceStatus.status

     } else { 
         Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status 
         $svcName = $serviceStatus.name 
         $svcState = $serviceStatus.status

     }           

   } 
BenH
  • 9,766
  • 1
  • 22
  • 35

2 Answers2

0

You are calling the full collection $machinename inside your loop rather than the individual $machine.

$machineName = get-content -path "C:\serverlist.txt"


foreach ($machine in $machinename) { 

 $serviceStatus = get-service -ComputerName $machine -Name "SAM*"

 if ($serviceStatus.status -eq "Running") {

     Write-Host $machine `t $serviceStatus.name `t $serviceStatus.status 
     $svcName = $serviceStatus.name 
     $svcState = $serviceStatus.status

 } else { 
     Write-Host $machine `t $serviceStatus.name `t $serviceStatus.status 
     $svcName = $serviceStatus.name 
     $svcState = $serviceStatus.status

 }           

} 

Edit: Alternatively, you could change the variable name of the list and then individual object

$machineList = get-content -path "C:\serverlist.txt"


foreach ($machinename in $machineList) { 
BenH
  • 9,766
  • 1
  • 22
  • 35
  • Modified it .Seems like I made some silly mistake its printing all the status and for no service as well.Any suggestions foreach ($machine in $machineName) { $serviceStatus = get-service -ComputerName $machineName -Name "SAM*" if($serviceStatus.status -ne "Running" -and ![string]::IsNullOrEmpty($servicestatus.Name) { Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green $svcName = $serviceStatus.name $svcState = $serviceStatus.status Add-Content $report " $machineName" Add-Content $report "$svcName" Add-Content $report "$svcState"}else{}} – Bhavneet Singh Rana Jan 12 '17 at 12:05
  • Any suggestions @benh – Bhavneet Singh Rana Jan 12 '17 at 12:05
0

Please change $machineName to $machine in your code so it looks like this:

$serviceStatus = get-service -ComputerName $machine -Name "SAM*"
paul543
  • 178
  • 4
  • 16
  • Modified the code to get status only where service is not running or it doesn't exist .. Seems like I made some silly mistake..its printing all the status and for no service as well..Any suggestions – Bhavneet Singh Rana Jan 12 '17 at 11:53
  • foreach ($machine in $machineName) { $serviceStatus = get-service -ComputerName $machineName -Name "SAM*" if($serviceStatus.status -ne "Running" -and ![string]::IsNullOrEmpty($servicestatus.Name) { Write-Host $machineName t $serviceStatus.name t $serviceStatus.status -ForegroundColor Green $svcName = $serviceStatus.name $svcState = $serviceStatus.status Add-Content $report " $machineName" Add-Content $report "$svcName" Add-Content $report "$svcState"}else{}} – Bhavneet Singh Rana Jan 12 '17 at 12:08
  • @BhavneetSinghRana The issue is that when the service doesnt exist on some server/machine then it causes a terminating error. Please try checking this thread: http://stackoverflow.com/questions/27123198/check-with-powershell-if-service-is-installed-on-multiple-computers It is the same question that you have about getting the service status on multiple machines. – paul543 Jan 13 '17 at 16:47