-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoResourceTag.ps1
63 lines (56 loc) · 2.37 KB
/
AutoResourceTag.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
#Log into Azure
Login-AzureRmAccount
#Get list of all the VM's
$list = Get-AzureRmResource |sort-object name
#Reads tags from csv file
$reader = (Get-FileName "C:\Users")
$reader = [System.IO.File]::OpenText($reader)
$tags=@{}
$tags.clear()
$key=@()
$key.clear()
#Separates each line into the resource name, and the tags
$line = ($reader.readLine().split(','))
$line = $line[1..$line.Length]
$counter =0
foreach ($values in $line){
#Adds the keys to an array
$key+=$values
}
#Loops through rest of the CSV
while($null -ne ($line = $reader.ReadLine())) {
$values = $line.Split(',')
#Loops through each resource from Get-AzureRMResource
foreach ($element in $list){
#Check to make sure the Resource is not a virtual Machine
if($element.ResourceType -ne 'Microsoft.Compute/virtualMachines'){
if($values[0] -eq $element.name){
for($i=0;$i -lt $key.Length; $i++){
#If the key is Project, it'll put the resource group name for the value instead of what was in the csv
if($key[$i] -eq 'Project'){
$values[$i+1] = $element.ResourceGroupName
$tags+= @{$key[$i]=$values[$i+1]}
}elseif($key[$i] -eq 'Group'){
#For keys that are 'Group', checks to see if there is an associated value or not
#Will tag if there is a value
if($values[$i+1]){
$tags+=@{Group=$values[$i+1]}
}
}else{
#If key is not 'Project' or 'Group' then it'll add they key-value to a hash-table in $tags
$tags+=@{$key[$i]=$values[$i+1]}
}
}
#THis is the command that actually adds the tags to the correct resource
Set-AzureRmResource -ResourceGroupName $element.ResourceGroupName -ResourceType $element.ResourceType -ResourceName $element.ResourceName -Tag $tags -Force
$tags
$counter++
#Break the loop that is going through each resource, to move on to the next resource name to be tagged
break
}
}
}
echo '------------------------'
$tags.clear()
}
$counter