So I'm working a bit more on my PowerShell class, trying to do some work on remote machines. I add the option to supply a host, username and password and want’s to connect using WSManConnectionInfo. I end up with a ton of weird errors but can’t really figure out what is making all those errors, so desperate as I am, I end up using New-PSSession and Enter-PSSession. ( I should have just sticked with following the error)
I still get errors but now from the Runspace saying its not supported. Digging around on Google I find out I need to implement IHostSupportsInteractiveSession to my PSHost. Well that wasn’t to hard, I add
and smack in
At the end of the class and I’m good to go. Still after this I’m still getting tons of errors, so I start walking back the stack seeing what I missed. Turns out I forgot some error handling in a function I use, that saved all variables in a runspace, and it is my enumeration of variables that fails.
System.InvalidCastException: Unable to cast object of type 'System.Management.Automation.PSCustomObject' to type 'System.Management.Automation.PSVariable'.
I’m doing something like this
I loop all the PSObjects from doing a get-variable so I inspect the PS object and; WTF???
The BaseObject is “kind of empty” not really sure what it is, but the main object is a variable, so no sweat, I smack in
and the result is a new exception
System.InvalidCastException: Unable to cast object of type 'System.Management.Automation.PSVariable' to type 'System.Management.Automation.PSVariable'.
LOL of all the world’s most lame error messages that has GOT to be the one.
Screw it. after playing around with a TON of different good/stupid/desperate/brilliant hacks I remember that I used SessionStateProxy.PSVariable.GetValue() when fetching single variables, so I try this approach instead, and it work’s .. Its ugly, I know, but what the hell am I/we suppose to do in a weird situation like this ?