【发布时间】:2020-10-13 19:10:45
【问题描述】:
我有一个 CSV(5k 行),它是通过具有此标题的软件格式化的:
groupname,id,name,email,active,created,Last seen in software A,Last seen in software B,Last seen in software C,Software A,Software B,Software C
下面的行看起来像这样:
test-group,userID123,John Doe,jdoe@company.com,Yes,18 May 2017,22 Jun 2020,22 Jun 2020,22 Jun 2020,No,Yes,Yes
我想知道,鉴于 CSV 的日期格式为“日、月名、年”,我该如何过滤数据。 例如:我想过滤我在过去 90 天内没有见过的人,并且结果只搜索“软件 A”是否为“是”。
我还想添加更多参数,例如查看用户是否有:-and (email -like "*company.com") 等。
我以前在 AD 上搜索过,但没有从 CSV 导入。
$daysnumber= Read-Host 'Days since login'
(get-date).adddays(-$daysnumber)
$data = import-csv "userlist.csv"
$data | foreach-object {
New-Object PSObject -prop @{
"Last Seen in Software A" = [DateTime]::Parse("$_.Last Seen in Software A")
}
}
---更新--- 我走上了另一条路。我已经替换了日期的格式,因此我不再需要处理月份名称。 但我在验证数据甚至在控制台上显示或输出到 CSV 文件时遇到问题..
#Reads the file and then change all the months name to numeric value for ease of acces and sorting.
$original_file = 'C:\SCRIPTS\data\export-users.csv'
$destination_file = 'C:\SCRIPTS\data\export-users-replaced.csv'
(Get-Content $original_file) | Foreach-Object {
$_ -replace 'Jan', '01' `
-replace 'Feb', '02' `
-replace 'Mar', '03' `
-replace 'Apr', '04' `
-replace 'May', '05' `
-replace 'Jun', '06' `
-replace 'Jul', '07' `
-replace 'Aug', '08' `
-replace 'Sep', '09' `
-replace 'Oct', '10' `
-replace 'Nov', '11' `
-replace 'Dec', '12' `
-replace 'Never logged in', '01 01 1990' `
} | Set-Content $destination_file
#properties = 'groupname','id','name','email','active','created','Last seen in Jira Service Desk','Last seen in Jira Software','Last seen in Confluence','Jira Service Desk','Jira Software','Confluence'
Import-Csv $destination_file | ForEach-Object {
$groupname += $_.groupname
$id += $_.id
$name += $_.name
$email += $_.email
$active += $_.active
#JSD (Jira service desk)
$lastjsd += $_."Last Seen in Jira Service Desk"
#JSW (Jira Software)
$lastjsw += $_."Last seen in Jira Software"
#JCF (Jira Confluence)
$lastjcf += $_."Last Seen in Confluence"
$licjsd += $_."Jira Service Desk"
$licjsw += $_."Jira Software"
$licjcf += $_.Confluence
}
# Write-Host $active
$daysnumber= Read-Host 'Cantidad de dias'
$emaildomain=Read-Host 'Dominio de mails. Escriba "@dominio.com"'
$days = (get-date).adddays(-$daysnumber).ToString('dd MM yyyy')
Write-Host "Si desea buscar licencias de Service Desk ingrese Service,
Si desea buscar licencias de Jira Software ingrese Software,
Si desea buscar licencias de Confluence ingrese Confluence"
$license= Read-Host " ( service / software / confluence ) "
switch ($license) {
service {Where-Object $lastjcf -LE $days | Format-List | Out-String | Export-Csv -Path "C:\SCRIPTS\jiratest.csv" -Encoding UTF8 -Append -NoTypeInformation
}
software { Write-Host "nope"}
confluence { Write-Host "no"}
}
# Select-Object email -eq *$emaildomain* -and 'Last seen in Confluence' -le $days
# -and $licjsd -eq "Yes" -and $active -Is "Yes"
希望有人可以指导我如何制作这个低谷!
谢谢。
【问题讨论】:
-
(get-date).adddays(-$daysnumber)-->$daysSince = (Get-Date).AddDays(-[int]$daysnumber).Date和[DateTime]::Parse("$_.Last Seen in Software A")-->[datetime]::ParseExact($_.'Last Seen in Software A', 'dd MMM yyyy', $null) -
@Theo !谢谢(你的)信息!我对代码没有运气,因为我有很多解析错误。我继续走另一条路。将在下面附上我的代码。
标签: powershell csv sorting parsing foreach