profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Saturday, December 9, 2017

Rename the vmware esxi data stores with custom names with powercli, powershell

These are to serve as tech notes for me and anyone who is starting up with powershell, powercli and showcase that how easy it is to automate. Once you know it becomes very trivial.
My team was building another VxRack.
challenge :
Each host had one datastore and that was local datastore. We needed to rename the local datastores with a <DIS>.<last octet of the ip address>. Here is the script to do so.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function renameDs {

clear
$clusters = "batman,superman,flash,wonderwoman"
$clusters = $clusters.Split(',')
    foreach ($cluster in $clusters) {
        $cluster
    }
} 
renameDs

lines:
  1. function name
  2. blank
  3. clear any previous operations output on screen
  4. put a list of clusters here. you can also do a read-host here and expect the user to enter the cluster names separated by a comma
  5. prepare a list of clusters which can be easily iterated over
  6. foreach loop
  7. print out the cluster name
now run just this and if it prints out the cluster name then it means our code is good so far.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function renameDs {

clear
$clusters = "batman,superman,flash,wonderwoman"
$clusters = $clusters.Split(',')
    foreach ($cluster in $clusters) {
        $cluster
        $vmhosts = get-cluster $cluster | get-vmhost
        foreach ($vmhost in $vmhosts) {
            $vmhost.Name
        }
    }
} 
renameDs

lines
8.foreach cluster we make a list of esxi hosts
9.foreach loop which will iterate through the list of esxi hosts that we prepared earlier
10.prints out the esxi host on which we are performing the operation


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function renameDs {

clear
$clusters = "batman,superman,flash,wonderwoman"
$clusters = $clusters.Split(',')
    foreach ($cluster in $clusters) {
        $cluster
        $vmhosts = get-cluster $cluster | get-vmhost
        foreach ($vmhost in $vmhosts) {
            $vmhost.Name
            $customName = "DIS"
            $mgmtIp = (Get-VMHost $vmhost | Get-VMHostNetworkAdapter  | Where-Object {$_.Name -eq "vmk0"}).IP 
            $mgmtIpLastOctet = $mgmtIp.Split('.')[-1]                   
            $newDsName = "$customName"+"$mgmtIpLastOctet"
            $datastore = (get-vmhost $vmhost | get-datastore)
            get-vmhost $vmhost | get-datastore $datastore.Name | Set-Datastore -Name $newDsName
        }
    }
} 
renameDs

11.custom string name which is common in the new name of all the datastores
12.gets the management ip address of the esxi
13.if the ip is 1.2.3.4 then this line gets the .4, the last octet
14.new datastore name = DIS.4 -->custom string+the last octet of the management ip address
15.get that datastore of the esxi host
16.rename the datastore.

Sharing is caring! get some good karma by being social and share now.




No comments:

Post a Comment