![](/img/trans.png)
[英]Powershell ConvertFrom-Json Loop Through Multiple Files to Output to CSV
[英]PowerShell: ConvertFrom-Json to export multiple objects to csv
正如您可能從標題中理解的那樣,我是PowerShell的新手,即使試圖描述我的問題也很難。 因此,請原諒我的術語。
情境
我正在使用PowerShell查詢Office 365的審核日志Search-UnifiedAuditLog
返回“多組對象”(?),其中之一具有其他對象(?)的數組。 如果我理解正確,則輸出為JSON。
這是返回的示例(我將其稱為“一組對象”):
RunspaceId : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
RecordType : AzureActiveDirectoryStsLogon
CreationDate : 21/02/2017 12:05:23
UserIds : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Operations : UserLoggedIn
AuditData : {"CreationTime":"2017-02-21T12:05:23","Id":"{"ID":"00000000000000000","Type":3}],"ActorContextId":"xxxxxxxxxxxxxxxxxxxxxxxxx","ActorIpAddress":"xxxxxxxxxxxxx","InterSystemsId":"xxxxxxxxxxxxxxxxx","IntraSystemId":"000000000000-000000-000","Target":[{"ID":"00-0000-0000-c000-000000000000","Type":0}],"TargetContextId":"xxxxxxxxxxxxxxxxxx","ApplicationId":"xxxxxxxxxxxxxxxxxxxxxxxxxx"}
ResultIndex : 1
ResultCount : 16
Identity : xxxxxxxxxxxxxxxxxxxxxxxxxxx
IsValid : True
ObjectState : Unchanged
現在,我希望將AuditData
行的某些內容導出到csv(通常包含的數據比此處復制的要多得多)。 對於一組“對象集”(如上面的對象),這很好用。 為此,我使用:
$LogOutput = Search-UnifiedAuditLog -StartDate 2/20/2017 -EndDate 2/23/2017 -ResultSize 1
$ConvertedOutput = ConvertFrom-Json -InputObject $LogOutput.AuditData
$ConvertedOutput | Select-Object CreationTime,UserId,ClientIP | Export-Csv -Path "C:\users\some.user\desktop\users.csv
ResultSize
返回1,而不是多個“對象集”。 如果刪除ResultSize
則ConvertFrom-Json
無法正常工作。
所以問題是:
我可以遍歷所有 “對象集”並從json轉換,並在CSV上逐行導出嗎? 結果是這樣的:
UserId,Activity,UserIP
this@user.com, loggedIn, 10.10.10.10
that@user.org, accessedFile, 11.11.11.11
教育學的答案將非常非常感激。 非常感謝!
嘗試使用Search-UnifiedAuditLog <args> | Select-Object -ExpandProperty AuditData | ConvertFrom-Json
而不是-ResultSize
Search-UnifiedAuditLog <args> | Select-Object -ExpandProperty AuditData | ConvertFrom-Json
Search-UnifiedAuditLog <args> | Select-Object -ExpandProperty AuditData | ConvertFrom-Json
這將僅將AuditData
屬性轉發到ConvertFrom-Json
並忽略Search-UnifiedAuditLog
的其余對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.