Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do you pass arguments from Perl to Powershell?

by StangMan71 (Initiate)
on Nov 18, 2011 at 15:58 UTC ( [id://938872]=perlquestion: print w/replies, xml ) Need Help??

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

I'm running a Perl script that creates a WIN32 detached process. I'm trying to pass arguments to a Powershell script. However, Powershell is not picking up the arguments from the Perl script.
Powershell will pick up the arguments if I run the Powershell script from command line.
Any suggestions? See code snippet below.

Win32::Process::Create($process, $^X, 'C:\windows\system32\WindowsPowe +rShell\v1.0\powershell.exe -command "& D:\scripts\get-info.ps1 "."\'$ +val1\' "."\'$filename\'"', 0, DETACHED_PROCESS, ".");

Replies are listed 'Best First'.
Re: How do you pass arguments from Perl to Powershell?
by Util (Priest) on Nov 18, 2011 at 17:26 UTC

    First, basic debugging: Extract that long command string to a separate var, and print it.

    my $cmdline = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -command "& + D:\scripts\get-info.ps1 "."\'$val1\' "."\'$filename\'"'; print $cmdline, "\n"; Win32::Process::Create( $process, $^X, $cmdline, 0, DETACHED_PROCESS, ".", );
    You will see that you are not passing the string that you think. When you put a $var in single quotes, it does not get replaced with the value of $var (interpolation). Double-quoted strings *do* interpolate.

    Second, (as Anonymous Monk pointed out), $^X is the wrong thing to pass as second parameter; it would run another copy of Perl, instead of PowerShell. Read the docs, not just example code. The Win32::Process docs say that it is the "full path of the executable module", so in your case, it is probably the full path to PowerShell.

    When quoting is complex, it helps to build the string in stages, and sometimes to use q{...} and qq{...} in place of '...' and "...".

    Use 0 instead of DETACHED_PROCESS until everything else is working right, so you can better see the errors from PowerShell.

    The code below is untested for PowerShell, since I lack it on my Win2k box. I don't know exactly what kind of quoting it expects, or what that & does, but this should get you farther down the path:

    use strict; use warnings; use Win32::Process; my $val1 = 'dummy1'; my $filename = 'dummy2'; my $q_val1 = "'" . $val1 . "'"; my $q_filename = "'" . $filename . "'"; my $appname = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'; my $ps_command = join ' ', ( '&', 'D:\scripts\get-info.ps1', $q_val1, $q_filename, ) my $cmdline = qq{$appname -command "$ps_command"}; print $cmdline, "\n"; my $process; my $rc = Win32::Process::Create( $process, $appname, $cmdline, 0, 0, # replace with DETACHED_PROCESS later ".", ); if ( $rc == 0 ) { print Win32::FormatMessage( Win32::GetLastError() ); } else { print "Success!\n"; }

      Thanks so much for your reply. Your suggestions helped me resolve the problem.

Re: How do you pass arguments from Perl to Powershell?
by Anonymous Monk on Nov 18, 2011 at 16:12 UTC
    $^X is perl

      I've seen other examples for Win32::Process::Create commands. However, I admit that I don't understand the purpose of $^X. What is it doing exactly?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found