Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This script will rename all the VMs hosted by the current host based
  2. # on their Nova instance GUID (stored within VM notes).
  3. #
  4. # If the VM is clustered, the according cluster group/resources will be
  5. # renamed as well.
  6.  
  7. function is_guid($string){
  8.     $pattern = "^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-" +
  9.                "[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-" +
  10.                "[0-9a-fA-F]{12}$"
  11.     $string -match $pattern
  12. }
  13.  
  14. write-host "Renaming Nova instances using their GUID."
  15.  
  16. $cluster = get-cluster -ErrorAction SilentlyContinue
  17.  
  18. if (!(cluster)){
  19.     write-host "Could not retrieve any failover cluster. "`
  20.                "Assuming the instances are not clustered."
  21. }
  22. else {
  23.     write-host "Retrieved failover cluster. Any cluster VM resources "`
  24.                "created by Nova will be renamed."
  25. }
  26.  
  27. $vms = get-vm
  28.  
  29. foreach($vm in $vms) {
  30.     $vmName = $vm.Name
  31.     $vmGUID = $vm.Notes
  32.  
  33.     # Nova stores the instance GUID within the instance notes field.
  34.     if (!(is_guid $vmGUID)) {
  35.         write-host "Skipping renaming instance '$vmName' as the "`
  36.                    "instance notes '$vmGUID' do not contain a GUID."`
  37.                    "Assuming it's not created by Nova."
  38.         continue
  39.     }
  40.  
  41.     # Check the VM name
  42.     if ($vmName -eq $vmGUID) {
  43.         write-host "Instance '$vmName' is already named using its GUID."
  44.     }
  45.     else {
  46.         write-host "Renaming Nova instance '$vmName' to '$vmGUID'."
  47.         set-vm $vm -NewVMName $vmGUID
  48.     }
  49.  
  50.     # Rename the VM cluster group/resources, if found.
  51.     if (!($cluster)) {
  52.         continue
  53.     }
  54.  
  55.     $group = get-clustergroup $vmName -ErrorAction SilentlyContinue
  56.     if ($group) {
  57.         write-host "Renaming cluster group '$vmName' to '$vmGUID'"
  58.         $group.Name = $vmGUID
  59.     }
  60.     else {
  61.         write-host "Could not find cluster group '$vmName'. "`
  62.                    "Assuming the instance is not clustered."
  63.         continue
  64.     }
  65.  
  66.     $vmClusResName = "Virtual Machine $vmName"
  67.     $vmConfigClusResName = "Virtual Machine Configuration $vmName"
  68.  
  69.     $newVmClusResName = "Virtual Machine $vmGUID"
  70.     $newVmConfigClusResName = "Virtual Machine Configuration $vmGUID"
  71.  
  72.     $vmClusRes = get-clusterresource $vmClusResName
  73.     if ($vmClusRes) {
  74.         write-host "Renaming cluster resource $vmClusResName to "`
  75.                    "$newVmClusResName"
  76.         $vmClusRes.Name = $newVmClusResName
  77.     }
  78.     else {
  79.         write-error "Could not find cluster resource $vmClusResName, "`
  80.                     "although the according cluster group was found."
  81.     }
  82.  
  83.     $vmConfigClusRes = get-clusterresource $vmConfigClusResName
  84.     if ($vmConfigClusRes) {
  85.         write-host "Renaming cluster resource $vmClusResName to "`
  86.                    "$newVmClusResName"
  87.         $vmConfigClusRes.Name = $newVmConfigClusResName
  88.     }
  89.     else {
  90.         write-error "Could not find cluster resource $vmConfigClusRes, "`
  91.                     "although the according cluster group was found."
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement