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
}
}