-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPower-Azure-VM-From-Excel.ps1
114 lines (90 loc) · 3.5 KB
/
Power-Azure-VM-From-Excel.ps1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# This script will start, restart, or stop Azure virtual machines listed in an Excel file, based on user selection
Import-Module -Name Az
Import-Module -Name ImportExcel
#Connect to an Azure Account
try{
Connect-AzAccount
}
catch{
Write-Host "Could not connect to an Azure account."
exit
}
#Wait for Azure account connection to process
Start-Sleep -Second 8
#Input the Excel file path
while ($true){
$excelPath = Read-Host "Enter the path of the Excel file with Azure VMs listed "
Write-Host "`nConfirmed file path is $excelPath"
#Check if file path exists on local machine, and is of type .xlsx
$isPath = Test-Path -Path $excelPath
if ($isPath -and $excelPath -like "*.xlsx"){
break
}
else{
Write-Host "Please enter a valid Excel file path"
}
}
#Input the Excel column to process
$column = Read-Host "`nEnter the column to process "
Write-Host "Confirmed column to process is $column"
#Input the number of Excel sheet rows to iterate through
$rowCount = Read-Host "`nEnter the number of rows to process "
Write-Host "Confirmed number VMs to process is $rowCount"
#Select VM Operation - start, restart, stop
$operationList = 'start','restart','stop'
while ($true){
$vmOperation = Read-Host "`nWould you like to start, restart, or stop the VMs?`nEnter start, restart, or stop "
Write-Host "`nConfirmed operation is to $vmOperation the VMs`n"
if($operationList -contains $vmOperation ){
break
}
else{
Write-Host "Please enter one of the following VM operations - start, restart, stop"
}
}
#Print contents of Excel file
Import-Excel -Path $excelPath
#Open Excel workbook, sheet
$excelBook = Open-ExcelPackage -Path $excelPath
$excelSheet = $excelBook.Workbook.Worksheets['Sheet1']
#Iterate through each row of Excel column previously selected
for($i = 1; $i -le $rowCount; $i++){
$cell = $column + $i
Write-Host "Processing $cell ..."
$cellValue = $excelSheet.Cells[$cell].Value
Write-Host "`nPulling VM information...`n"
#Check if VM exists in connected Azure subscription
try{
$vm = Get-AzVM -Name $cellValue
$vmStartStateHash = Get-AzVM -Name $cellValue -Status | Select-Object powerstate
$vmStartStateValue = $vmStartStateHash.PowerState
Write-Host "VM Name: " $vm.Name
Write-Host "Resource Group: " $vm.ResourceGroupName
Write-Host "OS: " $vm.StorageProfile.ImageReference.Offer "- " $vm.StorageProfile.ImageReference.Sku
Write-Host "Status: " $vmStartStateValue
Write-Host "Tags: " $vm.Tags
}
catch{
Write-Host "`nCould not find $cellValue in this Azure environment"
}
Write-Host "`n`nPerforming $vmOperation on $cellValue ..."
#Perform selected VM operation
try{
if($vmOperation -eq "start"){
Start-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
}
elseif ($vmOperation -eq "restart"){
Restart-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
}
else{
Stop-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
}
}
catch{
Write-Host "`nCould not $vmOperation this VM"
}
#Add segment lines between operations
Write-Host "`n----------------------------------------------------------------------`n"
}
#Save Excel file changes
Close-ExcelPackage $excelBook