VMware Cloud Community
Itzasifhere
Contributor
Contributor
Jump to solution

Need to Attach multiple RDM to a VM

Hello geek,

I am trying to write a powercli script which will attach multiple RDMs to a VM. I googled and lot of scripts i received but still that is not clear.

I thought of creating csv file containing all canonical names of RDM and importing that using import-CSV, then no idea how to proceed further.

New-HardDisk cmdlet will do attach but how to attach multiple of RDMs?

PLease reply

Thanks,

Asif

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The UseCulture switch takes care of the different separator characters that exist in different locales (regional settings).

Get-Help Import-Csv -Parameter UseCulture

The % character is an alias for the ForEach-Object cmdlet.

You can check by doing a

Get-Alias %

The Get-Help and Get-Alias cmdlets are very useful when you use PowerShell.

Another useful one is the Get-Member cmdlet, which allows you to see the methods and properties on an object.

Get-VM -Name MyVM | Get-Member


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Since the DeviceName parameter on the New-Harddisk cmdlet doesn't take an array as value, you will have to use a ForEach loop.

Something like this

Import-Csv -Path hd.csv -UseCulture | %{
 
$vm = Get-VM -Name $_.VMName
 
New-HardDisk -VM $vm -DeviceName $_.CanonicalName -DiskType RawPhysical
}

This assumes your CSV file looks like this

VMName,CanonicalName

VM1,canonical1

VM1,canonical2

VM2,canonical3


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Itzasifhere
Contributor
Contributor
Jump to solution

Hi LucD,

Thanks for quick reply. It was helpful indeed.

Could you please explain on "-UseCulture | %" , what exactly it does?

% is hash, I understand that its the csv file which we are passing. But what -UseCulture does?

Correct me if I am wrong.

Appreciate for quick reply.

Thanks,

Asif

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The UseCulture switch takes care of the different separator characters that exist in different locales (regional settings).

Get-Help Import-Csv -Parameter UseCulture

The % character is an alias for the ForEach-Object cmdlet.

You can check by doing a

Get-Alias %

The Get-Help and Get-Alias cmdlets are very useful when you use PowerShell.

Another useful one is the Get-Member cmdlet, which allows you to see the methods and properties on an object.

Get-VM -Name MyVM | Get-Member


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Itzasifhere
Contributor
Contributor
Jump to solution

Hu LucD,

Thanks again.

I was just having look for UseCulture parameter and it states:

Use the list separator for the current culture as the item delimiter. The default is a comma (,).

But my csv file contains no comma. There are 2 columns as you suggested among which 1st column contains VMname and 2nd column contains Canonical name.

Hence, please provide little more insights about this parameter, how its working here below:

Import-Csv -Path hd.csv -UseCulture

Thanks again for all information.
Regards,Asif

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The CSV file format is not a fixed format, see Comma-separated values

With the UseCulture switch the Import-Csv cmdlet will take the default separator for the regional setting you are using on the PC where the PowerShell script runs.

If you another separator character (i.e. not the default one for your regional setting), then you can use the Delimiter parameter.

Get-Help Import-Csv -Parameter Delimiter


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Itzasifhere
Contributor
Contributor
Jump to solution

Hi LucD,

Thanks its working now.

Just for a small note, while preparing the excel sheet which contains canonical name, actually its should contain the Canonicalconsolname.

Remaining code works fine.

Appreciate your help !!!

Thanks,

Asif

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean that the column in the CSV has the name Canonicalconsolename instead of Canonicalname ?

If yes, then you just need to change the property name on the New-Harddisk cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
skyhigh3
Contributor
Contributor
Jump to solution

LucD

I don't have csv but I have something like below. It's not working. Any idea how do I construct the forEach loop here? How do I construct the correct $device  below? Thanks in advance!

$deviceName = (Get-VMHost | Get-ScsiLun | Where {$_.CapacityGB -match "10"}).ConsoleDeviceName

foreach ($item in $deviceName){

    $device = $item

    New-HardDisk -VM win2k12r2_srv2 -DiskType RawPhysical -DeviceName $device

}

Error I am getting:

New-HardDisk : Cannot validate argument on parameter 'DeviceName'. The argument is null. Provide a valid value for the argument, and then try running the command again.

At line:3 char:71

+ ... HardDisk -VM win2k12r2_srv2 -DiskType RawPhysical -DeviceName $device

+                                                                   ~~~~~~~

    + CategoryInfo          : InvalidData: (:) [New-HardDisk], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

New-HardDisk : Cannot validate argument on parameter 'DeviceName'. The argument is null. Provide a valid value for the argument, and then try running the command again.

At line:3 char:71

+ ... HardDisk -VM win2k12r2_srv2 -DiskType RawPhysical -DeviceName $device

+                                                                   ~~~~~~~

    + CategoryInfo          : InvalidData: (:) [New-HardDisk], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you are not picking up any LUN with the Where-clause you are using.

Try like this, it will select all LUNs with a capacity of 10GB or more.

Get-VMHost | Get-ScsiLun | Where {$_.CapacityGB -ge "10"}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos