问题
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