我查看了其他答案,但我仍在掙扎。我有一些來自RESTAPI的資料,我使用Invoke-RestMethod獲得這些資料,所以它已經從回傳的JSON中轉換出來了
$scans = Invoke-RestMethod -URI "https://<url>/api/v1/<api key>/scans"
我得到一個有兩個欄位的物件
Message:
資料:
$Scans.Data包含一個從JSON輸出轉換的哈希表,表中的每個條目都有以下的鍵:值對
ScanID
用戶名
目標
名稱
這就是$scans | Get-Member
$scans | gm
型別名稱。System.Management.Automation.PSCustomObject
名稱成員型別定義
---- ---------- ----------
Equals 方法 bool Equals(System.Object obj)
GetHashCode方法 int GetHashCode()
GetType 方法型別 GetType()
ToString方法 string ToString()
name NoteProperty string name=<redacted>
scan_id NoteProperty string scan_id=<redacted>
targets NoteProperty Object[] targets=System.Object[] 。
user_name NoteProperty string user_name=<redacted>
我需要將所有資料匯出到CSV中,但是$scan.data.target只顯示為System.Object[]
我不能使用-expandproperty,因為我正在選擇多個欄位。
我如何將$scans.data.target轉換為一個可讀的形式用于匯出,同時保持它與該條目的其他欄位的聯系?
類似于:
$scans.data | export-CSV <filepath>/code>
但是,當我打開CSV時,要以一種可讀的格式匯出
uj5u.com熱心網友回復:
正如所評論的,你可以選擇在輸出中的目標的組合欄位:
# demo
$scan = [PsCustomObject]@{data = [PsCustomObject]@{name = 'blah'; scan_id = 123; targets = 'abc','def','ghi'; user_name = 'ItIsMe'}}。
$scan.data | Select-Object *, @{Name = 'target'; Expression = {$_. targets -join '; '}}。-ExcludeProperty targets |
Export-Csv -Path 'D:Testlah.csv' -NoTypeInformation
這(在控制臺中顯示時)看起來像這樣:
name scan_id user_name targets
---- ------- --------- -------
blah 123 ItIsMe abc; def; ghi
或者創建輸出,在CSV中每個目標都有自己的行:
$result = foreach ($item in $scan.data) {
foreach ($target in $item.target) {
[PsCustomObject]@{
Name = $item.name
ScanID = $item.scan_id
目標 = $target.
UserName = $item.user_name
}
}
}
# output on screen
$result[/span
# 輸出到CSV檔案 $result
$result | Export-Csv -Path 'D:Testlah.csv' /span> -NoTypeInformation
這樣你就可以得到:
Name ScanID Target UserName
---- ------ --------
blah 123 abc ItIsMe
blah 123 def ItIsMe
blah 123 ghi ItIsMe
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/309313.html
標籤:
上一篇:如何在AD中禁用組
