@ECHO OFF
 
CLS
ECHO .
ECHO .
Echo "The Shutdown Procedure  for MK (Data Center) SERVRES is starting"
ECHO .
ECHO .
REM ----- Virables -----

REM This virable is to stop by mistake to shutdown the servers
REM if is CheckCommand=echo is just to view the commands
REM if is CheckCommand= the script run 
SET CheckCommand=echo
REM User Confirmation
SET UserAccept=%1
REM Control Var if shutdown all in one OR One by One
SET CONTROLS=%2
REM set time to shutdown to 1 min
SET SECOND=60

REM Command to connect to Linux server
REM To execute the halt without password in linux you must insert AdminUser to sudoers.
REM Add to file /etc/sudoers
REM lAdminUser ALL=NOPASSWD:/sbin/halt
SET LinuxConnect="C:\Program Files\Putty\plink.exe"
REM Linux Passwords
REM Passwords for Linux Servers
SET LinuxPasswordsA1=(Linux Password)
REM Linux command
SET LinuxSutdown=halt
REM ---------------------------
REM Command for Windows Shutdown
SET WindowsShutdown=c:\windows\system32\shutdown.exe /c "The SERVER Will ShutDown in %SECOND% seconds."

SET ShutDownOptions=/s /f /t %SECOND% /d P:4:2 

REM LOG File
SET LogFile=C:\Net-SNMP\usr\etc\shutdown-Log.txt

SET EVAPATH="C:\Program Files\Hewlett-Packard\Sanworks\Element Manager for StorageWorks HSV\Shutdown-EVA\"
REM ----- End Virables -----
REM --------------------------
REM ---------------------------

REM --- Check the if By mistace run the script ---


if "%UserAccept%"=="YES" (
	if "%CONTROLS%"=="1"  SET OneByOne="N"
	if "%CONTROLS%"=="2" SET OneByOne="Y"
	if "%CONTROLS%"=="" SET OneByOne="N"
	
	goto ShutDownProcedure
)
Echo the Procedure is not confirm from the user
GOTO XEND

REM ----- Start Shutdown Procedure ---
:ShutDownProcedure

REM Write the event to log
echo Shutdown Procedure Stared >> %LogFile%
date /T >> %LogFile%
time /T >> %LogFile%
REM --------------------------------------------------------------------------
REM --------------------------------------------------------------------------
REM ---- Start ShutDown Procedure ---
REM Windows Servers

REM ---- Windows Server name ----
SET SERVER=Windows Server name
SET Description=( %SERVER% Discription )
REM -------------------------------------------
echo .
echo .
echo "------------ %SERVER% ----------------"
echo Shutdown %SERVER% %Description%
echo .
echo .
REM -------------------------------------------
REM --- Check if if shutdown all in one OR One by One ---
set Confirm=Y
if %OneByOne%=="Y" goto check-one-%SERVER%
goto %SERVER%
:check-one-Windows Server name
	
 	echo Do you want to Shutdown  %SERVER% 
  	echo "Please Write [Yes Default =(Y)],[ No=(N)] Capital Letters"
 	SET /P Confirm=Answer"(%Confirm%)" ?
    if "%Confirm%"=="Y"	goto %SERVER%  
goto end-%SERVER%
	
REM ---END  Check if if shutdown all in one OR One by One ---

:Windows Server name
echo Shutdown %SERVER% %Description%
ECHO CHECK %SERVER% IF IS ON
ping -n 1  -w 720 %SERVER%|find "TTL=" && %CheckCommand% %WindowsShutdown% %ShutDownOptions% /m \\%SERVER%. 
REM ---- END Windows Server name ----
:end-Windows Server name


REM --------------------------------------------------------------------------
REM --------------------------------------------------------------------------
REM Linux Servers

REM ---- Linux Server name ----
SET SERVER=Linux Server name
SET Description="( %SERVER% Discription)"
REM -------------------------------------------
echo .
echo .
echo "------------ %SERVER% ----------------"
echo Shutdown %SERVER% %Description%
echo .
echo .
REM -------------------------------------------
REM --- Check if if shutdown all in one OR One by One ---
set Confirm=Y
if %OneByOne%=="Y" goto check-one-%SERVER%
goto %SERVER%
:check-one-Linux Server name
	
 	echo Do you want to Shutdown  %SERVER% 
  	echo "Please Write [Yes Default =(Y)],[ No=(N)] Capital Letters"
 	SET /P Confirm=Answer"(%Confirm%)" ?
    if "%Confirm%"=="Y"	goto %SERVER%  
goto end-%SERVER%
	
REM ---END  Check if if shutdown all in one OR One by One ---

:Linux Server name
echo Shutdown %SERVER% %Description%
ECHO CHECK %SERVER% IF IS ON
ping -n 1  -w 720 %SERVER%|find "TTL=" && %CheckCommand% %LinuxConnect% -pw %LinuxPasswordsA1% AdminUser@%SERVER%  %LinuxSutdown%
:end-Linux Server name
REM ---- END Linux Server name ----

REM --------------------------------------------------------------------------
REM --------------------------------------------------------------------------


REM --------------------------------------------------------------------------
REM --------------------------------------------------------------------------
REM ---- VMWare Server name ---- Θα πρέπει να γίνεται και ελεγχος στα vm αν εκλεισαν
SET SERVER=VMWare Server name
SET Description="( %SERVER% Discription)"
REM -------------------------------------------
echo .
echo .
echo "------------ %SERVER% ----------------"
echo Shutdown %SERVER% %Description%
echo .
echo .
REM -------------------------------------------
REM --- Check if if shutdown all in one OR One by One ---
set Confirm=Y
if %OneByOne%=="Y" goto check-one-%SERVER%
goto %SERVER%
:check-one-VMWare Server name
	
 	echo Do you want to Shutdown  %SERVER% 
  	echo "Please Write [Yes Default =(Y)],[ No=(N)] Capital Letters"
	echo .
	echo If you ShutDown %SERVER% Then  VmHosts will be ShutDown
 	SET /P Confirm=Answer"(%Confirm%)" ?

    if "%Confirm%"=="Y"	goto %SERVER%  
goto end-%SERVER%
	
REM ---END  Check if if shutdown all in one OR One by One ---

:VMWare Server name
REM Check if VMhosts is down 
echo .
echo .
echo Check if All Hosts ( VMHosts )are Down
echo .
:check-VMHost-1
%CheckCommand% cls
echo check VMHost-1
echo Please wait..
%CheckCommand% ping -n 1  -w 720 VMHost-1|find "TTL=" && goto check-VMHost-1
echo .
:check-VMHost-2
echo check VMHost-2
echo Please wait..
%CheckCommand% ping -n 1  -w 720 VMHost-2|find "TTL=" && goto check-VMHost-2
echo .

REM Check if VMHosts

echo Shutdown %SERVER% %Description%
ECHO CHECK %SERVER% IF IS ON
ping -n 1  -w 720 %SERVER%|find "TTL=" && %CheckCommand% %LinuxConnect% -pw %LinuxPasswordsA1% AdminUser@%SERVER%  %LinuxSutdown%
:end-VMWare Server name
REM ---- END Vs-1 ----
REM --------------------------------------------------------------------------
REM --------------------------------------------------------------------------


REM --------------------------------------------------------------------------
REM --------------------------------------------------------------------------
REM RESET ALL Virable and EXIT
:XEND
set CONTROLS=
set OneByOne=
cd C:\Net-SNMP\usr\etc\
ENDLOCAL
