PowerCLI

 View Only
  • 1.  Attaching existing disks to VM with specific settings

    Posted May 28, 2021 06:13 PM

    Good day,

    I am having trouble comprehending how to add 16 hard disks to two VMs with PowerCLI. These are existing hard disks shared between these two VMs.

    I need to add them with the following settings:

    VM storage policy: Datastore Default
    Type: Thick Provision Eager Zeroed
    Sharing: Multi-writer
    Disk File: <existing vmdk>
    Shares: Default (Normal > 1000)
    Disk Mode: Independent - Persistent
    Virtual Device Node: Specific SCSI controller (I have multiple SCSI controllers)

    I went over this link and as far as I understand I need to create a few new objects?
    $disk = New-Object VMware.Vim.VirtualDisk
    $back = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

    Thanks



  • 2.  RE: Attaching existing disks to VM with specific settings

    Posted May 28, 2021 06:21 PM

    Is there a question in here?

    Btw, that link you point to is for adding new disks, not adding existing disks



  • 3.  RE: Attaching existing disks to VM with specific settings

    Posted May 28, 2021 06:26 PM

    My apologies! This is what I get to trying to multitask.

    Yes, it is a question on how to add existing disks to a VM with specified hard disk settings. I was using the link as a potential reference that could be useful for my case. I am not very seasoned with PowerCLI so forgive me for miscommunication.



  • 4.  RE: Attaching existing disks to VM with specific settings

    Posted May 31, 2021 10:10 PM

    Hi  . I have narrowed my question a bit. I am wondering how to set a Multiwritter flag for the disk I am attaching.

    Looking at this thread, can I do something like this?

     

    $vm = Get-VM "myVM"

    $vm | New-HardDisk -DiskPath "[Datastore] VM/DISK01_3.vmdk" -Persistence IndependentPersistent -Controller "SCSI controller 2" -OutVariable $hd

    $ctrl = Get-ScsiController -HardDisk $hd
    Try {
        Get-AdvancedSetting -Entity $vm -Name "scsi$($ctrl.ExtensionData.BusNumber):$($hd.ExtensionData.UnitNumber).sharing" -ErrorAction Stop |
        Set-AdvancedSetting -Value "multi-writer"
    }
    Catch {
        New-AdvancedSetting -Entity $vm -Name "scsi$($ctrl.ExtensionData.BusNumber):$($hd.ExtensionData.UnitNumber).sharing"
    }

     



  • 5.  RE: Attaching existing disks to VM with specific settings

    Posted May 31, 2021 10:17 PM

    I looked at another system, as a reference, that has multi-writer preset for its sharing option for multiple drives but I do not see anything like scsi.sharing listed so perhaps my approach is flawed:

    $vm | Get-AdvancedSetting | Select-String -Pattern scsi

    scsi0.pciSlotNumber:160
    scsi0.sasWWID:50 ...
    scsi1.pciSlotNumber:256
    scsi2.pciSlotNumber:1184
    scsi3.pciSlotNumber:1216
    scsi1.sasWWID:50 ...
    scsi2.sasWWID:50 ...
    scsi3.sasWWID:50 ...
    scsi0:1.redo:
    scsi0:0.redo:

    ...

    I use ESXi/vCenter 7



  • 6.  RE: Attaching existing disks to VM with specific settings

    Posted May 31, 2021 11:01 PM

    Seems like it is a version issue. Was able to find the answer as to why scsi.sharing isn't showing, in your other post 

    Still, would very much appreciate your assistance with setting Multiwriter flag on version 7 (if different for 6.x)



  • 7.  RE: Attaching existing disks to VM with specific settings

    Posted Jun 01, 2021 05:58 AM

    I have managed to achieve what I wanted (referencing this code) but not sure if this is the correct way of doing this on version 7. Any feedback?:

    $vm = Get-VM "MyVM"

    $vm | New-HardDisk -DiskPath "[Datastore] VM/DISK01_3.vmdk" `
    -Persistence IndependentPersistent `
    -Controller "SCSI controller 2" `
    -OutVariable hd

    $diskDevice = $hd.ExtensionData
    $diskDeviceBaking = $hd.ExtensionData.backing

    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
    $spec.deviceChange[0].operation = 'edit'
    $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
    $spec.deviceChange[0].device = $diskDevice
    $spec.DeviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
    $spec.DeviceChange[0].device.backing = $diskDeviceBaking
    $spec.DeviceChange[0].device.Backing.Sharing = "sharingMultiWriter"


    Write-Host "`nEnabling Multiwriter flag on on VMDK:" $hd.Name "for VM:" $vm.name
    $task = $vm.ExtensionData.ReconfigVM_Task($spec)
    $task1 = Get-Task -Id ("Task-$($task.value)")
    $task1 | Wait-Task



  • 8.  RE: Attaching existing disks to VM with specific settings

    Posted Jun 01, 2021 09:21 PM

    Here is what I come up with to add multiple hard disks to a VM.

    settings.csv contains:
    vmdk,scsi
    [datastore] VM/hd1.vmdk,2
    [datastore] VM/hd2.vmdk,3
    ...

     

    $vm = Get-VM "MyVM"
    $items = Import-Csv c:\settings.csv
    
    $items | ForEach-Object {
        $vmdk = $_.vmdk
        $scsi_controller = $_.scsi
    
        $vm | New-HardDisk -DiskPath "$vmdk" `
                           -Persistence IndependentPersistent `
                           -Controller "SCSI controller $($scsi_controller)" `
                           -OutVariable hd
    
        $diskDevice = $hd.ExtensionData
        $diskDeviceBaking = $hd.ExtensionData.backing
    
        $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
        $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
        $spec.deviceChange[0].operation = 'edit'
        $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
        $spec.deviceChange[0].device = $diskDevice
        $spec.DeviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
        $spec.DeviceChange[0].device.backing = $diskDeviceBaking
        $spec.DeviceChange[0].device.Backing.Sharing = "sharingMultiWriter"
    
        Write-Host "`nEnabling Multiwriter flag on on VMDK:" $hd.Name "for VM:" $vm.name
        $task = $vm.ExtensionData.ReconfigVM_Task($spec)
        $task1 = Get-Task -Id ("Task-$($task.value)")
        $task1 | Wait-Task
    }

     

     

    This edits a VM twice per hard disk: first time when a hard disk is attached and a second time when "multiwriter" is set. Can anyone please help me minimize the number of calls this does?



  • 9.  RE: Attaching existing disks to VM with specific settings

    Posted Jun 18, 2021 08:40 PM

     Hello.

    Any suggestions on how to improve my script and perhaps consolidate the calls made to a VM?

    Thank you,