Hello,
I found a LUN report here: LUN report - datastore, RDM and node visibility - LucD notes . I noticed in comments on the page, it was suggested to change the ; to any other character, but I am still getting an error.
I tried changing $LunInfoDef += ("`n`tpublic string " + ($_.Name.Split(".")[0]) + ";")
to $LunInfoDef += ("`n`tpublic string " + ($_.Name.Split(".")[0]) + "*") <---- asterisk instead of semi-colon
I copied the script and attempted a run, creating the variable for the $clusName, but I am getting an error during the run. Below is the version of PS, PowerCLI I am on. Further down is the error output.
PS Version
Name Value
---- -----
PSVersion 5.1.16299.1146
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.1146
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PowerCLI Version
----------------
VMware PowerCLI 11.2.0 build 12483598
---------------
Component Versions
---------------
VMware Cis Core PowerCLI Component PowerCLI Component 11.2 build 12483642
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.2 build 12483638
VMware VimAutomation Srm PowerCLI Component PowerCLI Component 11.2 build 12483605
VMware VimAutomation License PowerCLI Component PowerCLI Component 10.0 build 7893904
VMware VimAutomation Vds Commands PowerCLI Component PowerCLI Component 11.2 build 12483615
VMware Vmc PowerCLI Component PowerCLI Component 11.2 build 12483614
VMware Nsxt PowerCLI Component PowerCLI Component 11.2 build 12483633
VMware VimAutomation vROps PowerCLI Component PowerCLI Component 10.0 build 7893921
VMware HorizonView PowerCLI Component 7.1.0 build 12680098
VMware VimAutomation Cloud PowerCLI Component PowerCLI Component 11.0 build 10379994
VMWare ImageBuilder PowerCLI Component 6.7 build 11233116
VMWare AutoDeploy PowerCLI Component 6.7 build 11233116
VMware VimAutomation Storage PowerCLI Component PowerCLI Component 11.2 build 12483611
VMware vSphere Update Manager PowerCLI 6.5 build 7862888
VMware VimAutomation Security PowerCLI Component PowerCLI Component 11.0 build 10380515
VMware Hcx PowerCLI Component PowerCLI Component 11.2 build 12483619
ERROR
Add-Type : c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(6) : ; expected
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(5) : public string SizeMB;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(6) : >>> public string esxi-host;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(7) : public string esxi-host2;
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:16 char:1
+ Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(6) : Invalid token ';' in class, struct, or interface member declaration
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(5) : public string SizeMB;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(6) : >>> public string esxi-host;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(7) : public string esxi-host2;
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:16 char:1
+ Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(7) : ; expected
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(6) : public string esxi-host;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(7) : >>> public string esxi-host2;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(8) : }
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:16 char:1
+ Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(7) : Invalid token ';' in class, struct, or interface member declaration
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(6) : public string esxi-host;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(7) : >>> public string esxi-host2;
c:\Users\\AppData\Local\Temp\qb1ucdbb.0.cs(8) : }
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:16 char:1
+ Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. Compilation errors occurred.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:16 char:1
+ Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
Do you actually have a dash ('-') in the name of your ESXi hosts?
Since that is not a valid character in names in C#, you get that error.
And you can't replace that semi-column, that is the line terminator.
You could avoid the dash by replacing it with an underscore, which is a valid character in a name in C#
$LunInfoDef += ("`n`tpublic string " + ($_.Name.Split(".")[0]).Replace('-','_') + ";")
But the name of that field is used in other places in the script, line 28 and 39.
You will need to use the same Replace there.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hey, Luc!
Thanks for the response. That got me further.
I am hitting this, and it repeats. Not sure if this is looking for a character count for the substring params? I have the script attached as it stands, currently.
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:41 char:1
+ $disk = $_.Backing.LunUuid.Substring(10,32)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:43 char:1
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
...
Looks like the LUNUuid on the RDMs are not the standard layout.
Can you run the following, and check how these LunUuid look like?
Select @{N='VM';E={$_.Parent.Name}},Name,
@{N='LUN';E={$_.ExtensionData.Backing.LunUuid}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Got this back; redacted the VM name:
VM Name LUN
-- ---- ---
Hard disk 15 0200010000514f0c5008e00001587472656d41
Hard disk 16 0200020000514f0c5008e00002587472656d41
Hard disk 14 0200030000514f0c5008e00003587472656d41
Hard disk 13 0200040000514f0c5008e00004587472656d41
Hard disk 12 0200050000514f0c5008e00005587472656d41
Hard disk 11 0200060000514f0c5008e00006587472656d41
Hard disk 10 0200070000514f0c5008e00007587472656d41
Hard disk 9 0200080000514f0c5008e00008587472656d41
Hard disk 8 0200090000514f0c5008e00009587472656d41
Hard disk 7 0200000000514f0c5008e0000a587472656d41
Hard disk 6 02000d0000514f0c5008e0000b587472656d41
Hard disk 5 02000a0000514f0c5008e0000d587472656d41
Hard disk 4 02000b0000514f0c5008e0000e587472656d41
Hard disk 3 02000c0000514f0c5008e0000f587472656d41
Hard disk 2 02000e0000514f0c5008e0000c587472656d41
Try changing that line to
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Getting further! Have another error out for this line: $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:43 char:1
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:43 char:1
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
That's another line where the ESXi hostname is used.
So that has to be changed as well. Something like this
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Tried that, comes back with the same. I have the modified script attached.
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:44 char:1
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt.ps1:44 char:1
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
We must be forgetting a $key somewhere.
Can you just dump the table, on a line just before the line that causes the error.
$scsiTab.GetEnumerator()
Or even better, start a debugging session and place a breakpoint on the line that causes the error.
The check what is actually in $scsiTab and in $key.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Looks like the $key is coming back with the following:
esxi-host2-514f0c5008e00013;
Then the $scsiTab.GetEnumerator()
Name Value
---- -----
esxi-host-514f0c515aa002e0; {naa.514f0c515aa002e0, , 4194304}
.....
So there is no entry with the key being 'esxi-host2-514f0c5008e00013'?
Would you mind attaching the script you are currently using?
Not sure if we are looking at the same code.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
On line 23 and 32, the replace from '-' to '_' was missing.
Try like this
$clusName = 'RHEL A'
$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]).Replace('-', '_') + ";")
}
$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]).Replace('-', '_') + "-" + ($_.CanonicalName.Split(".")[1].Replace('-', '_') + ";")
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]).Replace('-', '_') + "-" + ($_.DiskName.Split(".")[1].Replace('-', '_') + ";")
$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)
$key = (Get-View $vm.Runtime.Host).Name.Split(".")[0].Replace('-', '_') + "-" + $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 $csvName -NoTypeInformation -UseCulture
Invoke-Item $csvName
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the help so far, Luc!
I copy - pasted you post into a new script named lunrpt2.ps1. I have it attached. It is getting the following output. Wonder if there is something in another spot it is not liking?
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt2.ps1:85 char:13
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt2.ps1:85 char:13
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At C:\Users\\Documents\WindowsPowerShell\Scripts\lunrpt2.ps1:85 char:13
+ $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
There must something with the CanonicalName of the LUNs.
Can you run the following?
"$($esx.Name.Split(".")[0].Replace('-', '_'))" + "$($_.CanonicalName.Split(".")[1])"
}
Get-VM -PipelineVariable vm | Get-HardDisk -DiskType RawPhysical,RawVirtual | %{
"$((Get-View $vm.ExtensionData.Runtime.Host).Name.Split(".")[0].Replace('-', '_'))" + "$($_.ExtensionData.Backing.LunUuid.Substring(10))"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Seems I forgot some parts in my previous code.
It should have been like this. Sorry about that.
'LUN-' + "$($esx.Name.Split(".")[0].Replace('-', '_'))" + '-' + "$($_.CanonicalName.Split(".")[1])"
}
Get-VM -PipelineVariable vm | Get-HardDisk -DiskType RawPhysical,RawVirtual | %{
'RDM-' + "$((Get-View $vm.ExtensionData.Runtime.Host).Name.Split(".")[0].Replace('-', '_'))" + '-' + "$($_.ExtensionData.Backing.LunUuid.Substring(10))"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is strange.
The output shows that there are apparently no RDM disks on any of the VMs in that cluster.
While the error you are getting is in a line that is only executed when there are RDM disks.
I'm lost here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference