VMware Cloud Community
Naveenn
Contributor
Contributor

Looking to a script to run on vCenter.

Hi Team,

I'm looking for a Power-shell script to add thin provisioned drives to a virtual machine and then format and assign drive letters.

Reply
0 Kudos
3 Replies
JonathanKohler
Enthusiast
Enthusiast

Naveenn,

You might have more luck posting this question in the PowerCLI forum, located here: VMware vSphere™ PowerCLI

Reply
0 Kudos
nkrishnan
Expert
Expert

i believe we cannot do Guest OS operation using PowerCLI. But if your disk space is constant, then create a vmdk and format, clone the VMDK and attached with different VMs.

--Nithin
Reply
0 Kudos
MKguy
Virtuoso
Virtuoso

You can run custom scripts within the GuestOS with the Invoke-VMScript cmdlet to format the drive. I do that for an automated VM deployment script. The function responsible for partitioning, formatting and assigning drive letters to new drives looks like this:

function Run-GuestScript {

    param(

        [string]$admingroup,

        [string]$domain,

        [bool]$extend,

        [bool]$format

    )

    if(($admingroup -ne "") -and ($domain -ne "")) {

        Write-Host "Adding $admingroup to local Admins..."

        Invoke-VMScript -ScriptText "([adsi]`"WinNT://./Administrators,group`").Add(`"WinNT://$domain/$admingroup,group`")" -VM $VM -GuestCredential $LocalAdminCredentials

    }

    if($extend -eq 1) {

        Write-Host "Extending existing partition C:\"

        ###Windows 2008 C:\ = Volume #2

        Invoke-VMScript -ScriptText 'out-file c:\install\diskpartscript.txt -Encoding ASCII -InputObject "select volume 2`r`n extend`r`n"' -VM $VM -GuestCredential $LocalAdminCredentials

        Invoke-VMScript -ScriptText "diskpart /s c:\install\diskpartscript.txt" -ScriptType bat -VM $VM -GuestCredential $LocalAdminCredentials

    

        ###Windows 2012 C:\ = Volume #1

        Invoke-VMScript -ScriptText 'out-file c:\install\diskpartscript.txt -Encoding ASCII -InputObject "select volume 1`r`n extend`r`n"' -VM $VM -GuestCredential $LocalAdminCredentials

        Invoke-VMScript -ScriptText "diskpart /s c:\install\diskpartscript.txt" -ScriptType bat -VM $VM -GuestCredential $LocalAdminCredentials

    }

    if($format -eq 1) {

        Write-Host "Partitioning other new 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 online disk noerr`r`n attributes disk clear readonly`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 $LocalAdminCredentials

    }

}

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