How to access NoteProperties on the InputObject to a remoting session

六眼飞鱼酱① 提交于 2019-12-05 11:27:29

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]
    }
}

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!