Powershell:引用包含空格的属性

发布于 2024-10-15 02:54:31 字数 458 浏览 2 评论 0原文

我正在使用 powershell 导入 CSV 文件。

问题是,其中一栏的标题是“剩余时间 - 小时”。 所以我得到了一系列对象,它实际上被分配了属性“剩余时间 - 小时”。

引用该属性的语法是什么?

例如

Import-Csv AllOpenCases.csv | % {$case = $_ }
$case | get-member

返回,

Category               : Inquiry
Starred                : No
Remaining Time - Hours : 22.5

但如果我输入,

$case.Remaining Time - Hours

我会得到“表达式或语句中出现意外的标记‘时间’”

I am importing a CSV file using powershell.

The problem is, the title of one of the columns is "Remaining Time - Hours".
So I'm getting a sequence of objects, and it's actually assigned the property "Remaining Time - Hours".

What is the syntax to refer to this property?

eg

Import-Csv AllOpenCases.csv | % {$case = $_ }
$case | get-member

returns

Category               : Inquiry
Starred                : No
Remaining Time - Hours : 22.5

but if I type

$case.Remaining Time - Hours

I get "Unexpected token 'Time' in expression or statement"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萝莉病 2024-10-22 02:54:31

引用带有嵌入空格的属性时必须用引号引起来:

 $case."Remaining Time - Hours"

Properties with embedded spaces have to be quoted when they're referenced:

 $case."Remaining Time - Hours"
我不咬妳我踢妳 2024-10-22 02:54:31

只是补充一下,属性名称本身可以是一个变量,例如:

PS> $o = new-object psobject -prop @{'first name' = 'John'; 'last name' = 'Doe'}
PS> $o

last name                                         first name
---------                                         ----------
Doe                                               John


PS> $prop = 'first name'
PS> $o.$prop
John

Just to add, the property name can itself be a variable e.g.:

PS> $o = new-object psobject -prop @{'first name' = 'John'; 'last name' = 'Doe'}
PS> $o

last name                                         first name
---------                                         ----------
Doe                                               John


PS> $prop = 'first name'
PS> $o.$prop
John
梨涡 2024-10-22 02:54:31

或者您也可以将该属性包装在 { } 中。像这样:

 $case.{Remaining Time - Hours}

Or you can also wrap that property in { }. Like this:

 $case.{Remaining Time - Hours}
莫相离 2024-10-22 02:54:31

FWIW,如果编码变得很痛苦,您可以添加一个别名属性:

 $caseprops = @"
 Category = Inquiry
 Starred = No
 Remaining Time - Hours = 22.5
 "@
 $case = new-object psobject -property ($caseprops | convertfrom-stringdata)

 $case | add-member aliasproperty "RT" "Remaining Time - Hours"

 $case | fl

 Remaining Time - Hours : 22.5
 Starred                : No
 Category               : Inquiry
 RT                     : 22.5

FWIW, if it gets to be pain to code with, you can add an alias property:

 $caseprops = @"
 Category = Inquiry
 Starred = No
 Remaining Time - Hours = 22.5
 "@
 $case = new-object psobject -property ($caseprops | convertfrom-stringdata)

 $case | add-member aliasproperty "RT" "Remaining Time - Hours"

 $case | fl

 Remaining Time - Hours : 22.5
 Starred                : No
 Category               : Inquiry
 RT                     : 22.5
当梦初醒 2024-10-22 02:54:31

我认为这已经很旧了,但是如果您需要访问其中包含空格的属性,您也可以这样做:

$case | Select-Object ("Remaining Time - Hours"), ("Another Property")

I think this is old, but you can also do this if you need to access properties with spaces in them:

$case | Select-Object ("Remaining Time - Hours"), ("Another Property")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文