Skip to content

Instantly share code, notes, and snippets.

@9to5IT
Last active August 16, 2021 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 9to5IT/d67570b115d4f641b30a to your computer and use it in GitHub Desktop.
Save 9to5IT/d67570b115d4f641b30a to your computer and use it in GitHub Desktop.
PowerShell: Searching for RDM disks using PowerCLI
#requires -version 4
<#
.SYNOPSIS
Seaches for an RDM with the specified LUN ID.
.DESCRIPTION
Searches the vCenter environment to find the Virtual Machine that has an RDM configured with a particular LUN ID.
.PARAMETER None
.INPUTS Server
Mandatory. The vCenter Server or ESXi Host the script will connect to, in the format of IP address or FQDN.
.INPUTS Credentials
Mandatory. The user account credendials used to connect to the vCenter Server of ESXi Host.
.INPUTS LUN ID
Optional. The LUN ID of the RDM you want to search for.
If no LUN ID is specified, the script will retun all VMs with RDMs and their relevant LUN IDs.
.OUTPUTS Log File
The script log file stored in C:\Windows\Temp\Get-RDM.log.
.NOTES
Version: 1.0
Author: Luca Sturlese
Creation Date: 12.06.2015
Purpose/Change: Initial script development
Version: 1.1
Author: Luca Sturlese
Creation Date: 10.07.2015
Purpose/Change: Added Disk Capacity and Disk Identifier information
Version: 1.2
Author: Luca Sturlese
Creation Date: 18.10.2015
Purpose/Change: Updated to new version of template including using PSLogging Module
.EXAMPLE
Get-RDM.ps1
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = 'SilentlyContinue'
#Import PSLogging Module
Import-Module PSLogging
#Add VMware PowerCLI Snap-Ins
Add-PSSnapin VMware.VimAutomation.Core
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = '1.2'
#Log File Info
$sLogPath = 'C:\Windows\Temp'
$sLogName = 'Get-RDM.log'
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#-----------------------------------------------------------[Functions]------------------------------------------------------------
Function Connect-VMwareServer {
Param ([Parameter(Mandatory=$true)][string]$VMServer)
Begin {
Write-LogInfo -LogPath $sLogFile -Message "Connecting to VMware environment [$VMServer]..."
}
Process {
Try {
$oCred = Get-Credential -Message 'Enter credentials to connect to vSphere Server or Host'
Connect-VIServer -Server $VMServer -Credential $oCred
}
Catch {
Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
Break
}
}
End {
If ($?) {
Write-LogInfo -LogPath $sLogFile -Message 'Completed Successfully.'
Write-LogInfo -LogPath $sLogFile -Message ' '
}
}
}
Function Find-Data {
Param( [Parameter( Mandatory=$true )][string]$LunID )
Begin {
Write-LogInfo -LogPath $sLogFile -Message 'Finding RDM(s) based on script mode. Results are below...'
}
Process {
Try {
#Find single RDM or all RDMs (based on if LUN ID has been specified or not)
If( $LunID.Trim() -eq 'All' -or [string]::IsNullOrEmpty( $LunID.Trim() ) ){
$sType = 'All'
Write-LogInfo -LogPath $sLogFile -Message 'Process Mode: All RDMs'
} Else {
$sType = 'Single'
Write-LogInfo -LogPath $sLogFile -Message "Process Mode: Single RDM (LUN ID: $LunID)"
}
#Get list of all VMs within the environment
$VMs = Get-VM
Write-LogInfo -LogPath $sLogFile -Message ' '
#Enumeate all VMs to find ones with RDMs
ForEach( $VM in $VMs ) {
$Disks = Get-VM $VM | Get-HardDisk | Where-Object { $_.DiskType -eq 'RawPhysical' }
ForEach( $Disk in $Disks ) {
$DiskLun = Get-SCSILun $Disk.SCSICanonicalName -VMHost (Get-VM $VM).VMHost
$DiskLunID = $DiskLun.RuntimeName.Substring($DiskLun.RuntimeName.LastIndexof('L') + 1)
If( $sType -eq 'All' ) {
#List all RDMs
Write-LogInfo -LogPath $sLogFile -Message "$VM - LUN ID: $DiskLunID, Capacity: $($Disk.CapacityGB) GB, Identifier: $($Disk.ScsiCanonicalName)"
} Else {
#List only RDM with the correct LUN ID (do not exit in case multiple disks with the same LUN ID - e.g. from different storage arrays)
If( $DiskLunID -eq $LunID ) {
Write-LogInfo -LogPath $sLogFile -Message "$VM - LUN ID: $DiskLunID, Capacity: $($Disk.CapacityGB) GB, Identifier: $($Disk.ScsiCanonicalName)"
}
}
}
}
Write-LogInfo -LogPath $sLogFile -Message ' '
}
Catch {
Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
Break
}
}
End {
If($?) {
Write-LogInfo -LogPath $sLogFile -Message ' '
Write-LogInfo -LogPath $sLogFile -Message 'Completed Successfully.'
Write-LogInfo -LogPath $sLogFile -Message ' '
}
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Start-Log -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
$Server = Read-Host 'Specify the vCenter Server or ESXi Host to connect to (IP or FQDN)?'
Connect-VMwareServer -VMServer $Server
$LunID = Read-Host 'Specify the LUN ID of the RDM or alternatively enter "All" to return all details of all RDMs within the environment?'
Find-Data -LunID $LunID
Stop-Log -LogPath $sLogFile
@cajunitalian
Copy link

Line 117 should be enumerate. However, awesome stuff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment