VMware Cloud Community
hareshn
Enthusiast
Enthusiast

Attached Multiple existing disk to VM

Hello,

Which comdlets is getting used to add multiple existing disk to VM ?

Following comdlets only add single disk ? What if OtherVM has multiple Vmdk available ?

New-HardDisk -VM $vm -DiskPath "[storage1] OtherVM/OtherVM.vmdk"

Please guide

thanks in advance

haresh n

0 Kudos
11 Replies
LucD
Leadership
Leadership

The New-Harddisk cmdlet only accepts a single [string] as a parameter value on the DiskPath parameter.

The alternatives are the following:


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

0 Kudos
hareshn
Enthusiast
Enthusiast

Thanks Luc,

I think i should try first foreach loop for this. now 2nd question come in my mind How i defined this in the variable. Do i have to add all the disk information in variable or do you suggest any better option ?

for adding single disk i have defined Source Volume , Source VMDK and the destination VM where that will get attached.

for eq :

    $SourceVM -eq 'Larry1'

    $SourceVolume = 'SQL-Larry-1'

    $SourceVMDK = 'Larry1_5'

    $DestinationVM = 'Larry1-Dev'

    $DestinationVolume = 'SQL-Larry-1-Dev'

    $SnapVolume = 'snap-*-SQL-Larry-1'

New-HardDisk -VM $DestinationVM -DiskPath  "[$SnapVolume] $SourceVM/$SourceVMDK.vmdk"

0 Kudos
LucD
Leadership
Leadership

No, that looks a good way of doing this.

Instead of hard-coding the values in your script, you could use an external file.

For example a CSV file, and then use Import-Csv to get the values


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

0 Kudos
hareshn
Enthusiast
Enthusiast

Thanks Luc,

That's what i thought Importing CSV will be better option.

csv.JPG

CSV fill will have vmname and the disk associated with the vM

Function adddisk

{

$Harddisk=Get-Content C:\my\disk.csv

ForEach ($Harddisk in (1..2))

    {

    New-HardDisk -VM $DestinationVM -DiskPath "[$SnapVolume] $SourceVM/$SourceVMDK.vmdk"

    }

}

    adddisk

What do i need to used for $SourceVM/$SourceVMDK.vmdk  ( $SourceVM/$SourceVMDK.vmdk was hard coded in the script)

can you please guide me the sample to achieve this?

Haresh n

0 Kudos
LucD
Leadership
Leadership

Form what I can deduce it seems that $SourceVM is the name of the folder in which the VMDK file is located.

Normally that is the displayname of the VM where the VMDK was attached.

The $SourceVMDK.vmdk would then be the filename of the VMDK file.

For example, the Hard disk 1 of a VM named test on  datastore DS1 would give the path

[DS1] test/test.vmdk

If there is a 2nd hard disk for that VM, it would be

[DS1] test/test_1.vmdk

If there are snapshots, it would be (for the 1st snapshot)

[DS1] test/test-000001.vmdk

[DS1] test/test_1-000001.vmdk


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

0 Kudos
hareshn
Enthusiast
Enthusiast

Yes it snapshot but on the storage level.. so what it is doing it keeping same folder name and file name.

Only change is happening on the datastore name.

Source Vmname == ABCVM

Source VMdatastore name == NetappData

Source VM disk name is like

disk 1=  abc.vmdk

disk 2= abc1.vmdk

target Vmaname == XYZVM

Target VM datastore ( this will be mounted from the snapshot from the source VM --- Earlier this was mounted as snap..... +NetappData but we have manage to rename before it present to the host

so target VM datastore name == PureData

which will have folder name ==abcvm and disk as above.

0 Kudos
LucD
Leadership
Leadership

So does it work now ?


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

0 Kudos
hareshn
Enthusiast
Enthusiast

yes it is working for the single disk which i have hard coded in the script. using following command.

New-HardDisk -VM $DestinationVM -DiskPath "[$DestinationVolume] $SourceVM/$SourceVMDK.vmdk"

All the variable which you are seeing in above line is hard coded in the script. instead of hard code i want this ( $SourceVM/$SourceVMDK.vmdk") information need to get pull from csv file.

Also need to able to attached multiple disk using foreach loop

something like below

Function adddisk

{

$Harddisk=Get-Content C:\my\disk.csv

ForEach ($Harddisk in (1..2))

    {

    New-HardDisk -VM $DestinationVM -DiskPath "[$SnapVolume] $SourceVM/$SourceVMDK.vmdk"

    }

}

    adddisk

I am not sure how to import and use the content from the csv in the above .

I have attached complete script for your refer.

0 Kudos
LucD
Leadership
Leadership

Try something like this

It will connect the VMDK in the properties disk1,disk2,disk3... until the content of the property is blank.

$SnapVolume = 'abc'

foreach($line in Import-Csv input.csv -UseCulture){

    $vm = Get-VM -Name $line.vmname

    $i = 1

    $hd = $line."disk$($i)"

    while($hd -ne ""){

        New-HardDisk -VM $vm -DiskPath "[$SnapVolume] $hd"

        $i++

    }

}


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

0 Kudos
hareshn
Enthusiast
Enthusiast

Looks like i am very close but encounter and issue for adding 2 disk from the csv.

it has added first disk on the destination vm but gave error for 2nd

below is error screenshot

Adddisk.JPG

and Csv file content

csv.JPG

$SnapVolume = 'Pure-Test-Dev'

foreach($line in Import-Csv C:\MY\DISK.csv -UseCulture){

    $vm = Get-VM -Name $line.vmname

    $i = 1

    $hd = $line."disk$($i)"

    while($hd -ne ""){

        New-HardDisk -VM $vm -DiskPath "[$SnapVolume] $hd"

        $i++

    }

}

0 Kudos
JP112
Contributor
Contributor

Hi Luc,

I was trying to attach multiple vmdk with specific scsi bus number and device number using import-csv method and onyx tools,

Not sure what's wrong but it keep failed and don't able read scsi bus number and associate device number, i have attached CSV for reference

#File Path

$csvpath = "C:\Users\Desktop\Final\TESt0001.csv"

$vmlist = Import-CSV -path $csvpath

# map the Variables

foreach ($item in $vmlist) {

$vmname = $item.vmname

$vmdk = $item.datastorepath

$busnumber = $item.SCSIBUSNUMBER

$unitnumber = $item.UNITNUMBER

$vm = Get-VM $vmname

Write-Host “Adding Disk “$vmdk” to “$vm” at SCSI ID “$busnumber”:”$unitnumber

$ctrll = Get-ScsiController -VM $vm | ?{$_.extensiondata.busNumber -eq $busnumber}

Write-Host “Controller Key “$ctrll.extensiondata.key

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)

$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec

$spec.deviceChange[0].operation = “add”

$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk

$spec.deviceChange[0].device.key = -100

$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$spec.deviceChange[0].device.backing.fileName = “”+$vmdk

$spec.deviceChange[0].device.backing.diskMode = “persistent”

$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo

$spec.deviceChange[0].device.controllerKey = [int]$ctrll.extensiondata.Key

$spec.deviceChange[0].device.unitNumber = [int]$unitNumber

$spec.extraConfig = New-Object VMware.Vim.OptionValue[] (1)

$spec.extraConfig[0] = New-Object VMware.Vim.OptionValue

$spec.extraConfig[0].key = “scsi” + $busnumber + “:” + $unitnumber

$vm.ExtensionData.ReconfigVM($spec)

}

Error:

Exception calling "ReconfigVM" with "1" argument(s): "The device '0' is referring to a nonexisting controller '0'."

At line:50 char:1

+ $vm.ExtensionData.ReconfigVM($spec)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

Much Appreciate

0 Kudos