我在名为“Exchange Mailboxes”的 csv 列中混合了数据(有些行为空)。
Exchange Mailboxes
Include:[<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b21242325652f242e0b243e3f2724242065282426" rel="noreferrer noopener nofollow">[email protected]</a>]
Include:[<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89c998e919cd68b95918c90b8978d8c94979793d69b9795" rel="noreferrer noopener nofollow">[email protected]</a>]
Include:[<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="244f41524d4a0a484b5241644b5150484b4b4f0a474b49" rel="noreferrer noopener nofollow">[email protected]</a>]
我尝试先检查该行是否为空。如果它是空的,那么只为这些行分配空字符串。如果特定的 csv 行不为空,则使用 EXOMailbox 获取其名称和 ID。
换句话来说,如何跳过空行?
我尝试了这样的方法,但由于某种原因它不起作用。
$importFile = Import-Csv -Path "C:\AuditLogSearch\Dis\Modified-Audit-Log-Records.csv"
foreach ($csvFile in $importFile){
if([string]::IsNullOrEmpty($csvFile.'Exchange Mailboxes')){
$userName = ""
$userId = ""
}else{
$exoMailbox = $csvFile.'Exchange Mailboxes'.Split([char[]]@('[', ']'))[1]
$exoUser = Get-EXOMailbox -Filter "PrimarySmtpAddress -eq '$exoMailbox'"
$userName = $exoUser.DisplayName
$userId = $exoUser.Identity
}
}
最佳答案
如果您想要排除具有空 Exchange Mailboxes
列值的行:
$importFile =
Import-Csv "C:\AuditLogSearch\Dis\Modified-Audit-Log-Records.csv" |
Where-Object 'Exchange Mailboxes' -ne ''
如果您想排除所有列都为空的行(这也适用于您的(非典型)单列 CSV):
$importFile =
Import-Csv "C:\AuditLogSearch\Dis\Modified-Audit-Log-Records.csv" |
Where-Object { -join $_.psobject.Properties.Value }
注意:
Import-Csv
将 CSV 文件的行解析为[pscustomobject]
实例,其属性始终以字符串形式反射(reflect) CSV 的列值。$_.psobject.Properties.Value
使用 intrinsic.psobject
property反射(reflect)对象的所有属性,.Properties.Value
返回所有属性值。-join
的一元形式运算符直接连接所有 - 根据定义字符串 - 值并将结果作为单个字符串返回。因此,如果所有列值均为空,则结果为空字符串 (
''
);否则,字符串非空。
Where-Object
,当给定 script block 时({ ... }
),隐式将其输出强制为[bool]
($true
或$false
);在 PowerShell 中,将任何非空字符串转换为[bool]
会产生$true
,而空字符串会产生$false
。
结合应用上述内容并从 Exchange Mailboxes
列值中提取电子邮件地址:
$emailAddresses =
Import-Csv "C:\AuditLogSearch\Dis\Modified-Audit-Log-Records.csv" |
Where-Object { -join $_.psobject.Properties.Value } |
ForEach-Object { ($_.'Exchange Mailboxes' -split '[][]')[1] }
使用示例输入,输出 $emailAddresses
然后产生:
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adc7c2c5c383c9c2c8edc2d8d9c1c2c2c683cec2c0" rel="noreferrer noopener nofollow">[email protected]</a>
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="533732253a377d203e3a273b133c26273f3c3c387d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7dcd2c1ded999dbd8c1d2f7d8c2c3dbd8d8dc99d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>
适用于我认为的原始代码的意图:
$customObjects =
Import-Csv "C:\AuditLogSearch\Dis\Modified-Audit-Log-Records.csv" |
Where-Object { -join $_.psobject.Properties.Value } |
ForEach-Object {
$exoMailbox = ($_.'Exchange Mailboxes' -split '[][]')[1]
$exoUser = Get-EXOMailbox -Filter "PrimarySmtpAddress -eq '$exoMailbox'"
# Construct and output a custom object with the properties of interest.
[pscustomobject] @{
UserName = $exoUser.DisplayName
UserId = $exoUser.Identity
}
}
关于powershell - 如何使用 PowerShell 检查 csv 列中的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72609427/