-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update-AzureVMLicense.ps1
139 lines (132 loc) · 3.7 KB
/
Update-AzureVMLicense.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<#
.SYNOPSIS
This module is to update the licenses of Virtual Machines within Azure to Windows Server licneses.
.DESCRIPTION
The user logs into Azure and the script will run through all of the resource groups and their virtual machones to update the licenses.
.PARAMETER Machine
This is the name of the machine you want to change the license to.
.PARAMETER ResourceGroup
This is the name of the resource group of all the Virtual machines that you want to change.
.PARAMETER UpdateAll
This is the switch that will go through all user subscriptions and resource groups and change the licenses to Windows Server.
#>
function Update-AzureVMLicense
{
param (
[Parameter][string]$Machine,
[string]$ResourceGroup,
[switch]$UpdateAll
)
BEGIN
{
Login-AzureRmAccount
}
PROCESS
{
if ($Machine)
{
Write-Host "Updating $($machine.Name) in $($group.ResourceGroupName)"
#Find the machine
$wvm = Get-AzureRmVM -ResourceGroupName $ResourceGroup -Name $Machine
#Set the license
$wvm.LicenseType = "Windows_Server"
#Update the Machine
Update-AzureRmVM -ResourceGroupName $group.ResourceGroupName -VM $wvm
}
if ($UpdateAll)
{
$u = Read-Host "This function will go through every subscription and change their license to Windows Server. Enter Y to confirm"
if ($u.ToLower() -eq "y")
{
#Get all of the subscriptions
$allSubs = Get-AzureRmSubScription
#Go through each subscription
foreach ($sub in $allSubs)
{
$resourceGroups = Get-AzureRmResourceGroup
$count = 0;
$machines = @();
foreach ($group in $resourceGroups)
{
#Get all of the VMs
$ret = Get-AzureRMVm -ResourceGroupName $group.ResourceGroupName
if ($ret)
{
Write-Host "Found $($ret.Length) machines in $($group.ResourceGroupName)"
foreach ($machine in $ret)
{
if (-not ($machine.LicenseType -eq "Windows_Server"))
{
$count++
$machines += $machine
Write-Host "Updating $($machine.Name) in $($group.ResourceGroupName)"
$wvm = Get-AzureRmVM -ResourceGroupName $group.ResourceGroupName -Name $machine.Name
$wvm.LicenseType = "Windows_Server"
Update-AzureRmVM -ResourceGroupName $group.ResourceGroupName -VM $wvm
}
else
{
Write-Host "$($machine.Name) already has a Windows Server License"
}
}
}
else
{
Write-Host "No VMS found in $($group.ResourceGroupName)"
}
}
}
}
else
{
Write-Host "Exiting Process"
Exit
}
}
if ($ResourceGroup)
{
$rg = Get-AzureRmResourceGroup -Name $ResourceGroup
}
else
{
$rg = Get-AzureRmResourceGroup
}
$count = 0;
$machines = @();
foreach ($group in $resourceGroups)
{
$ret = Get-AzureRMVm -ResourceGroupName $group.ResourceGroupName
if ($ret)
{
Write-Host "Found $($ret.Length) machines in $($group.ResourceGroupName)"
foreach ($machine in $ret)
{
if (-not ($machine.LicenseType -eq "Windows_Server"))
{
$count++
Write-Host "Updating $($machine.Name) in $($group.ResourceGroupName)"
$wvm = Get-AzureRmVM -ResourceGroupName $group.ResourceGroupName -Name $machine.Name
$wvm.LicenseType = "Windows_Server"
Update-AzureRmVM -ResourceGroupName $group.ResourceGroupName -VM $wvm
$machines += $wvm
}
else
{
Write-Host "$($machine.Name) already has a Windows Server License"
}
}
}
else
{
Write-Host "No VMS found in $($group.ResourceGroupName)"
}
}
}
END
{
$date = Get-Date -UFormat "%Y-%m-%d"
$machines | Export-Csv -Path "$($date)-AzureUpdate.csv"
Write-Host "Updated the license of $($count) machines"
}
}
Update-AzureVMLicense