I have created a powershell script to retrive the Service status for list of computers but its only giving the first servive status. so kindly help me on this. my code is mention below.
To add a specific service, you’ll have to first pick it out of the collection like so:
# This is a Wmi query filter
$service = get-wmiobject Win32_service -computername $strComputer -Filter { Name = 'serviceName' }
# This is a where-object filter with PSv2 syntax. I'm assuming this is the version you're using since you use Get-WmiObject?
# Note: It's much slower than using a Wmi query filter.
$service = get-wmiobject Win32_service -computername $strComputer | Where { $_.Name -eq 'serviceName' }
On a completely different note, may I suggest you don’t use single letter variable for anything but iterators? It may not be an issue right with this script, but as they get bigger and longer, eventually you’ll find yourself scratching your head over why everything is called $a, $b, $c, $d and $e.
I need every service name and service status of every computers which is stored in (D:\list.txt) and i need output in csv or xls. so that i can filter it and find the stopped service on those computers.
OK, as Martin mentioned, you will need another loop over the collection returned by Get-WmiObject in your code. I’ve revised your original code to reflect this, and also renamed several variables to have more descriptive and useful meaning:
However, if you run this, you will notice that it’s extremely slow. Manipulating Excel spreadsheets via PowerShell takes a while, and every single cell update (3 of them per service, per computer) adds up. Personally, I would prefer to just generate a CSV file using PowerShell’s Export-Csv cmdlet. You can open that in Excel, pretty up the formatting and save it back as a spreadsheet if you like. (You can probably even script that with much less time than it would take to build the whole thing via the Excel COM objects directly.)
This will execute much, much faster than the original version. For starters, it gets rid of all the overhead of working with the Excel COM objects. It also passes all of the computer names to Get-WmiObject, and Get-WmiObject will perform parallel queries against several computers at once.
Before you port anything outside of Powershell, you should validate the data is what you want. You can get services in Powershell with WMI or the Get-Service cmdlet, which both take string arrays (e.g. string) as arguments for the computer. Try running some of these commands:
The results should produce Stopped services on the machines, however, the second method filters out Stopped services BEFORE it returns results where the first method returns all results and THEN filters. This won’t make a huge difference for 5 computers, but if you are doing a 100, then you want to only return exactly what you want from the computers. If you want or need to send the Excel spreadsheet somewhere else, then you can use the methodology you are using. However, if you just want to compare services quickly in a GUI form, then you should try something like this:
Get-WMIObject -Class Win32_Service -ComputerName Computer1, Computer2 -Filter "State = 'Stopped'" | Select PSComputerName, Name, State | Out-GridView