简体   繁体   English

Powershell 在条件下导入 csv 文件的前两列中的值

[英]Powershell to import values in first two columns of csv files on a condition


I have the following code which is importing csv data and exporting into an excel spreadsheet.我有以下代码导入 csv 数据并导出到 excel 电子表格中。
How do I export-excel, just the "Name" column values (first column), if the 'Status' column value (second column) is equal to "Active"?如果“状态”列值(第二列)等于“活动”,我如何导出 Excel,只有“名称”列值(第一列)?

Import-Csv -Path 'C:\Data\mergeSessions\CiscoAnyconnect.csv' | Sort-Object -Property 'Name' -Unique | Export-Excel -Path 'C:\Data\mergeSessions\reports\reportVCX01.xlsx' -WorkSheetname 'vpn'

And for the below code, how to I export-excel, just the value from the 'Associated User' column (first column)?对于下面的代码,我如何导出 Excel,只是来自“关联用户”列(第一列)的值?

Import-Csv C:\Data\mergeSessions\SessionSummary.csv | Sort-Object 'Associated User' -Unique | Export-Excel -Path C:\Data\mergeSessions\reports\reportVCX01.xlsx -WorksheetName 'citrix'

For the first part of the question you could do:对于问题的第一部分,您可以这样做:

Import-Csv -Path 'C:\Data\mergeSessions\CiscoAnyconnect.csv' |
Where-Object { $_.Status -eq 'Active' } |
Select-Object Name -Unique | Sort-Object |
Export-Excel -Path 'C:\Data\mergeSessions\reports\reportVCX01.xlsx' -WorkSheetname 'vpn'

The second part is easier because there is no condition:第二部分比较容易,因为没有条件:

Import-Csv -Path 'C:\Data\mergeSessions\SessionSummary.csv' |
Select-Object 'Associated User' -Unique | Sort-Object |
Export-Excel -Path 'C:\Data\mergeSessions\reports\reportVCX01.xlsx' -WorksheetName 'citrix'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM