如何将$ TwitterStatuses迭代为数组或对象? [英] how to iterate $TwitterStatuses as an array or as Objects?

查看:83
本文介绍了如何将$ TwitterStatuses迭代为数组或对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何迭代每个$TwitterStatuses元素/项目/对象?

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.

另请参阅:

https://devblogs.microsoft.com/scripting/powershell循环foreach的基础/

推荐答案

您的查询已经很好.您不需要强制转换为数组. 从Get-TwitterStatuses_UserTimeline获得的结果已经是一个结果数组.

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.

可以通过在对象上调用.GetType()方法来检查对象是哪种类型.

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

$TwitterStatuses.GetType()

(Get-Type方法可用于所有对象)

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

您可以使用Get-Member在要检查的对象上检查可用的成员.

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

$TwitterStatuses | Get-Member

然后,在您的foreach中,您需要精确确定实际要显示的内容.

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

示例

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

参考

获取-成员

.GetType()

这篇关于如何将$ TwitterStatuses迭代为数组或对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆