how to iterate $TwitterStatuses as an array or as Objects?

你。 提交于 2020-01-25 07:57:06

问题


How can I iterate each $TwitterStatuses element/item/object?

(And, how do I inspect each "status"?)

Import-Module PSTwitterAPI


Set-TwitterOAuthSettings -ApiKey $env:ApiKey -ApiSecret $env:ApiSecret -AccessToken $env:AccessToken -AccessTokenSecret $env:AccessTokenSecret

#Get-TwitterUsers_Lookup -screen_name 'mkellerman'

$TwitterStatuses = Get-TwitterStatuses_UserTimeline -screen_name 'mkellerman'
#$TwitterStatuses = [array]Get-TwitterStatuses_UserTimeline -screen_name 'mkellerman'

Foreach ($status in $TwitterStatuses)
{
   Write-Host $status
}

I was reading what looked like a sort of "cast" to array usage, but not sure of the distinction from objects here.

see also:

https://devblogs.microsoft.com/scripting/basics-of-powershell-looping-foreach/


回答1:


Your query is good already. You don't need to cast to array. What you get from Get-TwitterStatuses_UserTimeline is already an array of results.

You can check which type is an object by calling the .GetType() method on them.

$TwitterStatuses.GetType()

(The Get-Type method is available for all objects)

You can inspect the members available using Get-Member on the object you want to inspect.

$TwitterStatuses | Get-Member

Then, in your foreach, you need to precise what you actually want to display.

Example

Foreach ($status in $TwitterStatuses)
{
   Write-Host $status.text
}

If only one of the property interest you, you can even get the results directly by accessing the property name . The collection will be iterated and results displayed in the console.

$TwitterStatuses.text

References

Get-Member

.GetType()



来源:https://stackoverflow.com/questions/59874039/how-to-iterate-twitterstatuses-as-an-array-or-as-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!