-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate_VM.ps1
More file actions
37 lines (16 loc) · 998 Bytes
/
Copy pathUpdate_VM.ps1
File metadata and controls
37 lines (16 loc) · 998 Bytes
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
Param($ResourceGroupName, $VMName)
#Start Logging
Write-Output "Starting Powershell update $((get-date).AddHours(12))"
$vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName
$vm.LicenseType = 'Windows_Server'
Write-Output "Target VM: $($Vm.Name)"
$tags = (Get-AzResource -ResourceId $vm.id).Tags
if (-not($tags.ContainsKey('Created by')))
{Write-Output "Searching Activity log for Creator..."
$tags.'Created by'=(((Get-AzActivityLog -ResourceId $vm.Id |where-object{$_.OperationName.Value -eq 'Microsoft.Compute/virtualMachines/write' -and $_.SubStatus.value -eq 'Created'} ) | select -first 1).Caller -split '@' )[0]
$tags.'Remediated'=((get-date).AddHours(12))
}
#Update resources
Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName
Update-AzTag -ResourceId $vm.id -Tag $tags -Operation Merge
Write-Output "...Deployment Script complete...$((get-date).AddHours(12))"