VMConnect script [SOLVED]

by bahusdivus at 2013-02-27 11:18:00

Good day.
First, I would like to apologize for my bad English and for ?yrillic in quotes.
I try to use script from blog of Adam Driscoll. This script add smart resize to Hyper-V VMConnect window (like whith RDP window).
But when I run this script I got this error:
New-Object : Exception when calling “.ctor” with “1? argument: “Creating an instance of the control ActiveX” d2ea46a
7-c2bf-426b-af24-e19c44456399 “impossible: the current thread is not in single-threaded container.”
C:\Users???\Desktop\VMConnect.ps1:61 ???:37
+ $InteractiveSessionForm = New-Object <<<< Microsoft.Virtualization.Client.InteractiveSession.InteractiveSessionForm
-ArgumentList $RdpConnectionInfo
+ CategoryInfo : InvalidOperation: (:slight_smile: [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

As I understand, this line:
$RdpConnectionInfo = New-Object Microsoft.Virtualization.Client.InteractiveSession.RdpConnectionInfo -ArgumentList $Server,$VM,$TS.ListenerPort
not return object, but error:
Exception when calling “.ctor” with “1? argument: “Creating an instance of the control ActiveX” d2ea46a7-c2bf-426b-af24-e19c44456399 “impossible: the current thread is not in single-threaded container.”
Script running in RDP-session on Win 2008 R2.
Can It be fixed somehow? Can’t manage It by myself =(
Thank you.

EDITED: Cut full script text.
by adamdriscoll at 2013-02-27 12:17:15
Hello!

Can you check the status of your runspace?

$host.runspace

PS C:\Users\adriscoll> $host.runspace


Events : System.Management.Automation.PSLocalEventManager
ThreadOptions : ReuseThread
JobManager : System.Management.Automation.JobManager
RunspaceConfiguration :
InitialSessionState : System.Management.Automation.Runspaces.InitialSessionState
Version : 3.0
RunspaceStateInfo : Opened
RunspaceAvailability : Busy
ConnectionInfo :
OriginalConnectionInfo :
LanguageMode : FullLanguage
ApartmentState : STA
InstanceId : c5fb1929-aa0c-435f-9085-054d032545f8
SessionStateProxy : System.Management.Automation.Runspaces.SessionStateProxy
Debugger : System.Management.Automation.Debugger

This script requires STA threading mode you and may be running MTA. You can run powershell as STA using the following command.

powershell.exe -sta

I believe that PowerShell 3 runs in this apartment state by default.

Adam
by bahusdivus at 2013-02-28 03:19:40
Em… Sorry to trouble. It was my fault.
I run this scrip in powershell v2. After update to v3 script work as it should.
Thanks for you attention.