如何向对象添加名称注释属性?
如何为对象添加名称注释属性?我尝试过:
$a = "This", "Is", "a", "cat"
$a | Add-Member -type NoteProperty -name Name
$a
但这似乎不起作用。
预期输出是:
Name
----
This
Is
a
cat
How can add Name NoteProperty for an object? I tried:
$a = "This", "Is", "a", "cat"
$a | Add-Member -type NoteProperty -name Name
$a
but this doesn't seem to work.
The expected output is:
Name
----
This
Is
a
cat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是修改后的问题的答案:
根据要求,输出是
This is the answer to the amended question:
Output, as requested, is
以下示例展示了如何获取示例中 $a 中的每个值,将其转换为具有 Name 和 Value 属性的 PSObject 以及使用 Add-Member cmdlet。 ` 用于续行。由于正在管道中调用 Add-Member,因此使用 -passThru 属性来传递带有新成员的对象。
我将输出通过管道传输到 | ft -auto 收缩列以很好地适应这里。
回答更新问题的另一种方式:
$a | %{new-object psobject -p @{Name=$_}
预期输出匹配:
Here is an example of how to take your example each value in $a, convert it to a PSObject with a Name and Value properties as well as using the Add-Member cmdlet. The ` is for line continuation. Because the Add-Member is being called in a pipeline, the -passThru property was used to pass the object with the new member on.
I piped the output to | ft -auto to shrink the columns to fit here nicely.
Another way of answering the updated question:
$a | %{new-object psobject -p @{Name=$_}
Expected output matches: