[英]Why does foreach work here when select fails?
I've stored a list of attachments from my RavenDB server in the $Response
variable. 我在
$Response
变量中存储了RavenDB服务器中的附件列表。
[{"Size":3040,"Key":"attachments/deployments-7928/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0D0"},{"Size":2524,"Key":"attachments/deployments-7927/output-log","Metadata":{
"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0CF"},{"Size":530,"Key":"attachments/tasks-7842/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0AA"}]
I want to get the Key value so I can fetch the attachments. 我想获取Key值,以便我可以获取附件。
Why can't I select the Key value like this? 为什么我不能像这样选择Key值?
$Response | ConvertFrom-Json | Select Key
All the keys look empty when I do that. 当我这样做时,所有的键看起来都是空的。
Bizarrely, it works when I use Foreach instead. 奇怪的是,它在我使用Foreach时起作用。
$Response | ConvertFrom-Json | Foreach { $_.Key }
I see this: 我看到了这个:
attachments/deployments-7928/output-log
attachments/deployments-7927/output-log
attachments/tasks-7842/output-log
What's the difference? 有什么不同?
Why doesn't Select work here? 为什么不在这里选择工作?
It works if you assign the results to a variable first, and then pass that into Select-Object
. 如果首先将结果分配给变量,然后将其传递给
Select-Object
,则它可以工作。
$response = @'
[{"Size":3040,"Key":"attachments/deployments-7928/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0D0"},{"Size":2524,"Key":"attachments/deployments-7927/output-log","Metadata":{
"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0CF"},{"Size":530,"Key":"attachments/tasks-7842/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0AA"}]
'@
$ObjectList = $response | ConvertFrom-Json;
$ObjectList | Select-Object -Property Key;
Because convertfrom-json does not return array of PSObjects, it returns an array system.object[]. 因为convertfrom-json不返回PSObjects数组,所以它返回一个数组system.object []。 The array does not have a 'Key' member.
该数组没有“Key”成员。
Write-Output '[{"Size":3040,"Key":"attachments/deployments-7928/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0D0"},{"Size":2524,"Key":"attachments/deployments-7927/output-log","Metadata":{
"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0CF"},{"Size":530,"Key":"attachments/tasks-7842/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0AA"}]' `
| convertfrom-json | get-member
Output 产量
TypeName: System.Object[]
...
See, it only has the following members : 看,它只有以下成员:
Count 计数
Add 加
Address 地址
Clear 明确
Clone 克隆
CompareTo 相比于
Contains 包含
CopyTo 复制到
Equals 等于
Get 得到
GetEnumerator 的GetEnumerator
GetHashCode GetHashCode的
GetLength 对GetLength
GetLongLength GetLongLength
GetLowerBound GetLowerBound
GetType 的GetType
GetUpperBound GetUpperBound
GetValue 的GetValue
IndexOf 指数
Initialize 初始化
Insert 插入
Remove 去掉
RemoveAt RemoveAt移除
Set 组
SetValue 设定值
ToString 的ToString
Item 项目
IsFixedSize IsFixedSize
IsReadOnly IsReadOnly
IsSynchronized IsSynchronized
Length 长度
LongLength LongLength
Rank 秩
SyncRoot SyncRoot上
Assigning to a variable converts to TypeName: System.Management.Automation.PSCustomObject
and this has a 'key' member. 分配给变量会转换为TypeName:
System.Management.Automation.PSCustomObject
并且它具有“键”成员。 Powershell's built-in enumeration awareness expands this properly. Powershell的内置枚举意识正确地扩展了这一点。
如果要避免中间变量赋值:
{$response | ConvertFrom-Json}.invokereturnasis() | select key
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.