VMware Cloud Community
BenLiebowitz
Expert
Expert
Jump to solution

Need help with RDM script to set them to Perennially Reserved

So I found this script to set the RDMs to Perennially Reserved, however, we have probably 15-20 VMs with RDMs so doing this manually can be tough.  I'd like to be able to have PowerCLI build a list of all the RDMs. 

I tried to build a foreach loop around the existing one one to detect all the RDMs from the multiple VMs using:  Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent but was unsuccessful. 

$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

  }

}

Any help would be appreciated! 

Thanks,

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$rdmHD = Get-VM | Get-HardDisk -DiskType RawPhysical,RawVirtual

foreach($rdm in $rdmHD){

  $esxcli = Get-EsxCli -VMHost $rdm.Parent.VMHost

  

  $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

View solution in original post

10 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$rdmHD = Get-VM | Get-HardDisk -DiskType RawPhysical,RawVirtual

foreach($rdm in $rdmHD){

  $esxcli = Get-EsxCli -VMHost $rdm.Parent.VMHost

  

  $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

BenLiebowitz
Expert
Expert
Jump to solution

Testing now..  Smiley Happy

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
Reply
0 Kudos
BenLiebowitz
Expert
Expert
Jump to solution

It worked! 

I modified one line as we're attempting to run this against only 1 host, and set the get-vm to a specific cluster. 

I changed the $esxcli= line to use a $vmhost variable which is set to a specific hostname.

$rdmHD = get-cluster "CLUSTER1" | Get-VM | Get-HardDisk -DiskType RawPhysical,RawVirtual

$vmhost = get-vmhost "VMHOST"

foreach($rdm in $rdmHD){

    $esxcli = Get-EsxCli -VMHost $vmhost

    $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

Thanks Luc!

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
tdewanjee
Contributor
Contributor
Jump to solution

I have couple of MSCS VM running on a cluster due to which I am having slow boot time and rescan issue on the host and plan to change the rdm lun to Perennially Reserved. Can we run this script on the cluster level for all the MSCS VM at once and does it need a downtime or we can change the value on LUN with the script without any downtime.

While testing the script I am getting below error

Get-VM : 9/11/2017 8:40:30 AM    Get-VM        Exception has been thrown by

the target of an invocation.

At C:\Users\0070tsa\Desktop\RDM_Pr.ps1:1 char:32

+ $rdmHD = get-cluster "LABVC" | Get-VM | Get-HardDisk -DiskType

RawPhysical,RawVi ...

+                                ~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-VM], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio

   n.ViCore.Cmdlets.Commands.GetVM

Thanks !

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does the following return anything?

Get-Cluster "LABVC" | Get-VM

And no, the change doesn't require any downtime.


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

Reply
0 Kudos
SparkRezaRafiee
Enthusiast
Enthusiast
Jump to solution

LucD's script is valid.

The below script is Cluster based. So need to provide a cluster name and then it finds the all RDM disks attached to all VMs in that cluster. Then it makes the RDM devices perenerrally reserved on

the ESXi hosts.

$ClusterName = "Cluster Name"

# Create a connection object to all hosts in the Target Cluster

$TargetCluster = Get-Cluster -Name $ClusterName | Get-VMHost

# Find the ScsiCanonicalName for all RDM Disks attached to VMs in the Target Cluster

$RDMDisks = Get-VM  | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select ScsiCanonicalName

foreach ($rdm2 in $RDMDisks){
$rdm2
}

# Retrieve and EsxCli instance for each connection

foreach($RDMDisk in $RDMDisks) {

foreach($hst in $TargetCluster) {
# And for each RDM Disk

$esxcli=Get-EsxCli -VMHost $Hst
# Set the configuration to "PereniallyReserved".

# setconfig method: void setconfig(boolean detached, string device, boolean perenniallyreserved)

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

}

}

# Disconnect the connection objects created for the Target Cluster

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure where your code basically differs from the previous code snippets in this thread?


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

Reply
0 Kudos
SparkRezaRafiee
Enthusiast
Enthusiast
Jump to solution

Your script is valid.

Just wanted to share the script that I have that does a similar thing ina different way.

I should have put a descriptive comment on my post. Let me edit it.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No need for that.
I was just wondering if I was missing something :smileygrin:


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

Reply
0 Kudos
SparkRezaRafiee
Enthusiast
Enthusiast
Jump to solution

Your scripts can't go wrong mate Smiley Happy

Reply
0 Kudos