Hi All,
I am trying to add multiple RDM on some VMs with below code, but I am getting the error as below. please can some one help me to understand what is wrong here.
# Assume a CSV with columns
# VM UnitNr CanonicalName
#
$vms = @()
$vmLun = @{}
Import-Csv "G:\Soft-Data\OS\vmware-Stuff\Add-RDM\RDM&VMs.csv" -UseCulture | %{
$vms += $_.VM
$vmLun[$_.VM + "|" + $_.UnitNr] = $_.CanonicalName
}
$vms = $vms | Sort-Object -Unique
foreach($vmName in $vms){
$vm = Get-VM -Name $vmName
$sc = Get-ScsiController -VM $vm | where {$_.Extensiondata.BusNumber -eq 1}
$canonical = $vmLun[$vm.Name + "|" + $_.Extensiondata.UnitNumber]
$consDev = (Get-ScsiLun -VmHost esxserver01.jklab.test | where {$_.CanonicalName -eq $canonical}).ConsoleDeviceName
New-HardDisk -VM $vm -DeviceName $consDev -DiskType rawVirtual -Controller $sc | Out-Null
}
input File
Error
I am looking help here, pls help!!!!!
The error is indicating you're passing a NULL with the $consDEV variable. As you're not piping in anything to use an $_ in the $canonical line, I'd first try changing that line to :
$canonical = $vmLun[$vm.Name + "|" + $sc.Extensiondata.UnitNumber]
If that doesn't change things, I'd make sure that you have values for $sc, $canonical and $consDev after the following three lines:
$sc = Get-ScsiController -VM $vm | where {$_.Extensiondata.BusNumber -eq 1}
$canonical = $vmLun[$vm.Name + "|" + $sc.Extensiondata.UnitNumber]
$consDev = (Get-ScsiLun -VmHost esxserver01.jklab.test | where {$_.CanonicalName -eq $canonical}).ConsoleDeviceName
as you may not be hitting a scsi controller with bus id 1 -OR- it already had devices on it and the unitnumber is incremented beyond 0 or 1.
Hi, Thanks for replying on this.
So what I should be mentioning in the piping. I am sorry I am not much expert in powershell. Hence may ask some silly question. and you are also talking about some values for $sc, $canonical and $consDev so where these values should be putted. I find this code in one post from LucD. hence I tried it. I wanted to add the new multiple RDM to a VM from a input file. the input file will be having the VM name and the RDM ConsoleDeviceName.
If you're looking to just add the RDMs to the VMs, the script may be over complicating things with the double arrays. Try the following modified script on your test, it should not require a change to you CSV file and see if you get the result you're looking for:
# Assume a CSV with columns
# VM UnitNr CanonicalName
#
$newrdmlist = Import-Csv "G:\Soft-Data\OS\vmware-Stuff\Add-RDM\RDM.csv" -UseCulture
foreach($newrdm in $newrdmlist){
#get device for RDM
$consDev = (Get-ScsiLun -VmHost esxserver01.jklab.test | where {$_.CanonicalName -eq $newrdm.canonicalName}).ConsoleDeviceName
#get target VM
$vm = Get-VM -Name $newrdm.vm
#get the second controller from target VM
$sc = Get-ScsiController -VM $vm | where {$_.Extensiondata.BusNumber -eq 1}
#If controller exists, create and add new RDM, otherwise create new RDM and add to new controller
If ($sc){
New-HardDisk -VM $vm -DeviceName $consDev -DiskType rawVirtual -Controller $sc
} Else {
New-HardDisk -VM $vm -DeviceName $consDev -DiskType rawVirtual | New-SCSIController}
}