防止Powershell非终止错误返回给PHP

时间:2016-03-20 22:00:34

标签: php powershell

我有一个网页,允许IT人员输入新员工的信息,它会自动创建相应的帐户。我使用以下命令通过PHP调用Powershell脚本,只返回错误:

$psPath = 'c:\\Windows\\System32\WindowsPowerShell\v1.0\\powershell.exe -version 5';
$psDIR = "d:\\wamp64\\www\\includes\\";
$psScript = "NewHire.ps1";
$runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript;

$createuser = exec($runCMD.' 2>&1>1' , $out);

如果有任何错误,我会在var_dump($out);

的网页上显示这些错误

我的新雇用脚本中有一部分正在抛出从var_dump($out);显示的非终止错误我通过检查用户的AD用户名来查看它是否存在。然后再次尝试,如果它存在或使用该用户名。

$Count = 1

    DO
    {
        $adUsername = "$Firstname" +"$Lastname".Substring(0,$Count)
        $CheckEmail = C:\GAM\gam.exe info user $adUsername
        $CheckUser = Get-ADUser -LDAPFilter "(sAMAccountName=$Name)" 
        If ($CheckUser -eq $Null -and $CheckEmail -eq $Null)
        {
            $adUsername = $adUsername.ToLower()
        }
        Else
        {
            $Count = $Count + 1
        }
    } Until ($CheckUser -eq $Null -and $CheckEmail -eq $Null)

我要做的是防止记录/显示此部分的错误。它每次都从$ out变量显示:

array (size=10)
  0 => string 'Get-ADUser : The search filter cannot be recognized' (length=51)
  1 => string 'At D:\wamp64\www\includes\NewHire.ps1:191 char:18' (length=49)
  2 => string '+ ... CheckUser = Get-ADUser -LDAPFilter "(sAMAccountName=$Name)" -ErrorAct ...' (length=79)
  3 => string '+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' (length=75)
  4 => string '    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException' (length=73)
  5 => string '    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirec' (length=78)
  6 => string '   tory.Management.Commands.GetADUser' (length=37)
  7 => string '' (length=0)

我尝试过使用-ErrorAction Ignore和SilentlyContinue $CheckUser = Get-ADUser -LDAPFilter "(sAMAccountName=$Name)" -ErrorAction Ignore,但仍然显示错误。无论如何,我可以阻止这个或更好的方法来执行此代码吗?

0 个答案:

没有答案