From acb0383f1683a05de967a415de76247b558c1e3d Mon Sep 17 00:00:00 2001 From: NamedJason Date: Tue, 16 May 2017 08:19:00 -0700 Subject: [PATCH] Create DatastoreFunctions.psm1 --- Modules/DatastoreFunctions.psm1 | 184 ++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 Modules/DatastoreFunctions.psm1 diff --git a/Modules/DatastoreFunctions.psm1 b/Modules/DatastoreFunctions.psm1 new file mode 100644 index 0000000..e417112 --- /dev/null +++ b/Modules/DatastoreFunctions.psm1 @@ -0,0 +1,184 @@ +#Created by Alan Renouf, published at https://communities.vmware.com/docs/DOC-18008 +Function Get-DatastoreMountInfo { + [CmdletBinding()] + Param ( + [Parameter(ValueFromPipeline=$true)] + $Datastore + ) + Process { + $AllInfo = @() + if (-not $Datastore) { + $Datastore = Get-Datastore + } + Foreach ($ds in $Datastore) { + if ($ds.ExtensionData.info.Vmfs) { + $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].diskname + if ($ds.ExtensionData.Host) { + $attachedHosts = $ds.ExtensionData.Host + Foreach ($VMHost in $attachedHosts) { + $hostview = Get-View $VMHost.Key + $hostviewDSState = $VMHost.MountInfo.Mounted + $StorageSys = Get-View $HostView.ConfigManager.StorageSystem + $devices = $StorageSys.StorageDeviceInfo.ScsiLun + Foreach ($device in $devices) { + $Info = "" | Select Datastore, VMHost, Lun, Mounted, State + if ($device.canonicalName -eq $hostviewDSDiskName) { + $hostviewDSAttachState = "" + if ($device.operationalState[0] -eq "ok") { + $hostviewDSAttachState = "Attached" + } elseif ($device.operationalState[0] -eq "off") { + $hostviewDSAttachState = "Detached" + } else { + $hostviewDSAttachState = $device.operationalstate[0] + } + $Info.Datastore = $ds.Name + $Info.Lun = $hostviewDSDiskName + $Info.VMHost = $hostview.Name + $Info.Mounted = $HostViewDSState + $Info.State = $hostviewDSAttachState + $AllInfo += $Info + } + } + + } + } + } + } + $AllInfo + } +} + +Function Detach-Datastore { + [CmdletBinding()] + Param ( + [Parameter(ValueFromPipeline=$true)] + $Datastore + ) + Process { + if (-not $Datastore) { + Write-Host "No Datastore defined as input" + Exit + } + Foreach ($ds in $Datastore) { + $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname + if ($ds.ExtensionData.Host) { + $attachedHosts = $ds.ExtensionData.Host + Foreach ($VMHost in $attachedHosts) { + $hostview = Get-View $VMHost.Key + $StorageSys = Get-View $HostView.ConfigManager.StorageSystem + $devices = $StorageSys.StorageDeviceInfo.ScsiLun + Foreach ($device in $devices) { + if ($device.canonicalName -eq $hostviewDSDiskName) { + $LunUUID = $Device.Uuid + Write-Host "Detaching LUN $($Device.CanonicalName) from host $($hostview.Name)..." + $StorageSys.DetachScsiLun($LunUUID); + } + } + } + } + } + } +} + +Function Unmount-Datastore { + [CmdletBinding()] + Param ( + [Parameter(ValueFromPipeline=$true)] + $Datastore + ) + Process { + if (-not $Datastore) { + Write-Host "No Datastore defined as input" + Exit + } + Foreach ($ds in $Datastore) { + $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname + if ($ds.ExtensionData.Host) { + $attachedHosts = $ds.ExtensionData.Host + Foreach ($VMHost in $attachedHosts) { + $hostview = Get-View $VMHost.Key + $StorageSys = Get-View $HostView.ConfigManager.StorageSystem + Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..." + $StorageSys.UnmountVmfsVolume($DS.ExtensionData.Info.vmfs.uuid); + } + } + } + } +} + +Function Mount-Datastore { + [CmdletBinding()] + Param ( + [Parameter(ValueFromPipeline=$true)] + $Datastore + ) + Process { + if (-not $Datastore) { + Write-Host "No Datastore defined as input" + Exit + } + Foreach ($ds in $Datastore) { + $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname + if ($ds.ExtensionData.Host) { + $attachedHosts = $ds.ExtensionData.Host + Foreach ($VMHost in $attachedHosts) { + $hostview = Get-View $VMHost.Key + $StorageSys = Get-View $HostView.ConfigManager.StorageSystem + Write-Host "Mounting VMFS Datastore $($DS.Name) on host $($hostview.Name)..." + $StorageSys.MountVmfsVolume($DS.ExtensionData.Info.vmfs.uuid); + } + } + } + } +} + +Function Attach-Datastore { + [CmdletBinding()] + Param ( + [Parameter(ValueFromPipeline=$true)] + $Datastore + ) + Process { + if (-not $Datastore) { + Write-Host "No Datastore defined as input" + Exit + } + Foreach ($ds in $Datastore) { + $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname + if ($ds.ExtensionData.Host) { + $attachedHosts = $ds.ExtensionData.Host + Foreach ($VMHost in $attachedHosts) { + $hostview = Get-View $VMHost.Key + $StorageSys = Get-View $HostView.ConfigManager.StorageSystem + $devices = $StorageSys.StorageDeviceInfo.ScsiLun + Foreach ($device in $devices) { + if ($device.canonicalName -eq $hostviewDSDiskName) { + $LunUUID = $Device.Uuid + Write-Host "Attaching LUN $($Device.CanonicalName) to host $($hostview.Name)..." + $StorageSys.AttachScsiLun($LunUUID); + } + } + } + } + } + } +} +# +#Get-Datastore | Get-DatastoreMountInfo | Sort Datastore, VMHost | FT -AutoSize +# +#Get-Datastore IX2ISCSI01 | Unmount-Datastore +# +#Get-Datastore IX2ISCSI01 | Get-DatastoreMountInfo | Sort Datastore, VMHost | FT -AutoSize +# +#Get-Datastore IX2iSCSI01 | Mount-Datastore +# +#Get-Datastore IX2iSCSI01 | Get-DatastoreMountInfo | Sort Datastore, VMHost | FT -AutoSize +# +#Get-Datastore IX2iSCSI01 | Detach-Datastore +# +#Get-Datastore IX2iSCSI01 | Get-DatastoreMountInfo | Sort Datastore, VMHost | FT -AutoSize +# +#Get-Datastore IX2iSCSI01 | Attach-datastore +# +#Get-Datastore IX2iSCSI01 | Get-DatastoreMountInfo | Sort Datastore, VMHost | FT -AutoSize +#