forked from palma21/azurescripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managevm.sh
59 lines (53 loc) · 1.66 KB
/
managevm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
AZURE_RESOURCE_GROUP=$2
if [ $1 == "start" ];
then
if [ $AZURE_RESOURCE_GROUP == "all" ];
then
az vm start --ids $(az vm list --query "[].id" -o tsv)
else
VM_NAMES=$(az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[?powerState=='VM deallocated'].{ name: name }" -o tsv)
for NAME in $VM_NAMES
do
echo "Starting $NAME"
az vm start -n $NAME -g $AZURE_RESOURCE_GROUP --no-wait
done
fi
fi
if [ $1 == "stop" ];
then
if [ $AZURE_RESOURCE_GROUP == "all" ];
then
az vm deallocate --ids $(az vm list --query "[].id" -o tsv)
else
VM_NAMES=$(az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[?powerState=='VM running'].{ name: name }" -o tsv)
for NAME in $VM_NAMES
do
echo "Stopping $NAME"
az vm deallocate -n $NAME -g $AZURE_RESOURCE_GROUP --no-wait
done
fi
fi
if [ $1 == "status" ];
then
echo "Fetching Details..."
if [ $AZURE_RESOURCE_GROUP == "all" ];
then
az vm list --show-details
else
az vm list -g $AZURE_RESOURCE_GROUP --show-details
fi
echo "Fetching Summary..."
echo "Power Status of all VMs"
echo "-----------------------"
if [ $AZURE_RESOURCE_GROUP == "all" ];
then
az vm list --show-details --query "[].{name: name, status: powerState}" -o table
else
az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[].{name: name, status: powerState}" -o table
fi
fi
if [ $1 == "ip" ];
then
az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[?powerState=='VM running'].{ name:name, ip: publicIps }" -o table
fi