Service Report from list of computers

Code is almost done but seem to be missing something. The DisplayName is out-putting the the service name not the DisplayName of the service. So basically the Service is showing twice in the CSV file.


$ServiceSearch = "Firewall*"
$listtarget = get-content "c:\name_list.txt"

$infoColl = @()

Foreach($pcname in $listtarget) {
    Write-Host $pcname
	$infoObject = New-Object PSObject
	
     if (test-connection -quiet -computername $pcname -count 1) {
        try{
			$infoObject 
			Add-Member -inputObject $infoObject -memberType NoteProperty -name "ComputerName" -value $pcname -force
			Add-Member -inputObject $infoObject -memberType NoteProperty -name "Status" -value "Online" -force

			$ServiceFound = Get-Service $ServiceSearch -computername $pcname
			
			Add-member -inputObject $infoObject -memberType NoteProperty -name "Services" -value $ServiceFound
            Add-member -inputObject $infoObject -memberType NoteProperty -name "DisplayName" -value $ServiceFound
    	    $infoColl += $infoObject 
write-host $infoObject  
		}
		Catch [system.exception] 
        {"something Broke"}
	}	
	Else {    
       	Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $pcname -force
    	Add-Member -inputObject $infoObject -memberType NoteProperty -name "Status" -value "Offline" -force
        Add-member -inputObject $infoObject -memberType NoteProperty -name "Services" -value ""
        Add-member -inputObject $infoObject -memberType NoteProperty -name "DisplayName" -value ""
		
    	$infoColl += $infoObject        	
    }
}
	
	$infoColl | Export-Csv -path ".\PC Inventory $(Get-Date -f 'MM-dd-yyyy HH-mm').csv" -NoTypeInformation #Export the results in csv file. 

Any help with pointing out what is missing or incorrect would be appreciated.

May I suggest some improvements to your code?

$ServiceSearch = 'MpsSvc'
$ComputerNameList = Get-Content -Path 'c:\name_list.txt'

$Result = 
Foreach ($ComputerName in $ComputerNameList) {
    if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet ) {
        $ServiceList = @(Get-Service -Name $ServiceSearch -ComputerName $ComputerName)
        foreach ($Service in $ServiceList) {
            [PSCustomObject]@{
                ComputerName       = $ComputerName
                Online             = $true
                ServiceName        = $Service.Name
                ServiceDescription = $Service.DisplayName
                ServiceStatus      = $Service.Status
            }
        }
    }
    Else {
        [PSCustomObject]@{
            ComputerName       = $ComputerName
            Online             = $false
            ServiceName        = 'n/a'
            ServiceDescription = 'n/a'
            ServiceStatus      = 'n/a'
        }
    }
}

$Result | Export-Csv -Path ".\PC Inventory $(Get-Date -Format 'yyyy-MM-dd HH-mm').csv" -NoTypeInformation

I’d recommend NOT to use wildcards in the service name to query. If there are more than one services fitting to the pattern you use you will not get the results you’re looking for. If you want to query more than one service at a time you have to extend your code to reflect this requirement.

1 Like

I need the wildcard as the service that gets installs puts a unique number in the service name each time it installs. So when i am search 5000 computers that all have a different number at the end of the service name i need to use a wildcard. But i get your original point and would not otherwise use a wildcard if i didn’t have to.

Your code worked to give me the DisplayName but now i am missing the Services name. I need to have both the ServiceName and the DisplayName

Thank you Olaf… i added the add the ServiceName and looks like it is showing the right info now.

Great. I’m glad that we could help you. :+1:t4: :slight_smile:

Looks like i might have spoke too soon. While the script does work after i ran it fully the first time and got the results i found out that it is not reporting all the data correctly. On computers that have multiple installs of the service, the script is given errors of “System.Object”. Is there any way to get it so it reports all the services that meet my search criteria. I am basically trying to find what computers need to have the extra services uninstalled as it is causing issues having them all running.

That’s what I meant before. So you need a nested loop … changed my code suggestion above. Try it!

I thought i tried that already but will try it again. Should ServiceList actually be ServiceSearch in this code?

If i use $ServiceList then the csv file is blank. If i change it to $ServiceSearch then the headers and computer name displays but the service names are blank. So to me it is acting like it cant find the service.


$ServiceSearch = 'MsSrv*'
$ComputerNameList = Get-Content -Path '.\test.txt'

$Result = 
Foreach ($ComputerName in $ComputerNameList) {
    if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet ) {
        $Service = Get-Service -Name $ServiceSearch -ComputerName $ComputerName
        foreach ($Service in $ServiceSearch) {
        [PSCustomObject]@{
            ComputerName  = $ComputerName
            Online        = $true
            DisplayName   = $Service.DisplayName
            ServiceName   = $Service.ServiceName
            ServiceStatus = $Service.Status
        }
    }
    }
    Else {
        [PSCustomObject]@{
            ComputerName  = $ComputerName
            Online        = $false
            DisplayName   = 'n/a'
            ServiceName   = 'n/a'
            ServiceStatus = 'n/a'
            }
        }
    }


$Result | Export-Csv -Path ".\PC Inventory $(Get-Date -Format 'yyyy-MM-dd HH-mm').csv" -NoTypeInformation

Please copy the complete code snippet just like I posted it and try it.

That worked… now i need to compare line for line cause i thought my code was the same as what you had.

Thank you again Olaf

There are tools to do something like this or if you use VSCode you can do it there.