Why does PowerShell silently convert a string to an object [] along the length of the test?

Trustedhosts pukes on lines longer than 1023 characters, and I just parse this limit. I encoded a small small FILO queue to make sure that I can always add a new host when necessary, and only omit my least used servers when I found this weirdness. I cured him before I lost all my hair, so good. I do not understand why this is happening.

$namesString = 'server1.there.com,server2.there.com,server3.there.com'
Write-Host ("So, we have a nice little list of servers. Let trust them.")
Set-Item wsman:localhost\client\trustedhosts -Value $namesString -Force 
Write-Host ("They're nicely trusted now")
if ($namesString.length -gt 1) {
    Write-Host ("Now we know it longish")
}
Write-Host ("OK. Let try trusting the same string.")
Write-Host ("(By the way, it a $($namesString.getType()))")
Write-Host ("(and let check whether it an array: $($namesString -is [array]))")
Set-Item wsman:localhost\client\trustedhosts -Value $namesString -Force 
Write-Host ("Why did testing the length of the string cause this confirmed and tested string to report as an object array?")

Next release results

So, we have a nice little list of servers. Let trust them.
They're nicely trusted now
Now we know it longish
OK. Let try trusting the same string.
(By the way, it a string)
(and let check whether it an array: False)
Set-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by the parameter. Specified method is not sup
ported.
At C:\Users\...\AppData\Local\Temp\2\07da58ce-2578-4603-8291-83e1cc231522.ps1:11 char:9
+ Set-Item <<<<  wsman:localhost\client\trustedhosts -Value $namesString -Force 
    + CategoryInfo          : NotSpecified: (:) [Set-Item], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetItemCommand
Why did testing the length of the string cause this confirmed and tested string to report as an object array?

2000 , 3 . , wsman . , , , , , - -.

+3
2

PowerShell V1 V2, , PowerShell PSObject, .

PSObject , , , PSObject , PSO.

, :

$value = @{ Property = 1 }    # $value is a hashtable

$value.Property    # $value is now a PSObject wrapping the hashtable

$value.Property    # don't need to create another PSObject

# Under the covers, PowerShell V2 creates a PSObject wrapping the
# string before getting the length, but that PSObject isn't saved anywhere
"hello".Length

PowerShell V3 , .

+4

PowerShell v4, , , , , .

PowerShell v2, , , .

, , Length. ....

PSObject PowerShell string, BaseObject psobject; string ( ). , a string ( , ) , . , :

set-item WSMAN:\localhost\client\TrustedHosts $str.psobject.BaseObject -Force
+1

All Articles