【发布时间】:2015-03-17 20:37:05
【问题描述】:
我有一个自定义 POWERSHELL 函数,它需要接受来自 csv 文件(最好是另一个生成此 CSV 的 cmdlet)的数据数组,读取内容,然后根据比较生成一些所需的输出。比较部分按需要工作,但仅当我在函数内对 import-csv ComputerServicesList 行进行硬编码时。为了灵活性,我需要函数不接受这个作为参数。以下是我目前的功能。在这一点上,我不知道该尝试什么。
function myTest3 {
[cmdletBinding()]
param (
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage="Input a CSV vile with at least the follwoing cols: ComputerName, ServiceName, State")]
[array]$InputData
)
#$data = $InputData
$report= @()
$counter = 0
foreach ($line in $InputData) {
$tempreport = New-Object PSObject
$tempreport | Add-Member -type NoteProperty -name ComputerName -value $line.computerName
$tempreport | Add-Member -type NoteProperty -name ExitCode -value $line.ExitCode
$tempreport | Add-Member -type NoteProperty -name Name -value $line.ServiceName
$tempreport | Add-Member -type NoteProperty -name ProcessId -value $line.ProcessId
$tempreport | Add-Member -type NoteProperty -name StartMode -value $line.StartMode
$tempreport | Add-Member -type NoteProperty -name State -value $line.State
$tempreport | Add-Member -type NoteProperty -name Status -value $line.status
$counter = $counter + 1
write-host "Records Looped: $counter"
#Append
$report += $tempreport
}
write-output $report | ft
# I WANT TO GET RID OF THIS LINE
$data = import-csv .\ComputerServicesList.csv
#perform Computer Count
$uniqueComputerCount = ($data | Sort-Object ComputerName -Unique).Count
write-host "Uniq computers counted: $uniqueComputerCount"
$RunningServices = $data | where-object {$_.State -eq "Running"} | Select ComputerName, ServiceName
# List services that are shared.
$SvcsCollapsed = $RunningServices | Group-Object ServiceName | where {$_.count -eq $uniqueComputerCount} | select Name
write-output $SvcsCollapsed
}
import-csv .\ComputerServicesList.csv | myTest3 | ft
我的输入 CSV 如下所示:
计算机名称服务名称状态 本地主机 Svc1 正在运行 本地主机 Svc2 已停止 Comp1 Svc1 正在运行 Comp1 Svc2 正在运行 Comp1 Svc3 正在运行 Comp2 Svc1 正在运行 Comp2 Svc3 正在运行【问题讨论】:
-
这看起来和你上一个问题几乎一样。如果您觉得没有正确处理,您应该返回并编辑您之前的内容
-
我真的很想知道你为什么坚持这样做?管道将一次处理这些项目。你需要
Group-Object才能在整个实体上发挥它的魔力。
标签: powershell csv import