@ECHO OFF
rem Copyright (C) IBM Corp. 2005-2016, All rights reserved.

SETLOCAL

::::::::: COUNT THE NUMBER OF CONTROLLERS ::::::::::::::::
SET CONTROLLER=0
:COUNT
SET /A CONTROLLER+=1
sasflash.exe -o -listsasadd -c %CONTROLLER% >NUL
IF %ERRORLEVEL% EQU 0 (GOTO COUNT) ELSE (SET /A CONTROLLER-=1)

::ECHO CONTROLLER=%CONTROLLER%

:: IF Detail print Detail infor or else just print wwpn
IF "%1"=="detail" (goto :DETAIL) 

::::: PRINT SAS WWPN ONLY :::::::::::::
FOR /L %%i IN (1,1,%CONTROLLER%) DO ((sasflash.exe -o -list -c %%i | FIND /i "SAS ADD" >vtmp.txt)&&(call :MANIPULATE %%i))
GOTO END

:DETAIL
FOR /L %%i IN (1,1,%CONTROLLER%) DO ((ECHO.)&&(sasflash.exe -o -list -c %%i | FINDSTR "Vendor Product Con Address"))
GOTO END


:END
ECHO.
ENDLOCAL
GOTO :EOF
@ECHO ON

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::: FUNCTIONS:::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:MANIPULATE
set /p vTmp= < vTmp.txt
del vTmp.txt >nul
ECHO a="%vTmp%"> tmp.vbs
ECHO a = Right(Trim(a),19) >> tmp.vbs
ECHO b=Split(a,"-") >> tmp.vbs
ECHO Wscript.Echo "Controller%~1 Port1:   "^&b(0)^&"-"^&b(1)^&"-"^&b(2)^&"-"^&b(3) >> tmp.vbs
ECHO Wscript.Echo "Controller%~1 Port2:   "^&b(0)^&"-"^&b(1)^&"-"^&b(2)^&"-"^&LCase((hex(("&H"^&b(3))+1))) >> tmp.vbs

cmd /c "cscript.exe tmp.vbs" |find /i /v "microsoft"
del tmp.vbs
goto :EOF
