PowerShell的:如何汇总数据阵列? [英] Powershell: How to rollup Array data?

查看:123
本文介绍了PowerShell的:如何汇总数据阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的cmdlet,可以查询电脑的CSV列表,然后输出到CVS-文件中的每个服务的状态很好刚,见下面的例子。我要拿起这个CSV输出(管它trhough真的),然后运行比较;纯数组查询。例如,计算uniq的计算机的数量,并列出在所有计算机上运行或运行不上他们都运行服务的每一个服务名。

我经历了许多考验了,虽然我可以读取数组输入,从中选择特定COLS,我不能做uniq的计算机的选择列表和/或计数。下面是我的code当前版本的它拒绝折叠计算机名LLIST,并告诉我uniq的价值观和/或3正确的计数。

这在本例中的每个补偿运行服务的结果应该是:


SVc1

我在哪里错在我的思想?你的帮助是非常AP preciated。虽然没有新的节目,我是PS

     功能MYTEST {
    [cmdletBinding()]
        参数(
           [参数(强制性= $ True时,
                       ValueFromPipeline = $ True时,
                       ValueFromPipelineByPropertyName = $ True时,
                       HelpMessage =输入CSV文件,列出ComputerNames,服务名,国家)]
            [数组] $ ListOfSvrsAndSvcs
        )
        开始 {}
        PROCESS {
                尝试{
                   #$ ListOfRunningSvcs = $ ListOfSvrsAndSvcs |其中{$ _国-eq暗战。} |选择计算机名称,服务名称
                   #写输出$ ListOfRunningSvcs
                   $ UniqComps = $ ListOfSvrsAndSvcs |组{$ _计算机名。} |选择计算机名                   写输出$ UniqComps
                   }
                抓住{
                    写警告$ _。Exception.Message
                }
                最后{}
        }
        结束 {}
    }
    进口CSV \\ ComputerServicesList.csv。| MYTEST |英尺
我输入CSV是这样的:COMPNAME SVCNAME国家
本地主机Svc1运行
本地主机Svc2停止
COMP1 Svc1运行
COMP1 Svc2运行
COMP1 Svc3运行
COMP2 Svc1运行
COMP2 Svc3运行


解决方案

导入-CSV 这样做没有必要每SEI自定义函数返回一个数组。管道是直入组对象会给你你所需要的答案。

  $数据=进口的CSV。\\ ComputerServicesList.csv
$ uniqueComputerCount =($数据|排序对象 - 独特COMPNAME).Count之间
$数据|组对象SVCNAME |位置对象{$ _计数-eq $ uniqueComputerCount} |选择-ExpandProperty名称

这将返回所有的所有主机上present服务。它的情况下仅仅是 Svc1 。如果你想确保他们正在运行以及微小的改动将净相同的输出

  $数据|组对象SVCNAME,州|位置对象{$ _计数-eq $ uniqueComputerCount} |选择对象-ExpandProperty名称|的ForEach-对象{($ _ -split,)[0]}

有更多来自这一点,但我试图在忍受的纯阵列查询的组件的问题。你是否希望能处理每一行,并比较了一些符合变量你一起走?似乎是一种浪费,当PowerShell的处理它本身。

目前的功能是在同一时间获得的一行数据和单个项目被传递给组对象。就像我在我的评论说,你还必须符合您的变量 COMPNAME 不等于计算机名

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

解决方案

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

这篇关于PowerShell的:如何汇总数据阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆