如何将 InputObject 上的 NoteProperties 访问到远程处理会话 [英] How to access NoteProperties on the InputObject to a remoting session
问题描述
我无法访问定义了 NoteProperties 的 PSObject
,这些对象通过 Invoke-Command
传递到远程会话.我为此目的构造一个对象的原因是我需要访问远程会话中的两个独立信息,但 PowerShell 仅提供单个 InputObject
.
I'm having difficulty accessing PSObject
s with NoteProperties defined, that are being passed through to a remote session via Invoke-Command
. The reason I am constructing an object for this purpose is that I need access to two separate pieces of information in the remote session, but PowerShell only provides for a single InputObject
.
我的代码如下所示:
$sessionInput = new-object -TypeName System.Object
$sessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty1 -Value @("some", "value")
$sessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty2 -Value @("some", "other", "value)
$session = New-PSSession -ComputerName my-computer -Credential (Get-Credential)
Invoke-Command -Session $session -InputObject $sessionInput {
$input #1
$input | Get-Member #2
$input.NoteProperty1 #3
$input.NoteProperty1 | Get-Member #4
}
对于上面的每一行,我都得到了以下输出.
I get the following output for each of the numbered lines above.
所以,首先我尝试了我常用的将变量转储到控制台的技术,这确认属性已通过预期值传递:
So, first of all I tried my usual technique of dumping the variable to the console, which confirmed that the properties were passed through with the expected value:
NoteProperty1 : {some, value}
NoteProperty2 : {some, other, value}
PSComputerName : my-computer
RunspaceId : d1f35c8b-f631-4caa-bae0-f7c0066bbd55
PSShowComputerName : True
2.<代码>$input |获取会员
这比之前的尝试产生了更多的信息:
2. $input | Get-Member
This generated slightly more information than the previous attempt:
TypeName: Deserialized.System.Object
WARNING: column "PSComputerName" does not fit into the display and was removed.
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString(), string ToString(string format, System.IFormatProvider formatProvider)
NoteProperty1 NoteProperty Deserialized.System.String[] NoteProperty1=some val...
NoteProperty2 NoteProperty Deserialized.System.String[] NoteProperty2=some oth...
请注意,最后两行清楚地表明远程会话接收到的对象上存在 NoteProperty.所有这些提到的 Deserialized
开始激起我的兴趣......
Note that the last two lines clearly show the NoteProperty is present on the object received by the remote session. All those mentions of Deserialized
are starting to pique my interest...
前两个运气不错,我开始尝试检查 NoteProperty1
本身.然而,这个属性的值似乎是空的;使用上面的行没有将任何内容写入控制台,$input.NoteProperty1 -eq $null
返回 True.
Having had some luck with the first two, I started trying to inspect NoteProperty1
itself. However, the value of this property appears to be null; nothing is written to the console using the line above, $input.NoteProperty1 -eq $null
returns True.
所以,最后,我尝试通过 Get-Member
直接检查 NoteProperty1
值,但失败并显示消息No object has been specified to theget-member cmdlet.",这与传递给 Get-Member 的空对象一致.
So, last of all, I tried to inspect the NoteProperty1
value directly via Get-Member
, which failed miserably with the message "No object has been specified to the get-member cmdlet.", which is consistent with a null object being passed to Get-Member.
所以,我现在完全不知所措:这个薛定谔财产既存在又不存在;两者都有期望值,也没有值.我想我可以用稍微不同的方式来编写它,从而消除对 NoteProperties 的需求,但我很想知道这里发生了什么!
So, I'm completely at a loss now: this Schrödinger property both exists and does not; both has the expected value, and has no value. I think I can write this in a slightly different way that will eliminate the need for the NoteProperties, but I would love to know what's going on here!
推荐答案
选择对象即可获胜!:)
Select Object for the win! :)
$SessionInput = New-Object -TypeName System.Object
$SessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty1 -Value @("some", "value")
$SessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty2 -Value @("some", "other", "value")
$Session = New-PSSession -ComputerName 127.0.0.1
Invoke-Command -Session $Session -InputObject $SessionInput {
[array]$NoteProperty1 = $Input | Select -Expand NoteProperty1
for ($i = 0; $i -lt $NoteProperty1.Count; $i ++)
{
Write-Host -ForeGroundColor "Magenta" $i
$NoteProperty1[$i]
}
}
对于字符串变量,您仍然需要像这样在 select 语句中扩展字符串:
For String variables you still need to expand the string in the select statement like this:
$SessionInput = New-Object -TypeName System.Object
$SessionInput | Add-Member -MemberType NoteProperty -Name NoteProperty1 -Value "SomeValue"
$Session = New-PSSession -ComputerName 127.0.0.1
Invoke-Command -Session $Session -InputObject $SessionInput {
$NoteProperty1 = $Input | Select -Expand NoteProperty1
$NoteProperty1
}
这篇关于如何将 InputObject 上的 NoteProperties 访问到远程处理会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!