Automation

 View Only
  • 1.  How to create a partition for the hard drive of a new VM?

    Posted Mar 15, 2013 08:54 PM

    I wrote a small PowerShell script that automates the process of creating a new VM. The script does what it should except that the new hard drive doesn't contain a partition and therefore the OS (I provide a CD drive with an ISO file) cannot install.

    I couldn't find any option with New-VM and anything inside the PowerCLI documentation so I am really wondering how this can be accomplished.

    I am using VMWare Workstation and not vSphere but this shouldn't make any difference from the perspective of PowerCLI and New-VM.

    Kind regards,

    Peter

    Here is my script (the comments and the messages are in German). The important part is New-VM down below:

    <#
    .Synopsis
    Anlegen einer VM mit einer ISO-Datei
    .Description
    Bei den DataStore-Pfaden kommt es auf die Groß-/Kleinschreibung an
    #>

    # Snapin laden
    Add-PsSnapin -Name VMware.VimAutomation.Core

    # Konfiguration setzen - keine Zertifikatwarnung
    Set-PowerCLIConfiguration -DefaultVIServerMode Single -InvalidCertificateAction Ignore -DisplayDeprecationWarnings:$True -Confirm:$False | Out-Null

    # Eckdaten für die Anmeldung am VIServer festlegen
    $IP = "192.168.2.100"
    $Port = "453"
    $UserName = "Administrator"
    $Pw = ConvertTo-SecureString -String "!secret" -AsPlainText -Force
    $Cred = New-Object -Typename System.Management.Automation.PsCredential -ArgumentList $UserName, $Pw

    # Verbinding zum VIServer herstellen
    $VIServer = Connect-VIServer -Server $IP -Credential $Cred -Port 453 -Protocol https -ErrorAction SilentlyContinue

    # Gab es ein Problem?
    if (!$?)
    {
      Write-Warning "Keine Verbindung mit VI-Server möglich."
      break
    }

    Write-Verbose "Verbindung mit VI-Server hergestellt." -Verbose

    # ISO-Ordner anlegen wenn er noch nicht existiert
    $ISODataStorePath = "vmstores:\$IP@$Port\ha-datacenter\standard\ISO"
    if (!(Test-Path -Path $ISODataStorePath))
    {
       md -Path $ISODataStorePath -Verbose | Out-Null
    }

    # ISO-Datei in DataStore kopieren
    $XPISOName = "de_windows_xp_professional_with_service_pack_3_x86_cd_x14-80444.iso"
    $XPISOPath = "E:\ISOS\$XPISOName"
    if (!(Test-Path -Path $XPISOPath))
    {
        Write-Verbose "Kopiere $XPISOName nach $ISODataStorePath" -Verbose
        Copy-DatastoreItem -Item $XPISOPath -Destination $ISODataStorePath
    }

    # VM anlegen
    $VMNeu = New-VM -Name XPA -MemoryMB 768 -NetworkName "NAT" -CD -DiskGB 20 -DiskStorageFormat Thin -GuestID winXPProGuest

    # CD-Laufwerk mit ISO-Path verbinden
    $VMNeu | Get-CDDrive | Set-CDDrive -IsoPath "[standard] ISO\$XPISOName" -StartConnected:$True -Confirm:$False | Out-Null

    # Zum Schluss die VM starten - zur Abwechslung einmal asynchron
    # $VMNeu | Start-VM -Confirm:$false -RunAsync



  • 2.  RE: How to create a partition for the hard drive of a new VM?

    Posted Mar 15, 2013 09:30 PM

    You should have a look at the vmkfstools.

    https://www.vmware.com/support/developer/vcli/vcli41/doc/reference/vmkfstools.html

    Viele Grüße :smileywink:

    EDIT

    Forget what I said to --createfs. Thats on datastore level. Anyway vmkfstools should offer something that helps.



  • 3.  RE: How to create a partition for the hard drive of a new VM?

    Posted Mar 15, 2013 10:39 PM

    I'm afraid that PowerCLI doesn't contain any cmdlets to create partitions.

    But PowerShell does contain some cmdlets to create partitions, see for example A Woops–Creating Partition and Format disk using PowerShell does not update the $ share, a post from the Deployment Bunny.

    But you will need to have an environment that has PowerShell and that can access the vDisk.

    Should be easy, set up a VM and install an OS on there with PowerShell.

    Then attach the new vDisk to this VM and do the partitioning and formatting.

    Would that be a possible solution ?



  • 4.  RE: How to create a partition for the hard drive of a new VM?

    Posted Mar 16, 2013 01:31 PM

    Hello,

    Thanks a lot. That clarifies what I was suspecting. I was just uncertain because VM Workstation does the partitioning for a new drive inside the UI so I had hoped there might be a "hidden feature" of New-VM that does the same thing.

    Regards,

    Pete