从Perl脚本执行PowerShell脚本

时间:2013-12-18 15:14:20

标签: perl powershell

我正在尝试从Perl脚本执行PowerShell脚本并遇到一些错误。该脚本在Windows Server 2012框上作为Windows服务运行。我要做的是确定用户是否存在Exchange邮箱。编写之前的Perl代码,以便它可以连接到已经工作多年的Exchange 2003。我们现在升级到Exchange 2013并需要更新代码,以便它调用PowerShell脚本来执行此任务。 PowerShell脚本将检查用户是否有邮箱,如果有,则返回一个字符串。

Perl代码

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脚本 - 目前正在使用Exchange 2007进行测试,因为我们尚未部署2013,但代码大致相同。

#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"
}

PowerShell代码工作正常,我认为问题是我在Perl中调用PS脚本的命令?我的Perl知识非常有限。这是来自不再在这里的员工的代码。我只添加了应该执行PowerShell脚本的行。

3 个答案:

答案 0 :(得分:1)

单引号,双引号和反引号字符在Perl中都有不同的特定含义,让它们混淆会使你的程序以意想不到的方式运行。

对于"double quotes"内的文本,Perl会插入变量并重新解释以反斜杠开头的字符

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

'single quotes'内部,Perl不会插入变量,并将反斜杠视为文字反斜杠字符,除非后面跟着另一个反斜杠或单个引号字符。使用它们告诉Perl按字面意思处理引号内的文本。

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

对于反引号内的文本(抱歉,标记不可用),Perl会做两件事。 (1)重新解释字符串,好像它在双引号内(插入变量,替换转义字符序列),以及(2)将结果字符串作为命令运行传递给操作系统,返回命令的输出

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

<小时/>

回到原来的问题:你希望运行命令

C:\Windows\System32\PowerShell\v1.0\powershell.exe -command "C:\LDIAD\PS\mailboxExists.ps1 $username"
在您的操作系统上

,其中“$username”被Perl变量$username的字符串值替换,并且您希望将该命令的输出分配给变量$result。以下是实现这一目标的一些方法:

  1. 使用插值反引用,转义\

    $result = `C:\\Windows\\System32\\PowerShell\\v1.0\\powershell.exe -command "C:\\LDIAD\\PS\\mailboxExists.ps1 $username"`;
    
  2. 使用单引号将文字字符串分配给临时变量

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

答案 1 :(得分:0)

尝试像

一样运行
$scriptpath="C:\LDIAD\PS\mailboxExists.ps1 $username";
$exepath = "C:\Windows\System32\PowerShell\v1.0\powershell.exe";

然后使用

$result = `$exepath -File $scriptpath`;

system("$exepath -File $scriptpath");

答案 2 :(得分:0)

在Windows 7上,以下工作正常。 是的,需要进行大量的清洁工作,但截至目前,它对我有用。

$constant = "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe";
$param1 = " -psc ";
$param2 = "\"C:\\Program\ Files\ (x86)\\VMware\\Infrastructure\\vSphere\ PowerCLI\\vim.psc1\" -noe -c";
$param3 = " \". \\\"C:\\Program\ Files\ (x86)\\VMware\\Infrastructure\\vSphere\ PowerCLI\\Scripts\\Initialize-PowerCLIEnvironment.ps1\\\"\"";

$test = $param1.$param2.$param3;
open (IN, "| \"$constant\" $test") or die "$!";