-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ClusterLunMapping.ps1
118 lines (93 loc) · 3.51 KB
/
Get-ClusterLunMapping.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<#
Get-ClusterLunMapping
.Description
Get cluster LUN info
russ 02/05/2016
.Example
./Get-ClusterLunMapping.ps1
#>
# Get Cluster info and populate $vms
$Cluster = Get-Cluster
$countCL = 0
Write-Output " "
Write-Output "Clusters: "
Write-Output " "
foreach($oC in $Cluster){
Write-Output "[$countCL] $oc"
$countCL = $countCL+1
}
$choice = Read-Host "Which Cluster do you want to review?"
Write-Output " "
Write-host "please wait for script to finish, it may take a while...." -ForegroundColor Yellow
$clusName = get-cluster $cluster[$choice]
#Pre-code saves to path with unique name
$datacenter = get-datacenter
$filepath = "C:\vSpherePowerCLI\Output\"
if (!(Test-path $filepath)){ New-Item -Path $filepath -ItemType Directory}
else { Write-host "ok, path exists"}
$filename = "ClusterLunMapping"
$initalTime = Get-Date
$date = Get-Date ($initalTime) -uformat %Y%m%d
$time = Get-Date ($initalTime) -uformat %H%M
Write-Host "---------------------------------------------------------" -ForegroundColor DarkYellow
Write-Host "Output will be saved to:" -ForegroundColor Yellow
Write-Host $filepath$datacenter$clusName-$filename-$date$time".csv" -ForegroundColor White
Write-Host "---------------------------------------------------------" -ForegroundColor DarkYellow
#param($clusName,$csvName=("C:\vSpherePowerCLI\Output" + $clusName + "-LUN.csv"))
$rndNum = Get-Random -Maximum 99999
$LunInfoDef = @"
public string ClusterName;
public string CanonicalName;
public string UsedBy;
public string SizeMB;
"@
$LunInfoDef = "public struct LunInfo" + $rndNum + "{`n" + $LunInfoDef
$esxServers = Get-Cluster $clusName | Get-VMHost | Sort-Object -Property Name
$esxServers | %{
$LunInfoDef += ("`n`tpublic string " + ($_.Name.Split(".")[0]) + ";")
}
$LunInfoDef += "`n}"
Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef
$scsiTab = @{}
$esxServers | %{
$esxImpl = $_
# Get SCSI LUNs
$esxImpl | Get-ScsiLun | where {$_.LunType -eq "Disk"} | %{
$key = $esxImpl.Name.Split(".")[0] + "-" + $_.CanonicalName.Split(".")[1]
if(!$scsiTab.ContainsKey($key)){
$scsiTab[$key] = $_.CanonicalName,"",$_.CapacityMB
}
}
# Get the VMFS datastores
$esxImpl | Get-Datastore | where {$_.Type -eq "VMFS"} | Get-View | %{
$dsName = $_.Name
$_.Info.Vmfs.Extent | %{
$key = $esxImpl.Name.Split(".")[0] + "-" + $_.DiskName.Split(".")[1]
$scsiTab[$key] = $scsiTab[$key][0], $dsName, $scsiTab[$key][2]
}
}
}
# Get the RDM disks
Get-Cluster $clusName | Get-VM | Get-View | %{
$vm = $_
$vm.Config.Hardware.Device | where {$_.gettype().Name -eq "VirtualDisk"} | %{
if("physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode){
$disk = $_.Backing.LunUuid.Substring(10,32)
$key = (Get-View $vm.Runtime.Host).Name.Split(".")[0] + "-" + $disk
$scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
}
}
}
$scsiTab.GetEnumerator() | Group-Object -Property {$_.Key.Split("-")[1]} | %{
$lun = New-Object ("LunInfo" + $rndNum)
$lun.ClusterName = $clusName
$_.Group | %{
$esxName = $_.Key.Split("-")[0]
$lun.$esxName = "ok"
if(!$lun.CanonicalName){$lun.CanonicalName = $_.Value[0]}
if(!$lun.UsedBy){$lun.UsedBy = $_.Value[1]}
if(!$lun.SizeMB){$lun.SizeMB = $_.Value[2]}
}
$lun
} | Export-Csv $filepath$datacenter$clusName-$filename-$date$time".csv" -NoType -UseCulture
Invoke-Item $filepath$datacenter$clusName-$filename-$date$time.csv