Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to write vbscript equivalent to perl

by shanthi (Initiate)
on Dec 28, 2005 at 13:58 UTC ( [id://519552]=perlquestion: print w/replies, xml ) Need Help??

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

I have written a program in vbscript for getting system usage information, but while tried the same in perl failed to get the data.

' run as cscript hello.vbs Dim strMessage strMessage = "Hello world in VB Script!" WScript.Echo strMessage strComputer = "." Set objWMIService = GetObject("winmgmts:" & _ "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colSettings = objWMIService.ExecQuery _ ("Select * from Win32_OperatingSystem") For Each objOperatingSystem in colSettings Wscript.Echo "Total Physical Memory: " & objOperatingSystem.TotalVi +sibleMemorySize Wscript.Echo "Free Physical Memory: " & objOperatingSystem.FreePhys +icalMemory Wscript.Echo "Total Virtual Memory Size: " & objOperatingSystem.Tot +alVirtualMemorySize Wscript.Echo "Free Virtual Memory Size: " & objOperatingSystem.Fre +eVirtualMemory Next 'also want to get percentage of CPU, Memory, VirtualMemory used

Here adding my perl script.

use strict; use Win32::OLE qw( in ); my $NameSpace = "root/cimv2"; my $Machine = shift @ARGV || "."; $Machine =~ s/^[\\\/]+//; print $Machine."\n"; my $WMIServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel + = impersonate}!//$Machine/$NameSpace" ) || die; # get TotalVisibleMemorySize, FreePhysicalMemory, TotalVirtualMemoryS +ize, FreeVirtualMemory print "Win32_OperatingSystem: "; my $memOS = $WMIServices->InstancesOf(Win32_OperatingSystem); printf "%s %ld %ld %ld %ld",$memOS->{Name}, $memOS->{TotalVisibleMemor +ySize}, $memOS->{FreePhysicalMemory}, $memOS->{TotalVirtualMemorySiz +e}, $memOS->{FreeVirtualMemory};

20051228 Janitored by Corion: Added formatting

Replies are listed 'Best First'.
Re: How to write vbscript equivalent to perl
by esskar (Deacon) on Dec 28, 2005 at 14:17 UTC
    here we are:
    use strict; use Win32::OLE qw( in ); use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; my $NameSpace = "root\\cimv2"; my $Machine = shift @ARGV || "."; $Machine =~ s/^[\\\/]+//; print $Machine. "\n"; my $WMIServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel = impersonate}!\\\\$Machine\\$NameSp +ace") || die; # get TotalVisibleMemorySize, FreePhysicalMemory, TotalVirtualMemoryS +ize, FreeVirtualMemory print "Win32_OperatingSystem: "; my $col = $WMIServices->ExecQuery( "SELECT * FROM Win32_OperatingSyste +m", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly ); foreach my $memOS (in $col) { printf "%s %ld %ld %ld %ld", $memOS->{Name}, $memOS->{TotalVisibleMemorySize}, $memOS->{FreePhysicalMemory}, $memOS->{TotalVirtualMemorySize}, $memOS->{FreeVirtualMemory}; }
Re: How to write vbscript equivalent to perl
by tcf03 (Deacon) on Dec 28, 2005 at 14:53 UTC
    ActiveState has a vbscript to perl converter. I have used it a time or two and it seems to work well, although im not really up to par on my vbscripting skills...

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
      This may be more of a meditation than a response, but would it be a violation of the license agreement, or unethical (if your employer actually owned the license) if someone were to post code that was converted by a licensed product such as ActiveStates vbscript converter?

      Ted
      --
      "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
        --Ralph Waldo Emerson

        As I understand ActiveState's License Agreement, the results of your usage of their tools (PerlDevKit) belong to you.

        as for the "unethical" part, I concider this borderline, where the main concern is in the (paid) work time spent on Perlmonks, opposed to doing real work... ;-)

        Huh?!?! I've bought ActiveState's Dev Kit. You suggesting I can't use it to answer a question on Perl Monks? And if I did, it would be unethical of me to use software that I've purchased with my own, hard-earnt money? Boy, I know that some people have a lax attitude to respecting intellectual property these days, but I think this is going a bit too far the other way!
        When I use any wizard in the visual studio - does that mean that the output code from that wizard belongs to M$?

        If I use a commercial templating solution - does the processed output of that template belong to the company that provided the template library?

        If I use an sql client to create a database and then use that same interface to dump my sql - does that interface own the sql or the sql dump?
Re: How to write vbscript equivalent to perl
by marto (Cardinal) on Dec 28, 2005 at 14:20 UTC
    Hi shanthi,

    By 'fails' do you mean that your script dies or does not print the data?
    I can't test this at the moment, however you may want to have a look at the example script List Operating System Properties from the Microsoft Perl scripts master index. You may find pointers towards achieving the rest of your goals within the scripts listed there.

    Hope this helps.

    Martin
Re: How to write vbscript equivalent to perl
by esskar (Deacon) on Dec 28, 2005 at 19:46 UTC
    M$ provides a tool called Scriptomatic that writes WMI scriptes for you - supported languages: Perl, Phyton,VBScript, JScript
Re: How to write vbscript equivalent to perl
by SamCG (Hermit) on Dec 29, 2005 at 15:36 UTC
    Simon, I believe tfc's meditation specified "if the employer actually owned the license" -- if your employer owns the Visual Studio license, for example, and you use a wizard within it to create code, it's the employer that would own the code, not M$.

    The same could be said of Scriptomatic, assuming it's the employer's license.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://519552]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-03-19 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found