Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Re : Powershell does NOT return errorcode to CMD file thats invoking PS

$
0
0

Hello there,

I am trying to return the exit code from PS back to CMD file that invokes the PS. Funny thing is PS does print exit code as 1 but CMD files still shows errorlevel as 0..... not sure what is causing it. The catch block in PS , I attempted to make generic and seems catch the exception correctly and is also printing the errorcode as 1 in PS. but upon exit 1 in PS , CMD file still shows %ERRORCODE% as 0 !

Appreciate suggestions on this.

CMD File


:DBLOGIN
CALL login.cmd username
if %errorlevel% == 1 goto badend
SET PWORD=%PASS_WORD%

:ADLOGIN
CALL AD_login.cmd %5
if %errorlevel% == 1 goto badend
SET AD_PWORD=%AD_PASS_WORD%


SET PS_DIR=%1
SET ROOT_DIR=%2
SET UNAME=%3
SET CONNSTR=%4
SET ELE_UNAME=%5
SET GROUPNAME=%6
SET GROUPDOMAINNAME=%7
SET DIR=%8



d:
cd %ROOT_DIR%



%PS_DIR% -File "%ROOT_DIR%\adsync.ps1" %UNAME% %PWORD% %CONNSTR% %ELE_UNAME% %AD_PWORD% %GROUPNAME% %GROUPDOMAINNAME% %DIR% 2>&1 | more

echo error level is %ERRORLEVEL%

if %ERRORLEVEL% NEQ 0 GOTO badend

GOTO ENDPROC




:badend
REM BAD END %ERRORLEVEL%
@ECHO OFF & ECHO. & DATE /T & TIME /T & ECHO. & @ECHO ON
exit /B 55

:ENDPROC
REM ACCEPTABLE RETURN CODE FOUND
REM Error Level - %ERRORLEVEL%
@ECHO OFF & ECHO. & DATE /T & TIME /T & ECHO. & @ECHO ON
EXIT /B 0

The powershell code is : 

Import-Module ActiveDirectory

$returncode =0;

try
{

$forestName = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Name
$ADsPath = [ADSI]"GC://$forestName"
$Search = New-Object System.DirectoryServices.DirectorySearcher($ADsPath)

$OraClientDir=$args[7];
echo $OraClientDir;

[Reflection.Assembly]::LoadFile("$($OraClientDir)Oracle.DataAccess.dll")
$DBUserName=$args[0];
$DBPassword=$args[1];
$DBSourceName=$args[2];
$eleuser=$args[3];
echo $eleuser;
$elepword=ConvertTo-SecureString -String $args[4] -AsPlainText -Force;
echo $elepword;
$Credentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $eleuser,$elepword;
echo $Credentials;
$ADGroupName=$args[5];
echo $ADGroupName;
$ADGroupServerName=$args[6];
echo $ADGroupServerName;
$ADGroupServerString="$($ADGroupName) -server ""$($ADGroupServerName)""" ;
echo $ADGroupServerString;





Write-Host \n;
echo " Start time is : $(Get-Date -format "yyyy:MM:dd hh:mm:ss:tt") ";

$con=New-Object Oracle.DataAccess.Client.OracleConnection("Data Source=$DBSourceName;User Id=$DBUserName;Password=$DBPassword")
$con.open()
$cmd=$con.CreateCommand()
$cmd.CommandText="SELECT blah blah SQL"
$rdr=$cmd.ExecuteReader()


$found= $false;
$counter=0;
$counterNew=0;
$group=Get-ADGroup $ADGroupName -server $ADGroupServerName;
$members = @()
Get-ADGroupMember -Identity $group | Select-Object -ExpandProperty sAMAccountName |ForEach-Object {$members += $_};
$path="\logs\log_$(Get-Date -format yyyy_MM_dd_hh_mm_ss_tt).log";
Write-Host "The total members in group " $group " are " $members.length;
Add-Content -Path $path -Value ("The total members in group " +  $group + " are " + $members.length ) -Force;


while ($rdr.Read()) 
{ 

	for ($i=0;$i -lt $rdr.FieldCount ; $i++) 
	{ 
		$found= $false;
		$s=$rdr.GetValue($i);
		$Search.Filter = "(&(objectCategory=User)(SamAccountName=$s))"
		if ($Search.FindAll().Count -eq 0)
			{
			Add-Content -Path $path -Value "The user $s does NOT exists in AD global catalog" -Force;
			Write-Host "The user : " $s  "does NOT exists in AD global catalog" ;
			$counter++;
			}
		else	{
			try {
  				foreach ($res in $Search.FindAll())
     				{
           			$User = $res.GetDirectoryEntry();
				Add-Content -Path $path -Value ("The user " + $User.SamAccountName + " Found in AD global catalog") -Force;
				Write-Host "The user distinguished name is  : " $User.DistinguishedName " Found in AD global catalog" ;
				$DC = $User.DistinguishedName.ToString();
				$DCString = $DC.SubString($DC.IndexOf("DC="));
				$FQDN = $DCString.replace("DC=","").replace(",",".");
				# Add-Content -Path $path -Value $FQDN -Force;
				$found = $true;

				if ( ($found) -and ($members -notcontains $s))
					{
					$checkuser= Get-ADUser $User.SamAccountName.ToString()  -Server $FQDN ;
					Add-ADGroupMember $group -Members $checkuser -Server $ADGroupServerName  -Credential $Credentials;
					Add-Content -Path $path -Value ("Added user: " + $User.SamAccountName ) -Force ;
					Write-Host "Added user: "  $User.SamAccountName;
					$counterNew++;
					}

				}
			     } catch  [Exception] { 
				    Write-Host $_.Exception.Message "is inner catch";
				    $returncode =1;
				      		  }
			}
	}
}

Add-Content -Path $path -Value "Total of :  $counterNew  users users have been added " -Force;
Write-Host "Total of :  $counterNew  users users have been added to AD Group";
Add-Content -Path $path -Value "Total of :  $counter  users do NOT exist in Windows AD global catalog";
Write-Host "Total of :  $counter  users do NOT exist in Windows AD global catalog";

}

catch  [Exception] { 
Write-Host $_.Exception.Message "is outer catch";
$returncode =1;

}






Write-Host \n;
echo " End time is : $(Get-Date -format "yyyy:MM:dd hh:mm:ss:tt") ";

Write-Host "value of return code is  " $returncode; 
exit $returncode;


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images