Using Classes in VirtualBox

I thought of creating some advance functions using Classes in PowerShell. The code of Source code of VirtualBox is actually written in c++. I created a new COM object and tried to access the method CreateMachine. but it is showing me a error.

 $x = New-Object -ComObject VirtualBox.VirtualBox
    $x.CreateMachine('Windows 8','Windows','c:\OS','Windows2008_64') 
     #Typical Syntax 

Its showing me this error

PS C:\> $x.CreateMachine('Windows 8','Windows','c:\OS','Windows2008_64')
Exception setting "CreateMachine": Cannot convert the "System.Object[]" value of type "Object[]" to type "array".
At line:1 char:1
+ $x.CreateMachine('Windows 8','Windows','c:\OS','Windows2008_64')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : RuntimeException   

And so I digged through the SDK and found this C++ code snippet for VirtualBox

virtual HRESULT STDMETHODCALLTYPE CreateMachine( 
            /* [in] */ BSTR aName,
            /* [in] */ BSTR aOsTypeId,
            /* [in] */ BSTR aBaseFolder,
            /* [in] */ BSTR aId,
            /* [in] */ BOOL aOverride,
            /* [retval][out] */ IMachine **aMachine) = 0;

And so a search in bing told me about BSTR data type and if I used that to create BSTR variables using this snippet,

BSTR $Name = SysAllocString("Windows 8");
BSTR $OSTypeID = SysAllocString("Windows");
BSTR $BaseFolder = SysAllocString("C:\$");
BSTR $ID = SysAllocString("Windows2008_64");

$x.CreateMachine($Name,$OSTypeID,$BaseFolder,$ID )

The error is

The variable '$Name' cannot be retrieved because it has not been set.
At line:1 char:6
+ BSTR $Name = SysAllocString("Windows 8");
+      ~~~~~
    + CategoryInfo          : InvalidOperation: (Name:String) [], RuntimeExcep 
   tion
    + FullyQualifiedErrorId : VariableIsUndefined
 
The variable '$OSTypeID' cannot be retrieved because it has not been set.
At line:2 char:6
+ BSTR $OSTypeID = SysAllocString("Windows");
+      ~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (OSTypeID:String) [], RuntimeE 
   xception
    + FullyQualifiedErrorId : VariableIsUndefined
 
The variable '$BaseFolder' cannot be retrieved because it has not been set.
At line:3 char:6
+ BSTR $BaseFolder = SysAllocString("C:\$");
+      ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (BaseFolder:String) [], Runtim 
   eException
    + FullyQualifiedErrorId : VariableIsUndefined
 
The variable '$ID' cannot be retrieved because it has not been set.
At line:4 char:6
+ BSTR $ID = SysAllocString("Windows2008_64");
+      ~~~
    + CategoryInfo          : InvalidOperation: (ID:String) [], RuntimeExcepti 
   on
    + FullyQualifiedErrorId : VariableIsUndefined
 

Could any one help me in creating a Machine from PowerShell.

I don’t know how you define SysAllocString but it can be

$Name = SysAllocString("Windows 8");
$OSTypeID = SysAllocString("Windows");
$BaseFolder = SysAllocString("C:\$");
$ID = SysAllocString("Windows2008_64");

$x.CreateMachine($Name,$OSTypeID,$BaseFolder,$ID )

or

[BSTR]$Name = SysAllocString("Windows 8");
[BSTR]$OSTypeID = SysAllocString("Windows");
[BSTR]$BaseFolder = SysAllocString("C:\$");
[BSTR]$ID = SysAllocString("Windows2008_64");

$x.CreateMachine($Name,$OSTypeID,$BaseFolder,$ID )

?