$RGs =@()
$processed_vm =@()
$RGs = $(Get-AzureRMResourceGroup |Select-Object -Property ResourceGroupName -Verbose:$false )
foreach ($RG in $RGs)
{
$ResourceGroupName = $RG.ResourceGroupName
$vm_list = Get-AzureRmVM -ResourceGroupName $ResourceGroupName | ? ( {($_.Tags.Keys -notcontains "Do Not Disturb") -or ($_.Tags.Keys -eq "Do Not Disturb" -and $_.Tags.Values -eq "No") })
foreach($vm in $vm_list)
{
$vm_name=$vm.Name
Get-AzureRmVM -name $vm_name -Status -ResourceGroupName $ResourceGroupName | Select-Object -Property Name, Statuses|
ForEach-Object {
$_.Statuses |
Where-Object {$_.Code -like 'PowerState/*'} |
ForEach-Object {
New-Object -TypeName psobject -Property @{
Status = $_.DisplayStatus
}
if ( $_.DisplayStatus -eq "VM running" -And $(Get-AzureRmResource -ResourceGroupName "$ResourceGroupName" -Name $VMName).Tags.'Do Not Disturb' -eq "No" ){
#Stop-AzurermVM -ResourceGroupName "$ResourceGroupName" -Name $VMName -Force -ErrorAction SilentlyContinue
$processed_vm += $vm_name
}
}
}
}
}
write-output " List of VMs Stopped " |Out-File vmlist.txt
write-output "-----------------------" | Add-Content vmlist.txt
$processed_vm | Add-Content vmlist.txt
$processed_vm =@()
$RGs = $(Get-AzureRMResourceGroup |Select-Object -Property ResourceGroupName -Verbose:$false )
foreach ($RG in $RGs)
{
$ResourceGroupName = $RG.ResourceGroupName
$vm_list = Get-AzureRmVM -ResourceGroupName $ResourceGroupName | ? ( {($_.Tags.Keys -notcontains "Do Not Disturb") -or ($_.Tags.Keys -eq "Do Not Disturb" -and $_.Tags.Values -eq "No") })
foreach($vm in $vm_list)
{
$vm_name=$vm.Name
Get-AzureRmVM -name $vm_name -Status -ResourceGroupName $ResourceGroupName | Select-Object -Property Name, Statuses|
ForEach-Object {
$_.Statuses |
Where-Object {$_.Code -like 'PowerState/*'} |
ForEach-Object {
New-Object -TypeName psobject -Property @{
Status = $_.DisplayStatus
}
if ( $_.DisplayStatus -eq "VM running" -And $(Get-AzureRmResource -ResourceGroupName "$ResourceGroupName" -Name $VMName).Tags.'Do Not Disturb' -eq "No" ){
#Stop-AzurermVM -ResourceGroupName "$ResourceGroupName" -Name $VMName -Force -ErrorAction SilentlyContinue
$processed_vm += $vm_name
}
}
}
}
}
write-output " List of VMs Stopped " |Out-File vmlist.txt
write-output "-----------------------" | Add-Content vmlist.txt
$processed_vm | Add-Content vmlist.txt
No comments:
Post a Comment