I have the following script and it runs without any errors however it doesnt change the value of LVM.DisallowSnapshotLun. Can anyone please let me know what I am missing? Thanks.
$VMHosts= Get-VMHost -State "Connected" { $VMHost = $VMHosts \ | Get-View $optmgrMoRef = $VMHost.configManager.advanceOption $optmgr = $optmgrMoRef \ | Get-View $optarray = $optmgr.QueryOptions("LVM.DisallowSnapshotLun") $optarray[0].Value = 0 $optmgr.UpdateOptions($optarray) } |
The script block that you want to execute for each "connected" vmHost needs to be in a foreach (alias %) loop.
There was a typo in $VMHost.configManager.advanceOption, that should be $VMHost.configManager.advancedOption.
To convert a MoRef you need to use the -Id parameter of the Get-View cmdlet. This can't be done via the pipe.
This should work.
Get-VMHost -State "Connected" | %{ $vmHost = $_ | Get-View $optmgrMoRef = $vmHost.configManager.advancedOption $optmgr = Get-View -Id $optmgrMoRef $optarray = $optmgr.QueryOptions("LVM.DisallowSnapshotLun") $optarray[0].Value = 0 $optmgr.UpdateOptions($optarray) }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
One last thing, I am trying to run a report on all of the hosts to gather there current LVM.DisallowSnapshotLun setting. I think I am real close however I cant get the value to transverse. Here is what I have so far.
$report = @()
$vmhosts = Get-VMHost -State "Connected"
foreach ($vmHost in $vmhosts){
$esxhost = $vmHost | Get-View
$optmgrMoRef = $esxhost.config.option | ? {$_.Key -eq "LVM.DisallowSnapshotLun"}
$row = "" | Select VMHost, Value
$row.vmhost = $vmhost
$row.value = $optmgrMoRef
$report += $row}
$report | Export-Csv .\lvminfo.csv -NoTypeInformation
Instead of storing the objects $vmhost and $optmgrMoRef in the $row variable, you should assign the actual properties in those objects.
Something like this:
$report = @() $vmhosts = Get-VMHost -State "Connected" foreach ($vmHost in $vmhosts){ $esxhost = $vmHost | Get-View $optmgrMoRef = $esxhost.config.option | ? {$_.Key -eq "LVM.DisallowSnapshotLun"} $row = "" | Select VMHost, Value $row.vmhost = $vmhost.Name $row.value = $optmgrMoRef.Value $report += $row } $report | Export-Csv ".\lvminfo.csv" -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference