Execute PowerShell script from Perl script

14,493

The single quote, double quote, and back quote characters all have different and specific meanings in Perl, and getting them confused will make your program behave in unexpected ways.

For text inside "double quotes", Perl interpolates variables and reinterprets characters preceded by a backslash

$foo = "bar";
print "\t$foo\n";       # outputs:  <TAB> bar <NEWLINE>

Inside 'single quotes', Perl does not interpolate variables, and treats a backslash as a literal backslash character unless it is followed by either another backslash or a single quote character. Use them to tell Perl to treat the text inside the quotes literally.

$foo = "bar";
print '\t$foo\n';       # outputs: <BACKSLASH> t <DOLLAR> foo <BACKSLASH> n

For text inside backticks (sorry, markup unavailable), Perl does two things. (1) reinterpret the string as if it were inside double quotes (interpolate variables, replace escaped character sequences), and (2) pass the resulting string to the operating system as a command to run, returning the output of the command

$foo = "bar";
print `ls /tmp/$foo 2>&1`;
# possible output:    ls: cannot access /tmp/bar: No such file or directory


Back to the original question: you wish to run the command

C:\Windows\System32\PowerShell\v1.0\powershell.exe -command "C:\LDIAD\PS\mailboxExists.ps1 $username"

on your operating system where "$username" is replaced by the string value of the Perl variable $username, and you want to assign the output of that command to the variable $result. Here are some ways to accomplish that:

  1. Use interpolating backquotes, escaping \

    $result = `C:\\Windows\\System32\\PowerShell\\v1.0\\powershell.exe -command "C:\\LDIAD\\PS\\mailboxExists.ps1 $username"`;
    
  2. Use single quotes to assign literal strings to temporary variables

    my $powershell = 'C:\Windows\System32\PowerShell\v1.0\powershell.exe';
    my $mboxScript = 'C:\LDIAD\PS\mailboxExists.ps1';
    $result = `$powershell -command "$mboxScript $username"`;
    
Share:
14,493

Related videos on Youtube

Kenny
Author by

Kenny

Updated on September 14, 2022

Comments

  • Kenny
    Kenny about 1 year

    I'm trying to execute a PowerShell script from a Perl script and running into some errors. The script runs as a Windows service on a Windows Server 2012 box. What I'm trying to do is determine if an Exchange mailbox exists for a user. The previous Perl code was written so that it could connect to Exchange 2003 which has been working for years. We are now upgrading to Exchange 2013 and need to update the code so that it calls a PowerShell script to perform this task. The PowerShell script will check to see if the user has a mailbox and return a string if it does.

    Perl code

    sub mailboxExists
    {
      my ($argstr) = @_;
      my ($username) = parseArgs($argstr);
    
      # Get the user's dn
      my $dn = userExists($username);
      if(!$dn)
      {
        print $CLIENT "ERR User does not exist, so mailbox can not\n";
        return undef;
      }
    
      # Search
    
      $result = 'C:\Windows\System32\PowerShell\v1.0\powershell.exe -command "C:\LDIAD\PS\mailboxExists.ps1 $username"';
    }
    

    PowerShell script - currently testing using Exchange 2007 as we haven't deployed 2013 yet but the code will mostly be the same.

    #Add the Exchange 2007 snapins
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Support
    
    #Start the script
    $user = Get-User -Identity $username
    If ($user.RecipientType -eq "UserMailbox")
    {
    "1"
    }
    Else
    {
    "0"
    }
    

    The PowerShell code works just fine, I think the problem is my command in Perl to call the PS script? My Perl knowledge is very limited. This is code from an employee that is no longer here. I only added the line that is supposed to execute the PowerShell script.

  • Our Man in Bananas
    Our Man in Bananas over 9 years
    Welcome to Stack Overflow. Please read Stack Overflow: How to answer