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.
$result = `command`;to executecommand– but there are more flexible, safer ways than that.