
[英]Powershell conundrum: how to compare to CSVs and pull matching data from each?
[英]PowerShell merging data from multiple CSVs
我有一堆 CSV 文件,其中包含有关 DHCP 范围的信息(100 多个文件,每个服务器一个)。 我希望将所有数据合并到一个新表中。 有一些方法可以合并 CSV,但这还不够好,因为一个最重要的信息没有包含在单独的 CSV 文件中……服务器名称。 服务器名称来自文件名本身。 因此,构建新表的方法。
我的问题是 Su.netMask、StartRange、EndRange、ScopeID 和 State 的属性在同一“单元格”中为 output。 我设法为 ScopeName 做到了这一点(即设法每行获得一个 ScopeName),但不确定如何处理 rest。
$outputTable = $null
$files = Get-ChildItem -Path "C:\Temp\Discovery\" -Filter *Scopes.csv
Write-Host $files
ForEach($file in $files)
{
$scopeFile = Import-Csv $file
ForEach ($scopeName in $scopeFile.Name)
{
$outputTable += @(
[pscustomobject]@{
` DHCPServer=$file.Name.ToString().Split(".")[0];
` ScopeName=$scopeName;
` SubnetMask=$scopeFile.SubnetMask;
` StartRange=$scopeFile.StartRange;
` EndRange=$scopeFile.EndRange;
` ScopeID=$scopeFile.ScopeID
` State=$scopeFile.State
}
)
}
}
$outputTable | ft
当前output:
预计 output:
DHCPServerName,ScopeName,SubnetMask,StartRange,EndRange,ScopeID,State
server1,london,255.255.0.0,192.168.0.1,192.168.0.254,192.168.0.1,Active
server1,london,255.255.0.0,192.168.1.1,192.168.0.254,192.168.1.1,Active
server1,london,255.255.0.0,192.168.1.1,192.168.0.254,192.168.1.1,NotActive
您可以使用Select-Object
的计算属性将文件的Name
添加为 Csv 的新列:
$merged = foreach($file in Get-ChildItem -Path C:\Temp\Discovery\ -Filter *Scopes.csv)
{
Import-Csv $file.FullName | Select-Object @{
Name = 'DHCPServerName'
Expression = { $file.Name.Split(".")[0] }
}, *
}
$merged | Format-Table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.