Automation

 View Only
  • 1.  Template OS Type Check

    Posted Aug 04, 2011 12:36 AM

    All,

    I'm looking for a way to check the OSCustomizationSpec for the type of OS is being built.  Trying to have the script run one set of code if the VM is Linux and another type if it's Windows, since Linux can't have DNS added and Windows MUST have DNS when setting the IP Address.

    Windows Customization Code

    Get-OSCustomizationSpec $OSCustomization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $IPAddress -SubnetMask $Subnetmask -DefaultGateway $DefaultGW -Dns xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx

    Linux Customization Code

    Get-OSCustomizationSpec

    $OSCustomization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $IPAddress -SubnetMask $Subnetmask -DefaultGateway $DefaultGW

    I figured it would be a simple if/else but I'm unsure on how to pull the OSType from the Template.

    Also any ideas on how to have the script change the CPU / Memory AFTER the VM is done being deployed.  Was kinda thinking a wait-task command may work.



  • 2.  RE: Template OS Type Check
    Best Answer

    Posted Aug 04, 2011 02:56 AM

    Hello, jnewton-

    It seems like you should be able to determine earlier in your script as to the OS being deployed, since it presumably has to point at one template or another for creating the new VM.  You could, for example, set a variable to "Windows" or "Linux" based on the template you are going to use, and then you would have the basis for your simple if/else.

    Also, depending on how you are using/creating the OSCustomizationSpec, you may be able to tell.  If you are using/copying a CustSpec that is in vCenter and then setting its OSCustomizationNicMapping, you have the CustSpec that was created as one type or the other -- Linux or Windows.  So, you could tell from that CustSpec like:

    $strOSType = (Get-OSCustomizationSpec $mySpec).OSType

    But, if you are not getting the OSType from one of these ways, you could probably use the GuestFullName property of the template, and try to match on "Windows" or "Linux".  This is not the best method, as the values for this property do not always contain one of those two words.  The GuestFullName property can be had from the Template (or, the Template view object).  To get it from the Template:

    (Get-Template myTemplate).ExtensionData.Config.GuestFullName

    These are based on the info you provided.  If you post a bit more about the first portion of your script, we can likely come up with other ideas.



  • 3.  RE: Template OS Type Check

    Posted Aug 04, 2011 02:04 PM

    That got it to work! Yesterday was a pretty long day and I never even thought of just setting a variable myself, that said I'd rather just have the script pick which direction to go.

    Here's the current code that I did:


    if((Get-OSCustomizationSpec $OSCustomization).OSType -match "Windows"){
    Get-OSCustomizationSpec $OSCustomization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $IPAddress -SubnetMask $Subnetmask -DefaultGateway $DefaultGW -Dns 192.168.20.1,192.168.20.2
    }

    if((Get-OSCustomizationSpec $OSCustomization).OSType -match "Linux"){
    Get-OSCustomizationSpec $OSCustomization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $IPAddress -SubnetMask $Subnetmask -DefaultGateway $DefaultGW
    }

    #Create VM
    $VMCreate = New-VM -Name $VMName -VMHost $VMHost -Location $Folder -Datastore $Datastore -Template $Template -OSCustomizationSpec $OSCustomization -Description $Notes -RunAsync
    Wait-Task $VMCreate
    #Change VM settings
    Set-VM $VMName -NumCpu $NumCPU -MemoryMB $Memory -NetworkName $Network -Confirm:$false

    The only thing left is to get the tasks to wait or ideally continue on deploying the VMs from the template then come back when they're finished and change any of the hardware settings.  (CPU, Memory, Network)

    Even if I have the -RunAsnyc or not there the script will continue on running once the deploy process stats and gets a % value in vCenter. Since the VM isn't created it errors out.  It seems that even the Wait-Task doesn't like the $VMCreate variable, which would make sense since this process has somewhat finished in the scripts mind.

    I'm still VERY new to powercli and powershell in general so everything I do is always a learning experiance.

    Ideas?

    Error message I'm currently getting when running this code.

    New-VM : 8/4/2011 7:36:06 AM    New-VM        Operation is not valid due to the current state of the object.   
    At C:\scripts\DeployFromTemplate.ps1:49 char:24
    +      $VMCreate = New-VM <<<<  -Name $VMName -VMHost $VMHost -Location $Folder -Datastore $Datastore -Template $Template -O
    SCustomizationSpec $OSCustomization -Description $Notes
    + CategoryInfo          : NotSpecified: (:) [New-VM], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

    Wait-Task : Cannot validate argument on parameter 'Task'. The argument is null or empty. Supply an argument that is not null
    or empty and then try the command again.
    At C:\scripts\DeployFromTemplate.ps1:50 char:12
    +      Wait-Task <<<<  $VMCreate
    + CategoryInfo          : InvalidData: (:) [Wait-Task], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.WaitTask



  • 4.  RE: Template OS Type Check

    Posted Sep 27, 2011 01:21 AM

    Hello, jnewton-

    Just revisiting this thread, and saw that you were still getting an error at last post.  Similar errors have been reported by others, and part of their problem was the environment in which they were running the script (i.e., not directly in PowerShell).

    Judging by the errors/symptoms, I wonder:  are you using a standard PowerShell session with the VMware.VimAutomation.Core PSSnapin added, or are you in an editor of some sort, say, PowerGUI Script Editor, or the PowerShell ISE, or...?



  • 5.  RE: Template OS Type Check

    Posted Aug 04, 2011 03:14 AM

    Ah, and, as for setting the number of CPUs and the amount of memory, Set-VM is the way.  Depending on how you used New-VM, you may or may not need to use Wait-Task.

    If you used the -RunAsync parameter on New-VM, then you would want to assign the task that is the output of the command to a variable, so as to be able to use Wait-Task:

    ## store the task in a variable (running asynchronously)
    $taskCreateVM = New-VM -Name newVM -OSCustomizationSpec mySpec ... -RunAsync
    ## wait for the task to complete
    Wait-Task $taskCreateVM
    ## set the CPU/Mem values
    Set-VM newVM -NumCpu 2 -MemoryMB 4096 -Confirm:$false

    Or, if you ran the New-VM command synchronously, you can just use Set-VM on the next line, as the new VM creation will have completed:

    ## create the new VM synchronously
    New-VM -Name newVM -OSCustomizationSpec mySpec ...
    ## set the CPU/Mem values
    Set-VM newVM -NumCpu 2 -MemoryMB 4096 -Confirm:$false

    You can then start up the VM as desired and let the CustSpec work away.