0

I have a custom cmdlet that can query the status of each service for a CSV-list of computers and then output to a CVS-file just nicely, see example below. I want to pick up this CSV-output (pipe it trhough really) and then run comparisons; pure array querying. For example, count the number of uniq computers and list every servicename that is running on all Computers, or Running services that do not run on all of them.

I have gone through many trials and although I can read the array input, select specific cols from it, I can't do select list of uniq Computers and/or count them. Below is the current version of my code where it refuses to collapse the ComputerName llist and show me uniq values and/or the proper Count of 3.

The outcome for services that run on each comp in this example should be:

SVc1

Where am I wrong in my thinking? Your help is much appreciated. although not new to programming, I am to PS



    function myTest {
    [cmdletBinding()]
        param (
           [Parameter(Mandatory=$True,
                       ValueFromPipeline=$True,
                       ValueFromPipelineByPropertyName=$True,
                       HelpMessage="Input CSV file listing ComputerNames, Servicename, State")]
            [array]$ListOfSvrsAndSvcs  
        )
        BEGIN {}
        PROCESS {
                Try {
                   #$ListOfRunningSvcs = $ListOfSvrsAndSvcs | where {$_.State -eq "Running"} | Select ComputerName, ServiceName
                   #write-output $ListOfRunningSvcs 
                   $UniqComps = $ListOfSvrsAndSvcs | group {$_.ComputerName} | select ComputerName

                   write-output $UniqComps 
                   }
                Catch {
                    write-warning $_.Exception.Message
                }
                finally {}
        }
        END {}
    }
    import-csv .\ComputerServicesList.csv | myTest | ft



My input CSV looks like:

CompName    SvcName    State
Localhost   Svc1       Running
Localhost   Svc2       Stopped
Comp1       Svc1       Running
Comp1       Svc2       Running
Comp1       Svc3       Running
Comp2       Svc1       Running
Comp2       Svc3       Running
1
  • The way you have it set up the rows are coming in one at a time so Group only has the one object to play with. Also your file has CompName and you are grouping on ComputerName which does not exist.
    – Matt
    Mar 16, 2015 at 17:32

1 Answer 1

0

Import-CSV does return an array so there is no need for a custom function per sei. Piping that straight into Group-Object would give you the answers you need.

$data = import-csv .\ComputerServicesList.csv
$uniqueComputerCount = ($data | Sort-Object CompName -Unique).Count
$data | Group-Object svcname | Where-Object{$_.Count -eq $uniqueComputerCount} | Select -ExpandProperty Name

That would return all the services that are present on all hosts. Which in the case is just Svc1. If you wanted to be sure they were running as well couple of minor changes will net the same output

$data | Group-Object svcname,state | Where-Object{$_.Count -eq $uniqueComputerCount} | Select-Object -ExpandProperty Name | ForEach-Object{($_ -split ",")[0]}

There is more to come from this but i'm trying to under stand the pure array querying component to the question. Are you hoping to process each row and compare to some tally variables as you go along? Seems like a waste when PowerShell handles it natively.

Currently your function is getting one row of data at a time and that single item is being passed to Group-Object. Like I said in my comments you also have to match your variables CompName is not equal to ComputerName

2
  • Thanks Matt, this got me in to a better direction. <br/> <pre> <code>function myTest { $data = import-csv .\ComputerServicesList.csv $uniqueComputerCount = ($data | Sort-Object ComputerName -Unique).Count write-host $uniqueComputerCount $RunningServices = $data | where-object {$_.State -eq "Running"} | Select ComputerName, ServiceName # Svcs that are shared. $SvcsCollapsed = $RunningServices | Group-Object ServiceName | where {$_.count -eq $uniqueComputerCount} | select Name write-output $SvcsCollapsed } myTest | ft </code> </pre>
    – Tropical
    Mar 16, 2015 at 18:58
  • @Tropical I dont know if you are still having issues but that amount of code in comments looks horrid. Please edit your question if you are still having issues. Ok I see so you are just using my code in your function then. If this answers your question then feel free to mark it as answered with the green check mark and Welcome to SO.
    – Matt
    Mar 16, 2015 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.