簡體   English   中英

在PowerShell中訪問嵌套對象

[英]Accessing nested objects in PowerShell

我正在嘗試從PowerShell對象提取特定值以創建CSV以便在另一個應用程序中使用。 更具體地說,我使用Microsoft Graph API在設定的時間范圍內下載日歷中的事件。

我已經在PowerShell中使用此命令來獲取初始數據

$data = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/users/{username}/calendar/events?startDateTime=2019-01-01T00:00:00.0000000&endDateTime=2019-01-30T23:59:00.0000000&select=subject,start,end,IsAllDay,ResponseStatus"

從這里,我可以訪問$data變量中的每個項目,以獲得一個不錯的事件列表。

$data |
    ForEach-Object {$_.value} |
    Select -Property subject, start, end, IsAllDay, ResponseStatus

輸出:

subject        : Event today
start          : @{dateTime=2019-01-18T11:00:00.0000000; timeZone=GMT Standard Time}
end            : @{dateTime=2019-01-18T11:30:00.0000000; timeZone=GMT Standard Time}
isAllDay       : False
responseStatus : @{response=none; time=0001-01-01T00:00:00Z}

subject        : Event tomorrow
start          : @{dateTime=2019-01-18T09:45:00.0000000; timeZone=GMT Standard Time}
end            : @{dateTime=2019-01-18T12:15:00.0000000; timeZone=GMT Standard Time}
isAllDay       : False
responseStatus : @{response=none; time=0001-01-01T00:00:00Z}

我想要在startend對象中選擇dateTime數據,因此它看起來像這樣:

subject        : Event today
start          : 2019-01-18T11:00:00.0000000
end            : 2019-01-18T11:30:00.0000000
isAllDay       : False
responseStatus : @{response=none; time=0001-01-01T00:00:00Z}

subject        : Event tomorrow
start          : 2019-01-18T09:45:00.0000000
end            : 2019-01-18T12:15:00.0000000
isAllDay       : False
responseStatus : @{response=none; time=0001-01-01T00:00:00Z}

我可以在PowerShell窗口中使用以下命令執行此操作

$data.value.[0].start.dateTime

但是如何使用上面的select命令來做到這一點?

使用計算出的屬性來擴展嵌套對象的屬性:

$data |
    Select-Object -Expand value |
    Select-Object subject, @{n='start';e={$_.start.dateTime}},
        @{n='end';e={$_.end.dateTime}}, IsAllDay, ResponseStatus

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM