我有一个具有大量属性的对象。我想返回其中一些属性,其名称可能并不总是一致的。我想排除具有或包含特定值的属性。
$notneeded = @('array of properties that I do not wish to select')
$csvPath = "$Log\$Summary"
$csvData = Get-Content -Path $csvPath | Select-Object -Skip 1 | Out-String | ConvertFrom-Csv #the first line is extra (not a header), needs skipped
$csvData | Select-Object -Property * -ExcludeProperty $notneeded
如果要排除的属性列表是静态的,那么我可以使用它。但是我想从包含特定值的视图中排除属性。
答案 0 :(得分:0)
输入 CSV
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
脚本
$csvData = Invoke-WebRequest -Uri "https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv" | ConvertFrom-Csv -Header "Name", "Surname", "Address", "City", "State", "Zip"
$particularValue = "*120*"
$notneeded = @()
$csvData | Foreach-Object { $notneeded += $_.PSObject.Properties | Where-Object Value -like $particularValue | Select-Object Name }
$notneeded = $notneeded | Select-Object -Unique -ExpandProperty Name
$csvData | Select-Object * -ExcludeProperty $notneeded | Format-Table
请注意,例如我想排除提到120的列。我也在脚本中命名了列
输出(请参阅缺少地址列)
Name Surname City State Zip
---- ------- ---- ----- ---
John Doe Riverside NJ 08075
Jack McGinnis Phila PA 09119
John "Da Man" Repici Riverside NJ 08075
Stephen Tyler SomeTown SD 91234
Blankman SomeTown SD 00298
Joan "the bone", Anne Jet Desert City CO 00123