0

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/

Thufir
  • 8,216
  • 28
  • 125
  • 273

1 Answers1

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()

enter image description here

(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

enter image description here

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

enter image description here

References

Get-Member

.GetType()

Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39