NetWallah has asked for the wisdom of the Perl Monks concerning the following question:

Revered Monks ..
I'm trying to convert some clunky, but functional VB code to perl, but running into trouble with the perl version (Snippet):
use strict; use warnings; use Win32::OLE qw(in with valof) ; use constant RUNNING=>5; my @VMList; #Connect to Virtual Server my $VirtualServer = Win32::OLE->new('VirtualServer.Application' ) or die "Oops, cannot Connect to Host Virtual server\n" & Win32 +::OLE->LastError(); print "Name: " . valof $VirtualServer->Name() . "\n"; foreach my $vm (in $VirtualServer->VirtualMachines) { print " Name: " . valof $vm->Name . " State: " . valof $vm->Sta +te . "\n"; }
The error I get is:
Win32::OLE(0.1403) error 0x80070542: "Either a required impersonation +level was not provided, or the provided impersonation level is invalid" in METHOD/PROPERTYGET "Name" at d:\Scripts\BackupVM.pl line 14 Use of uninitialized value in concatenation (.) or string at d:\Script +s\BackupVM .pl line 14. Name: Win32::OLE(0.1403) error 0x80070542: "Either a required impersonation +level was not provided, or the provided impersonation level is invalid" in METHOD/PROPERTYGET "VirtualMachines" at d:\Scripts\BackupVM.pl +line 16 Can't call method "Name" on an undefined value at d:\Scripts\BackupVM. +pl line 18 .
(First Line number corresponds to the first (print "Name...)) statement.

I know there is no security or user rights problem because
a) The equivalent VB code runs fine
b) I'm running as the godly domain admin

Here is the relevent snippet of (ugh!) VB:

Option Explicit On Error Resume Next Dim op,who,I,virtualServer Dim vmachine(50) Dim objvmColl,objVM Const RUNNING=5 Const ForReading=1,ForWriting=2 'Connect to Virtual Server Set virtualServer = CreateObject("VirtualServer.Application") op=WScript.Arguments(0) Who=WScript.Arguments(1) I=0 if op = "list" then Wscript.Echo "Name: " & virtualServer.Name Set objVMColl = virtualServer.VirtualMachines If objVMColl.Count = 0 Then Wscript.Echo " Virtual machines: [none]" Else Wscript.Echo "Virtual machines: " & objVMColl.Count For Each objVM in objVMColl VirtualMachineInfo(objVM) Wscript.Echo " Name: " & objVM.Name & " State: " & obj +VM.State Next End If end if wscript.echo I & " machines are RUNNING: " Redim preserve vmachine(I) Dim vm, fso, MyFile
Please help with insights ... TIA.

     "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein

Replies are listed 'Best First'.
Re: Win32 VirtualServer Automation prob: Perl Vs. VB
by NetWallah (Canon) on May 02, 2005 at 06:55 UTC
    For those who care how this was fially resolved ..

    I put a PerlScript (XML for Wsh) Wrapper around the code, and it works just fine.

    The wierd part is that the script now has a ".wsf" extension, and I need to start it by running "cscript.exe" instead of perl. But it now works fine!

         "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein