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