Why does PowerShell silently convert a string to an object[] on testing length? -


Pukes over 1023 characters more reliable, and I've just exposed through that range I have a good small filo Quote was queuing up to make sure that whenever I need it I can always add a new host and leave only the least used server when I get this inequality. Before I used to lose all my hair, before that I had fixed it, so it is good. I do not understand why this is happening

$ namesString = 'server1.there.com, server2.there.com, server3.there.com' write-host (" Therefore, we have a good list of servers. Let's believe them. "Set item wsman: local host \ client \ trusthosts -Value $ namesString Force type hosts (" they are now trusted now ") if ($ NamesString.length -gt 1) {Write host ("We now know that it is a long time"}} The host to write ("OK. Try typing trust. ") Write-host (" (By the way, this name is $ $ $ String.getType ()) "Write Host Set-item wsman: Local Host \ customer \ trusthosts -Value $ namesString Force Type Host ("Why Tested Length:" ("$ namesString -is [array])) (and let's check if this is an array)"
  As a result, We have a nice list of servers, let them trust them they are well trustworthy Now we know that it is very long. Programmed to try to rely on the same string. (By the way, this is a string) (And let's see that this is an array: Incorrect) Set-item: 'System.Object []' can not be changed in the 'System.String' type required by the parameter. The specified method is not supported on Celsius: \ Users \ ... \ AppData \ Local \ \ 2 \ 07da58ce-2578-4603-8291-83e1cc231522.ps1 Temperature: 11 four: 9 + set item  

That incompatibility was buried in 2000 line code and the question came under string 3 changes. At each point, the string described itself as a string, but wsman rejected it as an array. In the end, some serious binary searches were taken to ascertain that this length test was changed to any kind of crypto-array. In PowerShell V1 and V2, when you receive a property or invite a member,

Creating this PSObject was somewhat expensive, so if your object was a simple variable reference, then the PSObject variable was stored in that the later operations did not pay for overhead to create another psobb. Will go

If this is not clear, here are some examples:

  $ value = @ {property = 1} # $ value is a hashtable $ value.Property # $ value Now there is no PSObject wrapping a PSObject wrapping hashtable $ value.Property # does not need to create another PSObject # under cover, PowerShell V2 makes wrapping a PSObject # string before receiving the length, but PSObject is not saved anywhere "hello" . Significant changes have been made to improve PowerShell V3 and it was not necessary to get a better performance from the weird C     


Comments