VMware Cloud Community
Ryzz
Enthusiast
Enthusiast
Jump to solution

Assistance with scrip to bulk set Perennially Reservations using Get-ESXCLi

I was hoping for a little help here. Ive written a script that Should set a lun to "Perennially Reserved" using Powercli & Get-ESXICli, however when I run the following command locally on a host to confirm this worked and luns are tagged respectively, i find that the output is False

esxli storage core device list | grep reserved

The script ive written is below. Im aware their are easier ways to obtain the identifiers and get host names, etc. However im more concerned about the actual action of the script in setting the reservation.

Param(

  $RootCredentials = (Get-Credential)

)

$RDMDisks = ("naa.60002ac0000000000000003e00002b3e","naa.60002ac0000000000000002800002b3e","naa.60002ac0000000000000003f00002b3e","naa.60002ac0000000000000002b00002b3e","naa.60002ac0000000000000002d00002b3e","naa.60002ac0000000000000002f00002b3e","naa.60002ac0000000000000003100002b3e","naa.60002ac0000000000000003500002b3e","naa.60002ac0000000000000003600002b3e","naa.60002ac0000000000000003700002b3e","naa.60002ac0000000000000003800002b3e","naa.60002ac0000000000000003900002b3e","naa.60002ac0000000000000004100002b3e","naa.60002ac0000000000000003b00002b3e","naa.60002ac0000000000000002600002b3e","naa.60002ac0000000000000002700002b3e","naa.60002ac0000000000000002900002b3e","naa.60002ac0000000000000002a00002b3e","naa.60002ac0000000000000002c00002b3e","naa.60002ac0000000000000002e00002b3e","naa.60002ac0000000000000003000002b3e","naa.60002ac0000000000000003300002b3e","naa.60002ac0000000000000004000002b3e","naa.60002ac0000000000000003c00002b3e","naa.60002ac0000000000000003200002b3e","naa.60002ac0000000000000003400002b3e")

$TargetHosts = ("Server190","Server191","Server192","Server193","Server194","Server195","Server196")

Foreach ($ESXHost in $TargetHosts) {

Connect-VIServer $ESXHost -Credential $RootCredentials

}    

Start-Sleep -Seconds 3

Foreach($esxcli in Get-ESXCli) {

                                      Foreach ($ESXHost in $TargetHosts){

                                                      Foreach ($Disk in $RDMDisks) {

                                                                 $esxcli.storage.core.device.setconfig($false, $Disk, $true)

                                               }

                                   }

                           }

Foreach ($ESXHost in $TargetHosts) {

Disconnect-VIServer $ESXHost -Confirm:$false |Out-Null

}

Thanks in advance for any help.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To set the flag on all ESXi nodes in a cluster, the script needs a small change

$vmName = "VMwithRDM"

$vm = Get-VM -Name $vmName
$rdm = Get-HardDisk -DiskType rawPhysical -Vm $vm
$vmhosts = Get-Cluster -VM $vm | Get-VMHost
foreach($esx in $vmhosts){
 
$esxcli = Get-EsxCli -VMHost $esx
 
Get-HardDisk -DiskType rawPhysical -Vm $vm | %{
   
$esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |
   
Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved

   
$esxcli.storage.core.device.setconfig($false,$rdm.ScsiCanonicalName,$true)

   
$esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |
   
Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved
  }
}

The logic is now as follows:

  1. Get the physical RDM(s) connected to the VM (if your RDM is virtual, you will need to change the DiskType, or add both to the parameter)
  2. Get all the ESXi nodes of the cluster where the VM is configured, and run the following codeblock for each node
    1. List the current flag setting for the RDM LUN
    2. Change the flag
    3. List the current flag setting for the RDM LUN

See Some ways to enter PowerCLI code under the new forum SW on how to enter syntax colouring.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
20 Replies
Ryzz
Enthusiast
Enthusiast
Jump to solution

Bump! - Anyone able to offer any help here please?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There shouldn't be a need to connect to each ESXi individually when you want to use Get-EsxCli.

In the latest PowerCLI builds you can just specify the VMHost.

The following is an example on how to set the physical RDMs of a specific VM to perennially reserved.

When I run this I do not get a Boolean value returned.

Try this, and check if you get any Boolean value(s) back ?

$vmName = "VMwithRDM"

$vm = Get-VM -Name $vmName
$rdm = Get-HardDisk -DiskType rawPhysical -Vm $vm
$esx = Get-VMHost -VM $vm
$esxcli = Get-EsxCli -VMHost $esx
Get-HardDisk -DiskType rawPhysical -Vm $vm | %{
 
$esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |
 
Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved
 
 
$esxcli.storage.core.device.setconfig($false,$rdm.ScsiCanonicalName,$true)
 
 
$esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |
 
Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Ryzz
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

This is the output I get from running the above script.

Time                                        Device                                     IsPerenniallyReserved

----                                        ------                                     ---------------------

15:34:36.6497446                            naa.60002ac0000000000000002900002b3e       true

15:34:36.6497446                            naa.60002ac0000000000000002d00002b3e       true

15:34:36.6653447                            naa.60002ac0000000000000004000002b3e       true

15:34:36.6653447                            naa.60002ac0000000000000003700002b3e       true

15:34:36.6653447                            naa.600508b1001c6d5636ad49da9766e488       false

15:34:36.6653447                            naa.60002ac0000000000000003b00002b3e       true

15:34:36.6653447                            naa.60002ac0000000000000002300002b3e       false

15:34:36.6653447                            naa.50002ac39fb60cf4                       false

I'll apologise in advance, but i'm a little lost here with your script.

If i understand the logic correctly, the script does the following:

1. Checks the VM for any Physical RDM's

2. Identifies the VMHost the virtual machine is running on

3. Starts a loop, displays current state of all reservations, set reservations specific to this vm, displays all reservations again.

Im assuming this script only sets/check the reservation on the host that guest currently runs on, however every host with visibility of this lun needs the reservation set, how would you modify this script to run across all relevant hosts, ie within a cluster? could you get all hosts in the cluster based on the VM Name?

If i run something like

Get-Cluster -VM "NAME OF VM"

This will give me the name of the cluster, it doesnt give me the name of the hosts in the cluster. Would the following work instead

$Cluster = Get-Cluster -VM "Name of VM"

$EsxHosts = Get-Cluster $Cluster

foreach ($Esx in $EsxHosts) {

     $esxcli = get-EsxCli -VMHost $ESX

Get-HardDisk -DiskType rawPhysical -Vm $vm | %{

       $esxcli.storage.core.device.list($rdm.ScsiCanonicalName

          $esxcli.storage.core.device.setconfig($false,$rdm.ScsiCanonicalName,$true)

     }

}

Thanks again for your help.

P.s: How do you paste your script with the relevant syntax colouring, when i paste from Notepad++ its vanilla text.

Message was edited by: Ryzz

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To set the flag on all ESXi nodes in a cluster, the script needs a small change

$vmName = "VMwithRDM"

$vm = Get-VM -Name $vmName
$rdm = Get-HardDisk -DiskType rawPhysical -Vm $vm
$vmhosts = Get-Cluster -VM $vm | Get-VMHost
foreach($esx in $vmhosts){
 
$esxcli = Get-EsxCli -VMHost $esx
 
Get-HardDisk -DiskType rawPhysical -Vm $vm | %{
   
$esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |
   
Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved

   
$esxcli.storage.core.device.setconfig($false,$rdm.ScsiCanonicalName,$true)

   
$esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |
   
Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved
  }
}

The logic is now as follows:

  1. Get the physical RDM(s) connected to the VM (if your RDM is virtual, you will need to change the DiskType, or add both to the parameter)
  2. Get all the ESXi nodes of the cluster where the VM is configured, and run the following codeblock for each node
    1. List the current flag setting for the RDM LUN
    2. Change the flag
    3. List the current flag setting for the RDM LUN

See Some ways to enter PowerCLI code under the new forum SW on how to enter syntax colouring.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Ryzz
Enthusiast
Enthusiast
Jump to solution

Thanks LucD, really appreciate your help with this one. Im still fairly new with PowerCli and working hard to get my head around it, so helping explain the logic along the way makes a big difference.

Thanks again.
Ryan

Reply
0 Kudos
tdewanjee
Contributor
Contributor
Jump to solution

Does running Perennially Reservation on multiple VM via script require any downtime for the MSCS VM or it can be run without any impact and it will change the value to true.?

Reply
0 Kudos
jasonrobinson
Enthusiast
Enthusiast
Jump to solution

No downtime is required, there is no impact to the running MSCS VMs. We have performed this in our environment multiple times under the guidance of VMware support.

Jason @jrob24
Reply
0 Kudos
hamzasezer5
Contributor
Contributor
Jump to solution

Hi Luc,

I also tried to set perennial-reserved=true flag to the rdm disk that belongs to virtual machine on all hosts in that cluster. When i tried to use your script, i got the error below. I think it is related with the get-esxcli usage. It does not return the elements of command. I really appreciated if you help me. Thanks

PS C:\Users\> $esxcli = Get-EsxCli -VMHost $esx -V2

PS C:\Users\> $esxcli

===================

EsxCli: 10.x.x.x

No elements here

Error:

You cannot call a method on a null-valued expression.

+     $esxcli.storage.core.device.setconfig($false,$rdm.ScsiCanonicalNa ...

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

Regards

Hamza

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you use the V2 switch, the way to call the methods and pass the arguments is different.

Have a read through PowerCLI 6.3 R1: Get-ESXCLI Why the V2?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
hamzasezer5
Contributor
Contributor
Jump to solution

Hi Luc,

Actually the same without V2 parameter, i also tried with this and same result. What do you suggest about Get-EsxCli command? I think this is the problem why script gives this error. The value returns none elements. On blogs i saw the elements should appear when you check the $esxcli. I can not find the reason of that.

$vmName = "myvm"

$vm = Get-VM -Name $vmName

$rdm = Get-HardDisk -DiskType rawPhysical -Vm $vm

$vmhosts = Get-Cluster -VM $vm | Get-VMHost

foreach($esx in $vmhosts){

  $esxcli = Get-EsxCli -VMHost $esx

  Get-HardDisk -DiskType rawPhysical -Vm $vm | %{

    $esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |

    Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved

    $esxcli.storage.core.device.setconfig($false,$rdm.ScsiCanonicalName,$true)

    $esxcli.storage.core.device.list($rdm.ScsiCanonicalName) |

    Select @{N="Time";E={"{0:g}" -f (Get-Date).TimeOfDay}},Device,IsPerenniallyReserved

  }

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you saying $esxcli is empty after the Get-EsxCli cmdlet?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
hamzasezer5
Contributor
Contributor
Jump to solution

Yes it is empty. My hosts are 6.5.0.

PS C:\Users\t>  $esxcli = Get-EsxCli -VMHost $esx

PS C:\Users\t>  $esxcli

===================

EsxCli: 10.x.x.x

PS C:\Users\tasksch.000> $PSVersionTable

Name                           Value                                                                                                                                                                                

----                           -----                                                                                                                                                                                

PSVersion                      5.1.14409.1005                                                                                                                                                                       

PSEdition                      Desktop                                                                                                                                                                              

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                              

BuildVersion                   10.0.14409.1005                                                                                                                                                                      

CLRVersion                     4.0.30319.42000                                                                                                                                                                      

WSManStackVersion              3.0                                                                                                                                                                                  

PSRemotingProtocolVersion      2.3                                                                                                                                                                                  

SerializationVersion           1.1.0.1

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The $esxcli variable is not empty, it seems to show your ESXi node (EsxCli: 10.x.x.x).

But normally it should also show the elements

======================

EsxCli: esx12.local.lab

  

   Elements:

   ---------

   device

   esxcli

   fcoe

   graphics

   hardware

   iscsi

   network

   nvme

   rdma

   sched

   software

   storage

   system

   vm

   vsan

Are you connected to a vCenter?
Which PowerCLI version are you using?
Do other PowerCLI cmdlets on that ESXi node work? The New-VM cmdlet for example?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
hamzasezer5
Contributor
Contributor
Jump to solution

I am connected to Vcenter, other cmdlets are working (Get-Cluster, Get-VMhost, New-VM..etc). Get-EsxCli is the same for all hosts, i also tried on other hosts. It does not bring the elements as you show. I tried with PowerCLI version 6.3, 6.5 and finally i upgraded to 12.0 latest version. But it is the same. I also search on Google but nothing found.

Is another way possible to set perennial-reserved flag to true, another cmdlet maybe?

PowerCLI Version

----------------

   VMware PowerCLI 12.0.0 build 15947286

PS C:\Users\t> New-VM

cmdlet New-VM at command pipeline position 1

Supply values for the following parameters:

Name:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can always do an SSH session to the ESXi node and run the esxcli command on there.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
hamzasezer5
Contributor
Contributor
Jump to solution

Actually this is the method i am using now but for big clusters it is really hard to do it one-by-one. I mean another cmdlet in powercli to set the flag.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

As an additional test, after you do the Get-EsxCli, can you display what is in $esxcli.VMHost?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can script that SSH session and the command as well.

See for example my Use Posh-SSH instead of PuTTY


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
hamzasezer5
Contributor
Contributor
Jump to solution

the output is like below.

PS C:\Users\t> $esxcli.VMHost

Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Version

----                 --------------- ---------- ------ ----------- -----------   -------------   ------------- -------

10.x.x.64           Connected       PoweredOn      64       24357      134016         325,773         511,879   6.5.0

Reply
0 Kudos