Automation

 View Only

script to wait for oscustomizationspec to complete

  • 1.  script to wait for oscustomizationspec to complete

    Posted Aug 26, 2015 04:38 PM

    Gurus,

    I am using following code to create VMs with OSCustomizationSpec,

    $vmname = "TestTemp"

    $vmtemplate = "Windows2008R2"

    $Password = "Password1"

    $OrgName = "ORG"

    $FullName = "fullname"

    $vmfolder = "folder"

    $vmhost = "host14.code.local"

    $VLAN="318"

    $ip = "10.3.18.100"

    $subnetmask = "255.255.255.0"

    $gateway = "10.3.18.251"

    $dns = "4.2.2.2"

    $DomainPass="somepass"

    $DomainAccount="domain\someuser"

    $Domain="domain"

    $datastore = "Datastore12_R6"

    $workgroup = "Workgroup"

    $cluster="NB20"

    $TimeZone = "035"

    $portgroup = "10.3.18.0"

    $oscustspec=$vmname+(Get-Random)

    new-oscustomizationspec -name $oscustspec -ostype Windows -fullname $FullName -OrgName $OrgName -namingscheme Fixed -NamingPrefix $vmname -LicenseMode Perseat -adminpassword $password -Workgroup $Workgroup -TimeZone $TimeZone -ChangeSid

    Get-OSCustomizationNicMapping -Spec $oscustspec | set-oscustomizationnicmapping -ipmode UseStaticIP -IPAddress $ip -subnetmask $subnetmask -defaultgateway $gateway -dns $dns

    New-VM -Name $vmname -Template $vmtemplate -VMHost $vmhost -datastore (get-datastore -name $datastore -vmhost $vmhost) -OSCustomizationSpec (Get-OSCustomizationSpec -name $oscustspec) -Location $vmfolder | Start-VM

    try {Get-VM $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $portgroup -Confirm:$false -ErrorAction Stop}catch {break}

    Remove-OSCustomizationSpec -Confirm:$false -customizationSpec (Get-OSCustomizationSpec -name $oscustspec)

    and it is working, now we have to wait in front of the console to let the OSCustomizationspec to complete and wait till restart. I came to couple of script but not able to understand how to use those scripts with above mentioned script.

    1> Waiting for OS customization to complete - VMware PowerCLI Blog - VMware Blogs by Alan, but it's for multiple vms

    2>

    function StartVMAndWaitForSysprep

    (

    [Parameter(Mandatory=$True)]

    [Vmware.VIMAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]$vmRef

    )

    {

    $vm = Start-VM $vmRef -Confirm:$False -ErrorAction:Stop

    # wait until VM has started

    Write-Host "Waiting for VM to start ..."

    while ($True)

    {

    $vmEvents = Get-VIEvent -Entity $vm

    $startedEvent = $vmEvents | Where { $_.GetType().Name -eq "VMStartingEvent" }

    if ($startedEvent)

    {

    break

    }

    else

    {

    Start-Sleep -Seconds 2

    }

    }

    # wait until customization process has started

    Write-Host "Waiting for Customization to start ..."

    while($True)

    {

    $vmEvents = Get-VIEvent -Entity $vm

    $startedEvent = $vmEvents | Where { $_.GetType().Name -eq "CustomizationStartedEvent" }

    if ($startedEvent)

    {

    break

    }

    else

    {

    Start-Sleep -Seconds 2

    }

    }

    # wait until customization process has completed or failed

    Write-Host "Waiting for customization ..."

    while ($True)

    {

    $vmEvents = Get-VIEvent -Entity $vm

    $succeedEvent = $vmEvents | Where { $_.GetType().Name -eq "CustomizationSucceeded" }

    $failEvent = $vmEvents | Where { $_.GetType().Name -eq "CustomizationFailed" }

    if ($failEvent)

    {

    Write-Host "Customization failed!"

    return $False

    }

    if($succeedEvent)

    {

    Write-Host "Customization succeeded!"

    return $True

    }

    Start-Sleep -Seconds 2

    }

    }


    but I am not able to execute this :smileysad:


    can someone help me on this?


    Thanks