VMware Cloud Community
zare_N0222
Enthusiast
Enthusiast
Jump to solution

Power off VM which take place on certain datastore

Hi,

i trying but i don't get the desired result with my script.

We use NAS, SDN (over FC and ip) storage solutions.

My task is to shut down all VM  which use certain storage(datastore)

Get-VM  |  Select Name , powerstate , @{N =“Datastore”;E= {$_ |Get-Datastore | where{$_.Type -eq 'VMFS'} | Select Name, @{N='Stor_Dev';E={$_.ExtensionData.Info.Vmfs.Extent.DiskName -join ' | '}}}} | Out-GridView

with script I get all VM that use VMFS file system ( SAN and FC storage )  but I need to separate that,

when I get right result with command and do export to file than I try with script below:

foreach($vmlist in (Get-Content -Path C:\TEMP\vm_shutdown.txt))

{

$vm = Get-VM -Name $vm_shotdown

Shutdown-VMGuest -VM $vm -Confirm:$false

}

just to mention that some vm dont have installed vm-tools.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you check if the following returns the datastores that sit on LUNs with a canonicalname starting with 'naa'.
If it does, you would just need to add a Get-VM after the where-clause.

$esx = Get-VMHost -Name MyEsx

$canonical = Get-ScsiLun -VmHost $esx  -CanonicalName naa* | select -ExpandProperty CanonicalName

Get-Datastore -RelatedObject $esx  |

where{$canonical -contains $_.ExtensionData.Info.VMFS.Extent.DiskName}


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

View solution in original post

17 Replies
LucD
Leadership
Leadership
Jump to solution

You could turn this around start with the datastores of a specific type.

Then get the VMs on those datastores.

If the VMware Tools are running, do a graceful guest OS shutdown.

If not, stop the VM.

Get-Datastore | where{$_.Type -eq 'VMFS'} | Get-VM |

ForEach-Object -Process {

    if($_.Guest.GuestState -eq 'running'){

        Shutdown-VMGuest -VM $_ -Confirm:$false

    }

    else{

        Stop-VM -VM $_ -Confirm:$false

    }

}


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

ok, but how do i separate the vm they use FC storage from vm that use SDS storage?

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

i thought i was using the storage device name but it fails to make the correct script.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What do you mean by

with script I get all VM that use VMFS file system ( SAN and FC storage )  but I need to separate that,


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

There are two storages. One is connected through FC and the second one is connected over IP

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean FC vs FCoE?


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

yes

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should see different HBA for both.
What does this show?

Get-VMHostHba -Type FibreChannel


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

get-vmhost -Name 10.33.33.4  | Get-VMHostHba -Type FibreChannel

Device     Type         Model                          Status

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

vmhba2     FibreChannel Emulex LPe12000 8Gb PCIe Fi... online

vmhba3     FibreChannel Emulex LPe12000 8Gb PCIe Fi... online

vmhba64    FibreChannel                                online

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you check if the following returns the datastores that live on FCOE adapters?

Get-VMHost | ForEach-Object -Process {

    $esxcli = Get-EsxCli -VMHost $_ -v2

    $fcoeAdapters = $esxcli.storage.san.fcoe.list.Invoke() | Select -ExpandProperty Adapter

    $fcoeAdapters

    $fcoeDevices = $esxcli.storage.core.path.list.Invoke() | where{$fcoeAdapters -contains $_.Adapter} | Select -ExpandProperty Device

    $fcoeDevices

    $dsNames = $esxcli.storage.vmfs.extent.list.Invoke() | where{$fcoeDevices -contains $_.DeviceName} | Select -ExpandProperty VolumeName

    $dsNames

}


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

no , there is no error but also there is nothing to display

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That looks as if there are no FCoE adapters.
Or which of the 3 adapters you showed earlier are the FCoE adapters?


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

ok, I must start from begin .

There are six esxi hosts

Get-Datastore | where{$_.Type -eq 'NFS'} | Get-VM    -- all vms on NAS

Get-Datastore | where{$_.Type -eq 'VMFS'} | Get-VM  -- all VM on VMFS  , result is all VM that use two storages and I need to separate that output

with

Get-VMHost | Get-ScsiLun  -CanonicalName eui* | Out-GridView   - result all identifier for lun on one storage ( multiple 5 for each host )-   I need to get all vm and their state ,

Get-VMHost | Get-ScsiLun  -CanonicalName naa* | Out-GridView  - result all identifier for lun on second storage ( multiple 5 for each host )  I need to get all vm and their state ,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is becoming rather confusing.

So you have VMs that are located on both types of datastores?
What needs to be done with those VMs? Do they need an svMotion?

Is that the qualifying criterium?

LUN whose canonicalname starts with 'eui' are on which type of storage?

Same for LUNs whose canonicalname starts with 'naa'.

What exactly is the question now?
Do VMs on a specific type of datastore storage need to be stopped?


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

So you have VMs that are located on both types of datastores?   --- Yes
What needs to be done with those VMs? Do they need an svMotion?  to be shot down , svmotion -- NO, because leter on that vm will power on

Same for LUNs whose canonicalname starts with 'naa'   -- yes fc storage


Do VMs on a specific type of datastore storage need to be stopped? --  Yes

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you check if the following returns the datastores that sit on LUNs with a canonicalname starting with 'naa'.
If it does, you would just need to add a Get-VM after the where-clause.

$esx = Get-VMHost -Name MyEsx

$canonical = Get-ScsiLun -VmHost $esx  -CanonicalName naa* | select -ExpandProperty CanonicalName

Get-Datastore -RelatedObject $esx  |

where{$canonical -contains $_.ExtensionData.Info.VMFS.Extent.DiskName}


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

zare_N0222
Enthusiast
Enthusiast
Jump to solution

yes , that will do. Thanks LucD.

foreach($esx in Get-VMHost){

$canonical = Get-ScsiLun -VmHost $esx  -CanonicalName eui* | select -ExpandProperty CanonicalName

}

Get-Datastore -RelatedObject $esx 

where{$canonical -contains $_.ExtensionData.Info.VMFS.Extent.DiskName} | get-vm

0 Kudos