With the below script i am able to deploy VM but i am unable to set IP in new VM...
Could you please assist me in it.
Import-Csv "C:\PS\Serverdeploy\newserverdeploy.csv" -UseCulture | %{
$viserver = Read-Host "Enter VC Name"
Connect-VIServer $viserver
$Domainuser = $Domainname +"\"+$Domainusername
$Randomhost = Get-Cluster $_.clustername | Get-VMHost | get-random
$portgroup = $Randomhost | Get-VirtualPortGroup | select name
$portgroup | ForEach-Object {if ($_.Name -match $_.inputportgrp) {
$Networkname1 = $_.name
Write-Host $networkname1
}}
$customspec = Get-OSCustomizationSpec $_.customization | Set-OSCustomizationSpec -domain "enterprisenet.org" -domaincredentials (get-credential)
Get-OSCustomizationNicMapping -spec $_.customization | where { $_.Position -eq '1'} | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $_.ip1 -SubnetMask $_.subnet1 -DefaultGateway $_.Gateway1 -Dns $_.dns1,$_.dns2
New-vm -Name $_.ServerName -VMhost $randomhost -Template $_.Template -Datastore $_.Datastore -OSCustomizationspec $customspec
Get-NetworkAdapter -VM $_.ServerName | where {$_.Name -match "1"} | Set-NetworkAdapter -NetworkName $Networkname1 -Connected:$true -StartConnected:$true -Confirm:$false
Start-VM $_.servername
Start-Sleep -Seconds 360
}
Hello, Ganapathy-
It looks like you are setting the OSCustomizationNicMapping properly -- I do not see a problem there. To help locate the problem, there are a few places that you can check that may hold additional info about what happened:
Do those hold any information as to what is happening/failing for you?
Another thing that seems like a possible source for the problem is the CSV file itself. You have likely checked already, but the column headers all match the property names that you are using in your script, correct?
As far as the VM that results from your script -- in what state is the OS after deployment? Does it successfully get renamed/customized as expected, and just the IP address portion is failing? Or, are there other things that fail?
Another point to check, are you using the 32-bit or the 64-bit version of PowerCLI to run this script ?
The VIX-based functions require the 32-bit version.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There was an eror in this line (bolded the word)
$portgroup | ForEach-Object {if ($_.Name -match $_.inputportgrp) {
$Networkname1
= $_.name
Write-Host
$networkname1
}}
After assigning that to a variable it assigns IP to the Server. Now i am facing another issue it's unable to join to domain...
Server is up with network but not in domain!!! Could you please assist LUcD
What's the format of the domain credentials you're getting? Is it username@domain.com or domain\username?
Thanks for your reply...Its domain\user.
It's working on widnows 2003 but not on windows 2008 as SYS PREP differs...Any idea / suggestion please ?
Try username@domain.com. I was having the same issue using downlevel user\domain on 2008.
Found this thread and have 'borrowed' some code to get the NIC configured on some VMs at deploy. What I'd prefer to do for OS customization is to apply an existing customization spec, and just edit the NIC portion. That way, I don't have to worry about passing product keys and domain credentials in the scripts. Is there a way for me to do that on the fly? Using the Set-OSCustomizationNicMapping cmd, the stored customization spec is altered with the NIC settings that I only want to apply to a single VM.
Here's the powershell snippet that likely needs to be modified:
*********************************************************************************************************
Import-CSV C:\PowerShell\Import-CSV\IP_input\Deploy_withIP.csv | Foreach {
#
New-VM -vmhost $_.esxhost -Name $_.vmname -Template $_.templatename -Datastore $_.datastore -DiskStorageFormat thick -Location $_.folder -OSCustomizationSpec $_.customspec
Get-OSCustomizationNicMapping -spec $_.customspec | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $_.ip -SubnetMask $_.netmask -DefaultGateway $_.gw -Dns $_.dns1,$_.dns2
*********************************************************************************************************
Thanks in advance PowerCLI gurus!
Brent
Also see my answer to your other question.
You better create a clone of the OSCustomizationSpec and work on that one.
That way the original is not changed each time.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference