Azure CLI를 사용하여 단일 가상 머신에 대한 자동 종료를 구성하려면 다음 스크립트를 사용할 수 있습니다.
# Set the resource group name, VM name, and shutdown time
RESOURCE_GROUP_NAME="myResourceGroup"
VM_NAME="myVM" # Add your VM's name here
SHUTDOWN_TIME="18:00"
# Prompt the user to choose whether to auto-restart or leave the machines off
echo "Do you want to auto-restart the machine? (y/n)"
read RESTART_OPTION
# Set the auto-shutdown and auto-start properties based on the user's choice
if [ "$RESTART_OPTION" == "y" ]; then
AUTO_SHUTDOWN="true"
AUTO_START="true"
else
AUTO_SHUTDOWN="true"
AUTO_START="false"
fi
# Set the auto-shutdown and auto-start properties for the VM
az vm auto-shutdown -g $RESOURCE_GROUP_NAME -n $VM_NAME --time $SHUTDOWN_TIME
if [ "$AUTO_START" == "true" ]; then
az vm restart -g $RESOURCE_GROUP_NAME -n $VM_NAME --no-wait
fi
Azure CLI를 사용하여 여러 가상 머신에 대한 자동 종료를 구성하려면 다음 스크립트를 사용할 수 있습니다.
# Set the resource group name and shutdown time
RESOURCE_GROUP_NAME="myResourceGroup"
SHUTDOWN_TIME="18:00"
# Prompt the user to choose whether to auto-restart or leave the machines off
echo "Do you want to auto-restart the machines? (y/n)"
read RESTART_OPTION
# Set the auto-shutdown and auto-start properties based on the user's choice
if [ "$RESTART_OPTION" == "y" ]; then
AUTO_SHUTDOWN="true"
AUTO_START="true"
else
AUTO_SHUTDOWN="true"
AUTO_START="false"
fi
# Loop through all VMs in the resource group and set the auto-shutdown and auto-start properties
for VM_ID in $(az vm list -g $RESOURCE_GROUP_NAME --query "[].id" -o tsv); do
az vm auto-shutdown --ids $VM_ID --time $SHUTDOWN_TIME
az vm restart --ids $VM_ID --no-wait
done
위의 스크립트는 az vm auto-shutdown 및 az vm restart 명령을 사용하여 지정된 리소스 그룹에 있는 모든 VM의 auto-shutdown 및 restart 속성을 설정합니다. --ids 옵션은 해당 ID로 VM을 지정하는 데 사용되며 --time 및 --auto-start- 지원 옵션은 각각 자동 종료 및 자동 시작 속성을 설정하는 데 사용됩니다.
또한 두 스크립트는 컴퓨터를 자동으로 다시 시작할지 아니면 수동으로 다시 시작할 때까지 해제할지를 선택하라는 메시지를 표시합니다. 선택은 VM의 -auto-shutdown-enabled 속성을 설정하는 데 사용됩니다.