我正在匯入一個 CSV 檔案并讀取一個看起來像這樣的列
Exchange Mailboxes
Include:[[email protected]]
Include:[[email protected]]
Include:[[email protected]]
我使用 Get-EXOMailbox 來獲取他們的 DisplayName 和 Id。之后,我試圖將它傳遞到我的新物件中,如下所示,以便我可以匯出它。我遇到的問題是當我查看我的 Excel 檔案時,它顯示System.Object[]在每一行上,而不是顯示每個實際的 DisplayName 和 Id。
任何有關如何正確顯示它的幫助將不勝感激。
$result = 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
}
}
New-Object PsObject -Property @{
'Searched User' = $result.UserName //I'm trying to pass here
'SharePoint URL' = $spUrl
'Searched User GMID' = $result.UserId //and here
'Site Owner' = $spositeOwner
User = $u.User
"Result Status" = $u."Result Status"
"Date & Time" = $u."Date & Time"
"Search Conditions" = $u."Search Conditions"
"SharePoint Sites" = $u."SharePoint Sites"
"Exchange Public Folders" = $u."Exchange Public Folders"
"Exchange Mailboxes" = $u."Exchange Mailboxes".Split([char[]]@('[', ']'))[1]
"Case Name" = $u."Case Name"
"Search Criteria" = $u."Search Criteria"
"Record Type" = $u."Record Type"
"Hold Name" = $u."Hold Name".Split(('\'))[1]
"Activity" = if ($null -ne ($importData | where-object { $_.Name -eq $u."Activity" }).Value) { ($importData | where-object { $_.Name -eq $u."Activity" }).Value }
else { $u."Activity" }
} | Select-object -Property User, "Date & Time", "Case Name", "Hold Name", "Record Type", "Activity" , "Searched User", "Searched User GMID", "SharePoint URL", "Exchange Mailboxes", "Exchange Public Folders" , "Search Criteria", "Result Status"
}
$xlsx = $result | Export-Excel @params
$ws = $xlsx.Workbook.Worksheets[$params.Worksheetname]
$ws.Dimension.Columns
$ws.Column(1).Width = 20
$ws.Column(2).Width = 20
$ws.Column(3).Width = 15
$ws.Column(4).Width = 15
$ws.Column(5).Width = 15
$ws.Column(6).Width = 160
$ws.View.ShowGridLines = $false
Close-ExcelPackage $xlsx
uj5u.com熱心網友回復:
$result是一個物件陣列,包含輸入 CSV 中每個非空行的物件;因此,向$result.UserName您正在創建的物件的屬性添加值New-Object也將是陣列,這解釋了您的癥狀(似乎Export-Excel, likeExport-Csv并沒有有意義地支持陣列值屬性并且只是使用它們的型別名稱,System.Object[]在匯出期間)。
聽起來最簡單的解決方案是直接在ForEach-Object呼叫中添加附加屬性,通過現有[pscustomobject]文字 ( [pscustomobject] @{ ... }) 構造和輸出的各個物件:
$result =
Import-Csv "C:\AuditLogSearch\Dis\Modified-Audit-Log-Records.csv" |
Where-Object { -join $_.psobject.Properties.Value } | # only non-empty rows
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
# === Add the additional properties here:
'Searched User' = $exoUser.UserName
'SharePoint URL' = $spUrl
'Searched User GMID' = $exoUser.UserId
'Site Owner' = $spositeOwner
# ...
}
}
筆記:
以上僅顯示了您問題中的一些屬性;根據需要添加(尚不清楚其中
$u一些來自何處。使用自定義物件文字 (
[pscustomobject] @{ ... }) 不僅比New-Object PSObject -Property @{ ... }[1]呼叫更容易和更有效,與后者不同的是,它隱式保留了屬性的定義順序,因此不需要額外的Select-Object呼叫來確保所需的順序屬性。
[1] 也許令人驚訝的是,PSObject( [psobject]) 和PSCustomObject( [pscustomobject]) 指的是同一型別,即System.Management.Automation.PSObject盡管存在單獨的System.Management.Automation.PSCustomObject,但自定義物件實體自報告為 ( ([pscustomobject] @{}).GetType().FullName) - 有關背景資訊,請參閱GitHub 問題 #4344。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490709.html
標籤:电源外壳 CSV 天蓝色的PowerShell powershell-4.0
下一篇:重置組織單位的密碼
