Advertisement
newguyneal

AzureRM-QueryVMPlannedFabricMaintenceExample.ps1

Jan 3rd, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. This script is intended to be recieved as an informational bit of code.
  3. If used, it is with the understanding that the user has a complete understanding of the code and has made any
  4. necessary changes that are required for their respective environment.
  5. With this script and any others you find on the internet go through each line and test for yourself before EVER running
  6. in production.
  7. #>
  8. Param(
  9.     [Parameter(Mandatory,HelpMessage = "Location of CSV with list of VM Details that maintenance needs to be performed on")]
  10.     [string]$CSVPath
  11. )
  12.  
  13. Write-Host "Logging Into Azure"
  14. #login to azure account
  15. Add-AzureRmAccount
  16.  
  17. #Query Azure for All VMs in the current context
  18. $VMs = Get-AzureRmVM -Status
  19.  
  20. $AllVMDetails = [System.Collections.ArrayList]@()
  21.  
  22. #Enumerate Through all Azure VMs and Flatten important details of the object
  23. foreach($VM in $VMs)
  24. {
  25.     $SubscriptionID = $VM.ID.split("/")[2]
  26.     #Create a new object to flatten data
  27.     $VMDetails = [PSCustomObject]@{
  28.                     Name                                  = $VM.Name
  29.                     ResourceGroupName                     = $vm.ResourceGroupName
  30.                     Location                              = $VM.Location
  31.                     SubscriptionID                        = $SubscriptionID
  32.                     IsCustomerInitiatedMaintenanceAllowed = $vm.MaintenanceRedeployStatus.IsCustomerInitiatedMaintenanceAllowed
  33.                     PreMaintenanceWindowStartTime         = $vm.MaintenanceRedeployStatus.PreMaintenanceWindowStartTime
  34.                     PreMaintenanceWindowEndTime           = $vm.MaintenanceRedeployStatus.PreMaintenanceWindowEndTime
  35.                     MaintenanceWindowStartTime            = $vm.MaintenanceRedeployStatus.MaintenanceWindowStartTime
  36.                     MaintenanceWindowEndTime              = $vm.MaintenanceRedeployStatus.MaintenanceWindowEndTime
  37.                     LastOperationResultCode               = $vm.MaintenanceRedeployStatus.LastOperationResultCode
  38.                     LastOperationMessage                  = $vm.MaintenanceRedeployStatus.LastOperationMessage
  39.                 }
  40.     $null = $AllVMDetails.add($VMDetails)
  41. }
  42.  
  43. #Export VM Details of VM to CSVPath
  44. $AllVMDetails | Export-Csv $CSVPath -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement