PowerCLI

 View Only
  • 1.  script to extend Hard drive on Multiple servers

    Posted Mar 29, 2017 01:56 PM

    Dear All,

    I would like to extend a disk to certain size on multiple VM (Morethan 200), presume it is huge time taking, can anyone please suggest with any script which would allow me to do it through automation.

    Thanks in Advance.

    Rachis!



  • 2.  RE: script to extend Hard drive on Multiple servers

    Posted Mar 29, 2017 03:53 PM

    # PowerCLI-Extend-Disks.ps1

    # Must be run in VMware PowerCLI

    # Written by Jason Pearce, www.jasonpearce.com, (2015 June)

    # Inspiration from Brian Wuchner, Adam Stahl, and of course Luc Dekens (LucD)

    # BEGIN Variables

    # vCenter that contains target VMs

    $vCenter="vcenter.example.local"

    # New hard drive size you want (should be larger than current drive size)

    $NewCapacityGB=60

    # One or more virtual machines you want to target (modify and uncomment this line)

    # $VMs=(Get-Cluster -Name "ClusterName" | Get-VM -Name "VM-Prefix-*")

    # $VMs=("VM1","VM2","VM3")

    # Virtual Machine Windows Credentials (a local admin account)

    $GuestUser="administrator"

    $GuestPassword="password"

    # END Variables

    # BEGIN Script

    # Connect to vCenter via PowerCLI

    Connect-VIServer $vCenter

    # BEGIN foreach loop

    foreach ($VM in $VMs) {

    # Have vSphere PowerCLI increase the size of the first hard drive in each target VM

    Get-VM $VM | Get-HardDisk | Where-Object {$_.Name -eq "Hard Disk 1"} | Set-HardDisk -CapacityGB $NewCapacityGB -Confirm:$false

    # Run DISKPART in the guest OS of each of the specified virtual machines

    Invoke-VMScript -VM $VM -ScriptText "ECHO RESCAN > C:\DiskPart.txt && ECHO SELECT Volume C >> C:\DiskPart.txt && ECHO EXTEND >> C:\DiskPart.txt && ECHO EXIT >> C:\DiskPart.txt && DiskPart.exe /s C:\DiskPart.txt && DEL C:\DiskPart.txt /Q" -ScriptType BAT -GuestUser $GuestUser -GuestPassword $GuestPassword

    }

    # END foreach loop

    # Disconnect from vCenter

    Disconnect-VIserver -Confirm:$false

    # END Script

    #Source: https://www.jasonpearce.com/2015/06/12/use-powercli-set-harddisk-and-invoke-vmscript-to-increase-the-size-of-many-virtua…



  • 3.  RE: script to extend Hard drive on Multiple servers

    Posted Mar 29, 2017 04:22 PM

    Just a small remark, that scrip will only work for VMs that have a Windows Guest OS, and then only for specific (recent) versions.
    If you have a Linux distro as the guest OS, you will need to extend the disk inside the guest OS differently.