Last active
May 4, 2017 04:59
-
-
Save dzeyelid/b8f7cc146277a9c365eeae81abcdf26a to your computer and use it in GitHub Desktop.
Utility to use Azure VMs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
set CURRENT_DIR=%CD% | |
pushd %0\.. | |
setlocal enabledelayedexpansion | |
set RESOURCE_GROUP=%1 | |
set ACTION=%2 | |
set EMPTY_ARGS=FALSE | |
if "%1"=="" set EMPTY_ARGS=TRUE | |
if "%2"=="" set EMPTY_ARGS=TRUE | |
if %EMPTY_ARGS%==TRUE ( | |
echo; | |
echo Please specify args | |
echo; | |
echo rule:^ ^ ^ ^ vms.bat^ resource_group_name action | |
echo; | |
echo actions:^ start,^ stop,^ deallocate,^ ips,^ status | |
echo; | |
echo ex:^ ^ ^ ^ ^ ^ vms.bat^ myResourceGroup start | |
echo ex:^ ^ ^ ^ ^ ^ vms.bat^ myResourceGroup ips | |
echo ex:^ ^ ^ ^ ^ ^ vms.bat^ myResourceGroup deallocate | |
echo; | |
goto END | |
) | |
set VMLIST_FILE=%RESOURCE_GROUP%-vms.tsv | |
rem Get list of IP addresses of VM | |
if "%ACTION%"=="ips" ( | |
call :GET_VM_IP_LIST %RESOURCE_GROUP% | |
goto END | |
) | |
rem Get a list of VM ids | |
if not exist %VMLIST_FILE% ( | |
echo; | |
echo; | |
echo Creating^ a^ list^ of^ VM^ ids | |
echo ------------------------------------------------ | |
echo; | |
set CMD=az vm list -g %RESOURCE_GROUP% --query "[].id" -o tsv | |
echo --^>^ !CMD! | |
call !CMD! > %VMLIST_FILE%.tmp | |
call ping localhost -n 3 > null | |
for %%i in (%VMLIST_FILE%.tmp) do if %%~zi==0 goto END | |
call move %VMLIST_FILE%.tmp %VMLIST_FILE% | |
) | |
set VALID_ACTION=FALSE | |
if "%ACTION%"=="start" ( | |
set VALID_ACTION=TRUE | |
) else if "%ACTION%"=="stop" ( | |
set VALID_ACTION=TRUE | |
) else if "%ACTION%"=="deallocate" ( | |
set VALID_ACTION=TRUE | |
) else if "%ACTION%"=="status" ( | |
set VALID_ACTION=TRUE | |
) | |
if %VALID_ACTION%==FALSE ( | |
echo Invalid^ action | |
goto END | |
) | |
rem Create Id lists seperated by space | |
set IDS= | |
for /f %%i in (%VMLIST_FILE%) do ( | |
set IDS=!IDS!%%i^ | |
) | |
rem Exec action | |
if "%ACTION%"=="start" ( | |
call :EXEC_VM_SIMPLE_ACTION %ACTION% "%IDS%" | |
) else if "%ACTION%"=="stop" ( | |
call :EXEC_VM_SIMPLE_ACTION %ACTION% "%IDS%" | |
) else if "%ACTION%"=="deallocate" ( | |
call :EXEC_VM_SIMPLE_ACTION %ACTION% "%IDS%" | |
) else if "%ACTION%"=="status" ( | |
call :GET_VM_INSTANCE_STATUS "%IDS%" | |
) | |
goto END | |
rem ----------------------- | |
rem [Sub routine] | |
rem Get list of vm's ip addresses | |
rem ----------------------- | |
:GET_VM_IP_LIST | |
set RESOURCE_GROUP=%1 | |
echo; | |
echo; | |
echo Get list | |
echo ------------------------------------------------ | |
echo; | |
set CMD=az vm list-ip-addresses --query "[].virtualMachine[].{Name:name, PublicIp:network.publicIpAddresses[0].ipAddress, PrivateIp:network.privateIpAddresses[0]}" -o table -g %RESOURCE_GROUP% | |
echo --^>^ %CMD% | |
echo; | |
call %CMD% | |
exit /b | |
rem ----------------------- | |
rem [Sub routine] | |
rem Exec vm simple action | |
rem ----------------------- | |
:EXEC_VM_SIMPLE_ACTION | |
set ACTION=%1 | |
set IDS=%~2 | |
echo; | |
echo; | |
echo Exec^ action | |
echo ------------------------------------------------ | |
echo; | |
set CMD=az vm %ACTION% -o table --ids %IDS% | |
echo --^>^ %CMD% | |
echo; | |
call %CMD% | |
exit /b | |
rem ----------------------- | |
rem [Sub routine] | |
rem Get instance status | |
rem ----------------------- | |
:GET_VM_INSTANCE_STATUS | |
set IDS=%~1 | |
echo; | |
echo; | |
echo Get^ VM^ instance^ status | |
echo ------------------------------------------------ | |
echo; | |
set CMD=az vm get-instance-view --query "[].{Name:name,PowerState:instanceView.statuses[1].displayStatus}" -o table --ids %IDS% | |
echo --^>^ %CMD% | |
echo; | |
call %CMD% | |
exit /b | |
:END | |
endlocal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment