访问名称为 space 的 powershell 属性
Access powershell property with space in name
我有以下内容:
$test = Test-CsWebScheduler -TargetFqdn "pool.int.contoso.com"
Write-Output $test
Target Fqdn : pool.int.contoso.com
Target Uri : https://pool.int.contoso.com/Scheduler/
Result : Failure
Latency : 00:00:00
Error Message : Scheduling conference at
https://pool.int.contoso.com/Scheduler/Handler/WebSchedulerHandler.ashx failed
with status Failure.
Diagnosis :
现在的问题是:
如何访问 "Error Message"、"Target Uri" 或 "Target Fqdn"?我可以通过以下方式访问结果:
Write-Output $test.result
Failure
没有问题,但由于其他人在里面有一个 space,我还没有找到访问它们的方法。我尝试了以下(找到 here):
Write-Output $test."Target Uri"
或
Write-Output $test.{Target Uri}
我还尝试了以下方法:
$test | Select-Object -Property "Target Uri"
或
Write-Output ($test."Target Uri")
或者非常简单的:
$test."Target Uri"
但这不起作用,我没有得到返回值(也没有错误)。我收到的唯一错误消息是当我使用时:
$test | select -ExpandProperty "Target Uri"
select : Property "Target Uri" cannot be found.
At line:1 char:9
+ $test | select -ExpandProperty "Target Uri"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Rtc.S...s.WebTaskOutput:PSObject) [Selec
t-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCom
mand
有没有人知道我如何访问它(无需将其转储到临时文件并解析它)?
更新 01:
此处要求的是 $test | 的输出通用汽车
$test | gm
TypeName: Microsoft.Rtc.SyntheticTransactions.WebTaskOutput
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Diagnosis Property string Diagnosis {get;}
Error Property string Error {get;}
Latency Property timespan Latency {get;}
Result Property Microsoft.Rtc.SyntheticTransactions.ResultStatus Result {get;}
TargetFqdn Property string TargetFqdn {get;}
TargetUri Property string TargetUri {get;}
WorkflowLogger Property Microsoft.Rtc.SyntheticTransactions.Logging.SyntheticTransactionsWorkf...
如将 $test 变量通过管道传递给 get-member 时所见
$test | gm
提供的输出显示目标 URI 属性 实际上是一个词,因此只需在脚本中使用以下内容即可。
$test.TargetUri
如评论中所述:使用输出中看到的 "names" 并不意味着它们与真实的 属性 名称相同。但是,使用 Get-Member (or the short abbreviation GM) 可以检查存储在变量中的真实 属性 名称。
所以使用
$test | gm
或
$test | Get-Member
表明 "Target Uri" 是 "TargetUri":
[...]
TargetUri Property string TargetUri {get;}
[...]
所以使用:
$test.TargetUri
在这里解决了这个问题。
尝试访问这样的值:
$test.PSObject.Properties["Target Uri"].Value
我有以下内容:
$test = Test-CsWebScheduler -TargetFqdn "pool.int.contoso.com"
Write-Output $test
Target Fqdn : pool.int.contoso.com
Target Uri : https://pool.int.contoso.com/Scheduler/
Result : Failure
Latency : 00:00:00
Error Message : Scheduling conference at
https://pool.int.contoso.com/Scheduler/Handler/WebSchedulerHandler.ashx failed
with status Failure.
Diagnosis :
现在的问题是:
如何访问 "Error Message"、"Target Uri" 或 "Target Fqdn"?我可以通过以下方式访问结果:
Write-Output $test.result
Failure
没有问题,但由于其他人在里面有一个 space,我还没有找到访问它们的方法。我尝试了以下(找到 here):
Write-Output $test."Target Uri"
或
Write-Output $test.{Target Uri}
我还尝试了以下方法:
$test | Select-Object -Property "Target Uri"
或
Write-Output ($test."Target Uri")
或者非常简单的:
$test."Target Uri"
但这不起作用,我没有得到返回值(也没有错误)。我收到的唯一错误消息是当我使用时:
$test | select -ExpandProperty "Target Uri"
select : Property "Target Uri" cannot be found.
At line:1 char:9
+ $test | select -ExpandProperty "Target Uri"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Rtc.S...s.WebTaskOutput:PSObject) [Selec
t-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCom
mand
有没有人知道我如何访问它(无需将其转储到临时文件并解析它)?
更新 01:
此处要求的是 $test | 的输出通用汽车
$test | gm
TypeName: Microsoft.Rtc.SyntheticTransactions.WebTaskOutput
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Diagnosis Property string Diagnosis {get;}
Error Property string Error {get;}
Latency Property timespan Latency {get;}
Result Property Microsoft.Rtc.SyntheticTransactions.ResultStatus Result {get;}
TargetFqdn Property string TargetFqdn {get;}
TargetUri Property string TargetUri {get;}
WorkflowLogger Property Microsoft.Rtc.SyntheticTransactions.Logging.SyntheticTransactionsWorkf...
如将 $test 变量通过管道传递给 get-member 时所见
$test | gm
提供的输出显示目标 URI 属性 实际上是一个词,因此只需在脚本中使用以下内容即可。
$test.TargetUri
如评论中所述:使用输出中看到的 "names" 并不意味着它们与真实的 属性 名称相同。但是,使用 Get-Member (or the short abbreviation GM) 可以检查存储在变量中的真实 属性 名称。
所以使用
$test | gm
或
$test | Get-Member
表明 "Target Uri" 是 "TargetUri":
[...]
TargetUri Property string TargetUri {get;}
[...]
所以使用:
$test.TargetUri
在这里解决了这个问题。
尝试访问这样的值:
$test.PSObject.Properties["Target Uri"].Value