VMware Cloud Community
DustinHartje
Contributor
Contributor

Utilizing PowerCLI to automate volume resizing in w2k8

What I'm ultimately trying to do is run a powershell script on the guest OS and feed it a drive letter.  The desired result is that the drive size is increased by 5%, and once I have it working I'm going to trigger it via monitoring tools to autogrow a disk that is low on space.

My test VM includes 2 disks on local vmfs storage.  Both have 3 partitions, and OS/boot partition is the second partition on the first drive running win2k8 R2.  I've been able to get either disk itself to resize properly but the options to include resizing partitions on the disk (windows 2008 R2) fail on the first disk though it seems to work fine on the second disk.  Has anyone made this work correctly?

I have the big script running various WMI commands to determine most of the parameters, but in trying to get this basic function working properly I've simplified it down to known good static values for troubleshooting as such:

$TargetDisk = Get-HardDisk -VM DustyDiskTest | where {$_.Name -eq "Hard Disk 1"}

This stores the first disk in a variable and works fine.  If I run:

$TargetDisk | format-list

It will display the correct data including matching size and vmdk file and all that, so I'm confident it's getting the correct disk object in both cases.

I then attempt to increase the size of this disk and use the -ResizeGuestPartition switch to tell it to increase the last partition (default behavior if no -Partition switch is used):

Set-HardDisk $TargetDisk -CapacityKB 19818088 -ResizeGuestPartition -GuestUser "dustydisktest\administrator" -GuestPassword "************" -HostUser "root" -HostPassword "**************" -confirm:$false

The disk in question does get extended as desired but it's unallocated space.  The last partition does not get extended and I get an error stating:

Specified disk "SAS:0:0" was not found

If I specify "Hard Disk 2" instead it works properly and I've been able to resize several different partitions on that disk (when converted to dynamic) by also specifying:

-Partition D

or

-Partition F

etc

I even confirmed the parameters being passed into the batch file that does the guest resize by echoing them out to a text file and they seem appropriate.  When attempting to resize the last partition on the first disk (which errors) it passes in parameters as:

0 0 E SAS

in other words use Bus 0, ID 0, partition lettered E, on the SAS controller (which is actually scsi, but it works for the second disk so I dunno what's up with this).  When resizing the last partition on the second disk (which succeeds) it passes in paramaters as:

0 1 H SAS

Any advice or ideas would be greatly appreciated!!!!

-Dustin Hartje

0 Kudos
3 Replies
DustinHartje
Contributor
Contributor

Found the problem, the PowerCLI script that expands the guest disk has a bug.  If anyone else runs into this situation here's what I discovered:

After expanding the virtual disk, the Set-Harddisk command runs a batch file to extend the windows volume.  I did not test on any other OS version but the file in question (C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\GuestDiskExpansion_WindowsGuest.bat) contains this code:

::::::::::::::  Get each hard disk details and find needed one ::::::::::::::::

for /F "tokens=2 skip=8" %%A in (%list_output_file%) do (
   echo select disk %%A > %script_file%
   echo detail disk >> %script_file%
   diskpart /s %script_file% > %detail_output_file%
   if errorlevel 1 (
      type %detail_output_file%
      call :cleanup
      exit %errorlevel%
   )

At the time this is run the %list_output_file% contains only 7 lines before the disks are listed like so (the first blank line counts too):

1    
2     Microsoft DiskPart version 6.1.7600
3     Copyright (C) 1999-2008 Microsoft Corporation.
4     On computer: DUSTYDISKTEST

5

6       Disk ###  Status         Size     Free     Dyn  Gpt
7       --------  -------------  -------  -------  ---  ---
8       Disk 0    Online           29 GB  1444 MB   *    
9       Disk 1    Online         1309 MB      0 B   *   

Since the For loop skips the first 8 lines this means that the first disk is skipped altogether.  I was able to successfully expand volumes on the first disk by changing the line:

for /F "tokens=2 skip=8" %%A in (%list_output_file%) do (

to

for /F "tokens=2 skip=7" %%A in (%list_output_file%) do (

0 Kudos
justsomeguy293
Contributor
Contributor

AWESOME...thank you. Had this issue on Win7 32-bit and was in the process of debugging the batch file, but you saved me the time.

0 Kudos
MKguy
Virtuoso
Virtuoso

In case anyone could find this useful, the following is part of a template-based VM deployment script to extend the local C:\ volumes and format and assign drive letter to other hard disks if present. It's done via Invoke-VMScript in the guest as opposed to the VM-Side Set-Harddisk approach. It's certainly not perfect, but does the job for the static use case of our base templates:

    if($extend -eq 1) {

        Write-Host "Extending C:\..."
        Invoke-VMScript -ScriptText 'out-file c:\install\diskpartscript.txt -Encoding ASCII -InputObject "select volume 2`r`n extend`r`n"' -VM $vm -GuestCredential $localadmincreds
        Invoke-VMScript -ScriptText "diskpart /s c:\install\diskpartscript.txt" -ScriptType bat -VM $vm -GuestCredential $localadmincreds
    }

    if($format -eq 1) {
        Write-Host "Partitioning other disks..."
        $script = '
        $NumDisks = (Get-Wmiobject -class "Win32_DiskDrive").count
        for($CurrentDisk = 1; $NumDisks -gt $CurrentDisk; $CurrentDisk++) {
            for($j=67; gdr($NextFreeLetter = [char]++$j) 2>$null) {}
            out-file c:\install\diskpartscript.txt -Encoding ASCII -InputObject "select disk $CurrentDisk`r`n clean`r`n create partition primary`r`n format fs=ntfs quick`r`n assign letter=$NextFreeLetter"
            diskpart /s c:\install\diskpartscript.txt
        } '
    Invoke-VMScript -ScriptText $script -VM $vm -GuestCredential $localadmincreds
    }

-- http://alpacapowered.wordpress.com
0 Kudos