Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Convert from c# to Perl

by alwaysuseperl (Novice)
on Aug 10, 2010 at 02:28 UTC ( #853949=perlquestion: print w/replies, xml ) Need Help??

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

Hey guys, so im trying to convert a simple c# script into a perl script. All it really does is use the Win32_process namespace to execute a command on a remote server, the below c# will execute notepad.exe on a remote server, just for testing. NOTE: I know psexec can do this, but id like to stay away from piping commands through that..
namespace WMISample { class Program { static void Main(string[] args) { string remoteMachine = "server"; ConnectionOptions connOptions = new ConnectionOptions( +); connOptions.Impersonation = ImpersonationLevel.Imperso +nate; connOptions.EnablePrivileges = true; ManagementScope manScope = new ManagementScope(String. +Format(@"\\{0}\ROOT\CIMV2", remoteMachine), connOptions); manScope.Connect(); ObjectGetOptions objectGetOptions = new ObjectGetOptio +ns(); ManagementPath managementPath = new ManagementPath("Wi +n32_Process"); ManagementClass processClass = new ManagementClass(man +Scope, managementPath, objectGetOptions); ManagementBaseObject inParams = processClass.GetMethod +Parameters("Create"); inParams["CommandLine"] = @"notepad.exe"; ManagementBaseObject outParams = processClass.InvokeMe +thod("Create", inParams, null); Console.WriteLine("Creation of the process returned: " + + outParams["returnValue"]); Console.WriteLine("Process ID: " + outParams["processI +d"]); Console.ReadLine(); } } }

Replies are listed 'Best First'.
Re: Convert from c# to Perl
by eyepopslikeamosquito (Chancellor) on Aug 10, 2010 at 02:57 UTC

    To start up a process on a remote machine, you might try using WMI. Here's an old test Perl script I wrote ages ago to do that:

    use Win32::OLE qw(in); use Win32::OLE::Variant; my $cmdline = "notepad"; my $machname = 'dev-autotest'; # This is the WMI moniker used to connect to CIM repository. my $class = "WinMgmts:{impersonationLevel=impersonate}!//$machname"; # Get the WMI interface. my $wmi = Win32::OLE->GetObject($class) or die "error: Get WMI $^E"; # Get a Win32_Process class object. my $proc = $wmi->Get('Win32_Process') or die "error: Get Win32_Process + $^E"; # Create a BYREF variant so a COM object can modify its value # and return it to us. my $vPid = Variant(VT_I4 | VT_BYREF, 0); # Create the new process if ($proc->Create($cmdline, undef, undef, $vPid) == 0) { print "process created ok, pid=$vPid\n"; } else { print "process create failed $^E\n"; }
      Thank you! Thats a lot easier than I thought. You know if its possible to get the output of the command? Like if I ran a netstat?
Re: Convert from c# to Perl
by repellent (Priest) on Aug 10, 2010 at 04:07 UTC
      NOTE: I know psexec can do this, but id like to stay away from piping commands through that..

    I'm curious - Why not?

      Because when you pipe commands through psexec, you cant parse through it. It throws it in a different format. Ive tried it a few times. for example..

      $var = `psexec \\server netstat -n`;

      shouldn't display what psexec does unless you print $var, but it does

      Thanks for the code! I was close, but never would have figured out the BYREF Variant values.

      Is there a way to display the output of this command?

        You're most likely referring to psexec's STDERR. Try capturing it:
        my $stderr; my $stdout = do { open(local *STDERR, ">", \$stderr) or die "Could not capture STDERR: $!"; qx(psexec \\\\server netstat -n); }; print "psexec stdout: $stdout\n"; print "-" x 60, "\n"; print "psexec stderr: $stderr\n";

          Thanks for the code! I was close, but never would have figured out the BYREF Variant values.

        It was eyepopslikeamosquito, not me :-)
Re: Convert from c# to Perl
by Anonymous Monk on Aug 10, 2010 at 02:38 UTC
    Win32_process namespace to execute a command on a remote server

    So it uses chopsticks to fly airplanes?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-01-27 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?