I'm have an csv file that look like this
Name SMTPAddress ExistHold
John [email protected] True
Hanah [email protected] True
David [email protected] False
and I'm just wondering how can I loop thru the list to see which users have ExistHold = True and get all those user SMTP address so that I can do the next process with only those users.
$Global:downloadFile = "C:\AuditLogSearch\Downloaded-Audit-Log-Records.csv"
function FunctionName {
$importDownload = Import-Csv -Path $Global:downloadFile
$importDownload | ForEach-Object{
}
}
I'm kinda stuck with how to loop so I would be really appreciated if I can get any help or suggestion.
CodePudding user response:
You can simply use a Where-Object clause to get these users and don't need a loop:
# use a Where-Object clause to filter only items where field 'ExistHold' is set to 'True'
$importDownload = Import-Csv -Path $Global:downloadFile | Where-Object { $_.ExistHold -eq 'True' }
# you now have an array of objects and to get just the SMTPAddress simply do
$importDownload.SMTPAddress
Import-Csv by default expects a comma as delimiter character. If your file uses something else to separate the fields, like a TAB, you need to specify that with parameter -Delimiter "`t"
CodePudding user response:
I would personally use the ForEach instead of ForEach object, sometimes it can be easier to work with. If you import the CSV with the correct delimiter, you should be able to filter on it like this
$Global:downloadFile = "C:\AuditLogSearch\Downloaded-Audit-Log-Records.csv"
function FunctionName {
$importDownload = Import-Csv -Path $Global:downloadFile
ForEach ($import in $importDownload) {
If ($import.ExistHold) {
Write-Output "Its true :)"
}
}
}
}
