I'm trying to add a line to this script and the line should also show VM's, cluster and how much free and used space on the LUN's. Also I will like the script to give me the name of the datastores.
$report = foreach($esx in Get-VMHost){
foreach($hbaKey in ($esx.Extensiondata.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -like "*FibreChannelHba*"})){
if($hbaKey.Target){
foreach($tgtKey in $hbaKey.Target){
foreach($lunKey in $tgtKey.Lun){
$hba = $esx.Extensiondata.Config.StorageDevice.HostBusAdapter | where {$_.Key -eq $hbaKey.Adapter}
$lun = $esx.Extensiondata.Config.StorageDevice.ScsiLun | where {$_.Key -eq $lunKey.ScsiLun}
Select-Object -InputObject $lun -Property @{N="Host";E={$esx.Name}},
@{N="HBA";E={$hba.Device}},
CanonicalName,DisplayName,@{N="LUN";E={$lunKey.Lun}}
}
}
}
}
}
$report | Export-Csv "C:\temp\lun-report.csv" -NoTypeInformation -UseCulture
Hi,
The attached below would do your job, bold and italic lines are the ones I've added in:
$report = foreach($esx in Get-VMHost){
foreach($hbaKey in ($esx.Extensiondata.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -like "*FibreChannelHba*"})){
if($hbaKey.Target){
foreach($tgtKey in $hbaKey.Target){
foreach($lunKey in $tgtKey.Lun){
$hba = $esx.Extensiondata.Config.StorageDevice.HostBusAdapter | where {$_.Key -eq $hbaKey.Adapter}
$lun = $esx.Extensiondata.Config.StorageDevice.ScsiLun | where {$_.Key -eq $lunKey.ScsiLun}
$datastore = $esx | Get-Datastore | Where-Object {(%{$_.ExtensionData.Info.Vmfs.Extent} | %{$_.DiskName}) -match $lun.CanonicalName}
Select-Object -InputObject $lun -Property @{N="Cluster";E={Get-Cluster -VMHost $esx | %{$_.Name}}},@{N="Host";E={$esx.Name}},@{N="Datastore";E={$datastore | %{$_.Name}}},@{N="FreeSpace";E={$datastore | %{$_.FreeSpaceGB}}},@{N="Capacity";E={$datastore | %{$_.CapacityGB}}},@{N="Virtual Machines";E={[string]::Join(",", (Get-VM -Datastore $datastore))}},
@{N="HBA";E={$hba.Device}},
CanonicalName,DisplayName,@{N="LUN";E={$lunKey.Lun}}
}
}
}
}
}
$report | Export-Csv "C:\temp\lun-report.csv" -NoTypeInformation -UseCulture
Thanks Skan070.
Hi Skan070,
Can we add used space to the script? I left that out.
Thanks,
Here you go: $report = foreach($esx in Get-VMHost){ foreach($hbaKey in ($esx.Extensiondata.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -like "*FibreChannelHba*"})){ if($hbaKey.Target){ foreach($tgtKey in $hbaKey.Target){ foreach($lunKey in $tgtKey.Lun){ $hba = $esx.Extensiondata.Config.StorageDevice.HostBusAdapter | where {$_.Key -eq $hbaKey.Adapter} $lun = $esx.Extensiondata.Config.StorageDevice.ScsiLun | where {$_.Key -eq $lunKey.ScsiLun} $datastore = $esx | Get-Datastore | Where-Object {(%{$_.ExtensionData.Info.Vmfs.Extent} | %{$_.DiskName}) -match $lun.CanonicalName} $freespace = $datastore | %{$_.FreeSpaceGB} $capacity = $datastore | %{$_.CapacityGB} Select-Object -InputObject $lun -Property @{N="Cluster";E={Get-Cluster -VMHost $esx | %{$_.Name}}},@{N="Host";E={$esx.Name}},@{N="Datastore";E={$datastore | %{$_.Name}}},@{N="Free Space (GB)";E={$freespace}},@{N="Capacity (GB)";E={$capacity}},@{N="Used Space (GB)";E={$capacity - $freespace}},@{N="Virtual Machines";E={[string]::Join(",", (Get-VM -Datastore $datastore))}}, @{N="HBA";E={$hba.Device}}, CanonicalName,DisplayName,@{N="LUN";E={$lunKey.Lun}} } } } } } $report | Export-Csv "C:\temp\lun-report.csv" -NoTypeInformation -UseCulture
It errored out. Below is the original one without the "Used Space Comment" Maybe there was a typo when you made the changes.
$report = foreach($esx in Get-VMHost){
foreach($hbaKey in ($esx.Extensiondata.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -like "*FibreChannelHba*"})){
if($hbaKey.Target){
foreach($tgtKey in $hbaKey.Target){
foreach($lunKey in $tgtKey.Lun){
$hba = $esx.Extensiondata.Config.StorageDevice.HostBusAdapter | where {$_.Key -eq $hbaKey.Adapter}
$lun = $esx.Extensiondata.Config.StorageDevice.ScsiLun | where {$_.Key -eq $lunKey.ScsiLun}
$datastore = $esx | Get-Datastore | Where-Object {(%{$_.ExtensionData.Info.Vmfs.Extent} | %{$_.DiskName}) -match $lun.CanonicalName}
Select-Object -InputObject $lun -Property @{N="Cluster";E={Get-Cluster -VMHost $esx | %{$_.Name}}},@{N="Host";E={$esx.Name}},@{N="Datastore";E={$datastore | %{$_.Name}}},@{N="FreeSpace";E={$datastore | %{$_.FreeSpaceGB}}},@{N="Capacity";@{N="Used Space (GB)";E={$datastore | %{$_.CapacityGB}}},@{N="Virtual Machines";E={[string]::Join(",", (Get-VM -Datastore $datastore))}},
@{N="HBA";E={$hba.Device}},
CanonicalName,DisplayName,@{N="LUN";E={$lunKey.Lun}}
}
}
}
}
}
$report | Export-Csv "C:\temp\CF03-report.csv" -NoTypeInformation -UseCulture
This should do, just tested and it didn't produce any errors. $report = foreach($esx in Get-VMHost){ foreach($hbaKey in ($esx.Extensiondata.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -like "*FibreChannelHba*"})){ if($hbaKey.Target){ foreach($tgtKey in $hbaKey.Target){ foreach($lunKey in $tgtKey.Lun){ $hba = $esx.Extensiondata.Config.StorageDevice.HostBusAdapter | where {$_.Key -eq $hbaKey.Adapter} $lun = $esx.Extensiondata.Config.StorageDevice.ScsiLun | where {$_.Key -eq $lunKey.ScsiLun} $datastore = $esx | Get-Datastore | Where-Object {(%{$_.ExtensionData.Info.Vmfs.Extent} | %{$_.DiskName}) -match $lun.CanonicalName} $freespace = $datastore | %{$_.FreeSpaceGB} $capacity = $datastore | %{$_.CapacityGB} $usedspace = $capacity - $freespace Select-Object -InputObject $lun -Property @{N="Cluster";E={Get-Cluster -VMHost $esx | %{$_.Name}}},@{N="Host";E={$esx.Name}},@{N="Datastore";E={$datastore | %{$_.Name}}},@{N="FreeSpace (GB)";E={$freespace}},@{N="Capacity (GB)";E={$capacity}},@{N="Used Space (GB)";E={$usedspace}},@{N="Virtual Machines";E={[string]::Join(",", (Get-VM -Datastore $datastore))}}, @{N="HBA";E={$hba.Device}}, CanonicalName,DisplayName,@{N="LUN";E={$lunKey.Lun}} } } } } } $report | Export-CSV "C:\temp\CF03-report.csv" -NoTypeInformation -UseCulture