PowerCLI script to convert LUN canonical name to datastore and vice versa
#Thanks to vXav.fr
#Reference: https://www.vxav.fr/2016-10-13-convert-lun-canonical-name-to-datastore-and-vice-versa/
Function Convert-DSToCanonical
{
param(
[Parameter(Mandatory = $True,ValueFromPipeline=$True)]
[VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.DatastoreImpl[]]
$datastore
)
Process {
$datastore | where type -eq VMFS | ForEach-Object {
$CanonicalName = (Get-View $_).Info.Vmfs.Extent.diskname
[pscustomobject]@{
CanonicalName = $CanonicalName
Datastore = $_.name
}
}
}
}
Function Convert-CanonicalToDS {
param(
[Parameter(Mandatory = $True,ValueFromPipeline=$True)]
[string[]]
$canonicalname
)
Begin {
$Table = Convert-DSToCanonical (get-datastore | where type -eq VMFS)
}
Process{
$canonicalname | ForEach-Object {
$Table | where CanonicalName -eq $_
}
}
}
#Script execution format
#cd C:\Temp\CanonicalToDSToCanonical\
#./NaaToDS.ps1 vcenterserver to@mailaddress.com
$VCenter=$args[0]
$Mail=$args[1]
Connect-VIServer $VCenter
$results=@()
$OutFile = "C:\Temp\CanonicalToDSToCanonical\Output.csv"
$identifiers = Get-Content C:\Temp\CanonicalToDSToCanonical\naa_list.txt
foreach($identifier in $identifiers)
{
$ds = $identifier | Convert-CanonicalToDS
$esx=get-datastore $ds.Datastore | get-vmhost
$ESXiHost = $esx -join ", "
$vm=get-datastore $ds.Datastore | Get-VM
$VMName = $vm.Name -join ", "
$LUNsMapped = $ds.CanonicalName -join ", "
$result = new-object -TypeName psobject
$result | Add-Member -MemberType NoteProperty -Name 'Identifier' -Value $identifier
$result | Add-Member -MemberType NoteProperty -Name 'Datastore' -Value $ds.Datastore
$result | Add-Member -MemberType NoteProperty -Name 'ESXiHost' -Value $ESXiHost
$result | Add-Member -MemberType NoteProperty -Name 'VMName' -Value $VMName
$result | Add-Member -MemberType NoteProperty -Name 'LUNsMappedToDatastore' -Value $LUNsMapped
$results += $result
}
$results|select Identifier,Datastore,ESXiHost,VMName,LUNsMappedToDatastore | Export-Csv $OutFile -noTypeInformation
send-mailmessage -Attachments "C:\Temp\CanonicalToDSToCanonical\Output.csv" -to $args[1] -from "from@mailaddress.com" -subject "Naa Identifier to Datastore Conversion" -SmtpServer "smtp.mailaddress.com"
Disconnect-VIServer $VCenter -Confirm:$false
No comments:
Post a Comment