VMware Cloud Community
hum_mike
Contributor
Contributor
Jump to solution

Deploy VM's from a Template using Customization's and passing IP Info

Ok,

This doesn't see like it should be so difficult, but I cannot get it to work.

Here's the deal, I have several different templates that we deploy from. I also have different customization's for each of those templates.

In the customization, the only information that needs to be passed at time of deployment is the IP information.

For example, if I want to Deploy a Windows 2003 VM, I would use my windows 2003 template and customization. In that file, all the windows information is specified and the only items that I need to specify is what Nic 1's IP will be and what nic 2's IP will be. Does anybody know how to passes those varibles into the command to deploy the machine?

This command works as long as I don't specify the IP information.

New-VM -VMhost ESX1 -name VM1 -Template Windows_2003_Template -Datastore Datastore01 -OSCustomizationSpec

I have 2 nics that I need to configure as part of the build. In the customization file, I specified to prompt user for the IP address, but the gateways, dns servers, wins servers, are already configured. The only varible is what the actual IP will be. How to I get these 2 IP's into the OS Customization cmd.

New-VM -VMhost ESX1 -name VM1 -Template Windows_2003_Template -Datastore Datastore01 -OSCustomizationSpec -ipaddressnic1 10.10.10.15?

New-VM -VMhost ESX1 -name VM1 -Template Windows_2003_Template -Datastore Datastore01 -OSCustomizationSpec (what goes here)

I've seen several post, but have been unable to actually get an answer. I am using the latest version of powershell. (4.1)

0 Kudos
1 Solution

Accepted Solutions
AnotherNerd
Contributor
Contributor
Jump to solution

LucD,

Thanks! that little bit of powercli was amost exactly what I was looking for. Smiley Happy

Greg.

View solution in original post

0 Kudos
19 Replies
LucD
Leadership
Leadership
Jump to solution

Have a look at the New-OSCustomizationNicMapping and Set-OSCustomizationNicMapping cmdlets.

They allow you to set the IPAddress for a CustomizationSpec.

If you want to promtp the user, you could do that as follows

$ipAddr = Read-Host "Enter IP address"
Get-OSCustomizationSpec <your-spec> | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpAddress $ipAddr

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

hum_mike
Contributor
Contributor
Jump to solution

I actually do not want to prompt a user when using the powershell, because there are hundreds of these that we need to deploy. I would like to be able to run a 1 line command that would use a template, use the customization file, and imput the IP addressses without user intervention. Looking at the commands for the Nic mappings I can see how I would put one IP address, but am unclear as to how I would get 2 IP's. I would like to either pull this info from a CSV file or have a ps1 script that has several lines in it where the only difference in each line would be the server name and IP address.

When we setup our customization files, the computer name section in the file is "use virtual machine name". Then all the windows licensing, admin stuff, time zone is specified. Then for the network settings section, we use a custom setup. Here is that setup. There are two nics. (one for production and one for backups). Those nics have all the gateway's, DNS, Wins, domain lookups specified. However, for the actual IP address, the "Prompt user is specified". If we are only deploying one machine through the virtual center, this has worked perfect. However, for mass deployments, I would like to use a powershell script that will build one machine after another using certain a specified IP address.

For example, lets say I want to build VM1 using 1.1.1.1 for the backup (nic1 is customization file) and 2.2.2.2 for the production (nic2 in customization file). I want to buld the server on vmhost1 on datatstore1 using template1 and customization1. I think my 1 line command would be something like:

New-VM -VMhost VMhost1 -name VM1 -Template Template1 -Datastore Datastore1 -OSCustomizationSpec customization1 (beyond this is where I'm stuck)

- I need to somehowe specify the IPaddress for Nic1 1.1.1.1 and nic2 2.2.2.2

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, for the first NIC you could do this

Import-Csv "your-file" | %{
   Get-OSCustomizationSpec customization1 | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $_.nic1 -SubnetMask 255.255.255.0
   New-VM -VMhost $_.VMhost -name $_.VM -Template $_.Template -Datastore $_.Datastore -OSCustomizationSpec customization1 
}

This assumes your CSV file looks like this

VM,Datastore,VMhost,Template,nic1
xxx,yyy,zzz,aaa,10.0.0.1
....

Afaik, you can only configure 1 NIC with the Set-OSCustomizationNicMapping cmdlet.

But you could perhaps use the Set-VMGuestNetworkInterface cmdlet to configure the 2nd NIC once the new guest is cloned and started ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

AnotherNerd
Contributor
Contributor
Jump to solution

LucD,

Thanks! that little bit of powercli was amost exactly what I was looking for. Smiley Happy

Greg.

0 Kudos
hum_mike
Contributor
Contributor
Jump to solution

Sorry it has taken me so long to get back. I have spent today trying to get the above code to work, but I keep getting errors.

My CSV is setup as:

Vmname Datastore Vmhost Template nic1

VM1 Data1 esx1 temp1 10.10.15.230

When I run this in a ps1 script:

Import-Csv "C:\Scripts\VMware\Certified\Builds\serverbuild.csv" | %{

Get-OSCustomizationSpec Dev_Biztalk_Server_w2k8 | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $_.nic1 -SubnetMask 255.255.0.0

New-VM -VMhost $_.VMhost -name $_.VMname -Template $_.Template -Datastore $_.Datastore -OSCustomizationSpec Dev_Biztalk_Server_w2k8

}

I get the following errors:

C:\Scripts\VMware\Certified\Builds> .\test.ps1

Get-OSCustomizationNicMapping : 8/3/2010 12:39:39 PM Get-OSCustomizationNicMapping The pipeline has been stopped.

At C:\Scripts\VMware\Certified\Builds\test.ps1:2 char:81

+ Get-OSCustomizationSpec Dev_Biztalk_Server_w2k8 | Get-OSCustomizationNicMapping <<<< | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $_.nic1 -SubnetMask 255.

255.0.0

+ CategoryInfo : NotSpecified: (Smiley Happy , VimException

+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetOSCustomizationNicMapping

Set-OSCustomizationNicMapping : 8/3/2010 12:39:39 PM Set-OSCustomizationNicMapping When the IpMode parameter is set to UseStaticIp, you must specify the IPAddress, Subne

tmask, Dns(on Windows specs only), and DefaultGateway parameters.

At C:\Scripts\VMware\Certified\Builds\test.ps1:2 char:113

+ Get-OSCustomizationSpec Dev_Biztalk_Server_w2k8 | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping <<<< -IpMode UseStaticIp -IpAddress $_.nic1 -SubnetMask 255.

255.0.0

+ CategoryInfo : InvalidArgument: (Smiley Happy , ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateParameters_RequiredParametersSaticIp,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMapping

New-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\Scripts\VMware\Certified\Builds\test.ps1:3 char:32

+ New-VM -VMhost $_.VMhost -name <<<< $_.VMname -Template $_.Template -Datastore $_.Datastore -OSCustomizationSpec Dev_Biztalk_Server_w2k8

+ CategoryInfo : InvalidData: (Smiley Happy , ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

Any Ideas? I've tryied specify the virtual center server, default gateway, dns servers, etc in the ps1 script, but still no luck.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

For the Set-OSCustomizationNicMapping cmdlet you have to, like the message says, specify the IPAddress, Subnetmask, Dns(on Windows specs only) and DefaultGateway parameters.

For the New-VM cmdlet it looks as if the value you get from the CSV ($_.Vmname) is empty.

Can you just do the following and check if the properties are as you expect them to be ?

Import-Csv "C:\Scripts\VMware\Certified\Builds\serverbuild.csv" | %{
   $_
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
hum_mike
Contributor
Contributor
Jump to solution

This is all I get when going through powercli:

#C:\Scripts\VMware> Import-Csv "C:\Scripts\VMware\Certified\Builds\serverbuild.csv" | %{

#>> $_

#>> }

#>>

however, if I open the csv file in excel, I clearly see that it isn't empty and that values are in each field.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you use the CSV notation (comma-separated values) ?

Like this

Vmname,Datastore,Vmhost,Template,nic1
VM1,Data1,esx1,temp1,10.10.15.230

If your regional settings have defined another character as the field separator, you can use the -UseCulture parameter.

Import-Csv "C:\Scripts\VMware\Certified\Builds\serverbuild.csv" -UseCulture | %{
   $_
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
hum_mike
Contributor
Contributor
Jump to solution

Ok, I think it was a microsoft problem. I created the csv file then opened it with excel to update the fields. When I ran your command against that, I got a bunch of junk. I then create a text file, entered the information using the csv notation, and ran your command. That time it worked.

As always LUCD you have helped a great deal.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, you're welcome.

A pity you gave the "Correct Answer" to AnotherNerd Smiley Wink

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
hum_mike
Contributor
Contributor
Jump to solution

Oh Man, I am sorry about that LucD. It doesn't look like it will let me go back and fix it. However, i did give you 2 helpful answers on your post. Hopefully that will help.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks for that, but hey I was only joking Smiley Happy

I liked the other guy's name 'AnotherNerd'.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AnotherNerd
Contributor
Contributor
Jump to solution

Thanks Luc. Since you helped me in the process is there any way I can award you points? or do I have to post a question? I don't don't know how the points thing works around here.

and thanks to hum_mike for asking the question that I was looking for and answer to. Smiley Happy

Regards,

Greg.

just AnotherNerd

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks Greg.

The originator of a thread is the only one that can award points I'm afraid.

Don't worry about it, there will be other questions Smiley Happy

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
hum_mike
Contributor
Contributor
Jump to solution

Hi LucD and Anothernerd,

I thought you might be interested to know that I figured out how to update both nic IP info on 1 customization file. I used these commands in my script for each deployment.

Get-OSCustomizationNicMapping -spec Customization1 | where { $_.Position -eq '1'} | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress Nic1IP -SubnetMask Nic1Mask -DefaultGateway Nic1GW -Dns DNS

Get-OSCustomizationNicMapping -spec Customization1 | where { $_.Position -eq '2'} | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress Nic2IP -SubnetMask Nic2Mask -DefaultGateway Nic2GW -Dns DNS

I then used Netsh in the run once to remove the gateway off of one of the nic.

Hope that helps.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Great find. I'll try that out.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Ganapathymanian
Contributor
Contributor
Jump to solution

I am trying to get this kind of script, is it possible to share your script.

We will deploy 2 to 3 vm's per day, if i am implementing this it will reduce the hours. I am working with LUCD for this 2week before, could you please assist me.

LUCD:

I was asking similar script only, i need to deploy VM from a excel which needs to set ip also

Please assist me.....

Thanks ..........

0 Kudos
Ganapathymanian
Contributor
Contributor
Jump to solution

While executing the same scrpt, i am getting this error. Could you assist me...

I modified the script as:

Connect-VIServer 172.16.0.126

#Get-Content "c:\ipset.csv" | %{

Import-Csv "C:\ipset.csv" -UseCulture | %{

$custSpec = New-OSCustomizationSpec -name ostest21 -Type NonPersistent -OSType Windows -OrgName “My Organization” -FullName “MyVM3” -Domain “test1.com” –DomainUsername “test1\administrator” –DomainPassword “Ganesh@123”

Write-output $custSpec

$os1 = Get-OSCustomizationSpec ostest21

Write-output $os1

$os2 = $os1 | Get-OSCustomizationNicMapping

Write-output "Get-OSCustomizationNicMapping" $os2

$os3 = $os2 | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress 172.16.0.248 -SubnetMask 255.255.0.0

New-VM -VMhost $_.VMhost -name $_.VMname -Template $_.Template -Datastore $_.Datastore -OSCustomizationSpec Dev_Biztalk_Server_w2k8

}

Error is:

C:\> .\serverset.ps1

Name Port User

-


-


-


172.16.0.126 443 TEST1\Administrator

Vmname : vm12

Datastore : iscsi lun2

Vmhost : 172.16.0.240

Template : win2ktemplate1

nic1 : 172.16.0.248

Name : ostest20

Type : NonPersistent

ServerId : /Local=/

Server :

LastUpdate :

DomainAdminUsername : test1\administrator

DomainUsername : test1\administrator

Description :

AutoLogonCount :

ChangeSid : False

DeleteAccounts : False

DnsServer :

DnsSuffix :

Domain : test1.com

FullName : MyVM3

GuiRunOnce :

NamingPrefix :

NamingScheme : Vm

OrgName : My Organization

OSType : Windows

ProductKey :

TimeZone : Int'l Dateline

Workgroup :

LicenseMode : NotSpecified

LicenseMaxConnections :

EncryptionKey :

ExtensionData :

Id : ostest20

Uid : /Local=/OSCustomizationSpec=ostest20/

Name : ostest20

Type : NonPersistent

ServerId : /Local=/

Server :

LastUpdate :

DomainAdminUsername : test1\administrator

DomainUsername : test1\administrator

Description :

AutoLogonCount :

ChangeSid : False

DeleteAccounts : False

DnsServer :

DnsSuffix :

Domain : test1.com

FullName : MyVM3

GuiRunOnce :

NamingPrefix :

NamingScheme : Vm

OrgName : My Organization

OSType : Windows

ProductKey :

TimeZone : Int'l Dateline

Workgroup :

LicenseMode : NotSpecified

LicenseMaxConnections :

EncryptionKey :

ExtensionData :

Id : ostest20

Uid : /Local=/OSCustomizationSpec=ostest20/

Get-OSCustomizationNicMapping

Dns :

Wins :

SpecId : ostest20

Spec : ostest20

SpecType : NonPersistent

NetworkAdapterMac :

Position : 1

IPMode : UseDhcp

IPAddress :

SubnetMask :

DefaultGateway :

AlternateGateway :

VCApplicationArgument :

Id : /Local=/OSCustomizationNicMapping=OSCustomizationNicMap

pingImpl-ostest20-NonPersistent-1/

ExtensionData : VMware.Vim.CustomizationAdapterMapping

Set-OSCustomizationNicMapping : 8/8/2010 2:11:28 AM Set-OSCustomizationNicMa

pping When the IpMode parameter is set to UseStaticIp, you must specify

the IPAddress, Subnetmask, Dns(on Windows specs only), and DefaultGateway param

eters.

At C:\serverset.ps1:13 char:45

+ $os3 = $os2 | Set-OSCustomizationNicMapping <<<< -IpMode UseStaticIp -IpAdd

ress $_.nic1 -SubnetMask 255.255.0.0

+ CategoryInfo : InvalidArgument: (Smiley Happy [Set-OSCustomizationNicMapp

ing], ViError

+ FullyQualifiedErrorId : Core_NicMappingCmdletBase_ValidateParameters_Req

uiredParametersSaticIp,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSC

ustomizationNicMapping

Set-OSCustomizationNicMapping

New-VM : 8/8/2010 2:11:29 AM New-VM Could not find Template with name

'win2ktemplate1'.

At C:\serverset.ps1:15 char:7

+ New-VM <<<< -VMhost $_.VMhost -name $_.VMname -Template $_.Template -Datasto

re $_.Datastore -OSCustomizationSpec ostest20

+ CategoryInfo : ObjectNotFound: (win2ktemplate1:String)

, VimException

+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNo

tFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

New-VM : 8/8/2010 2:11:29 AM New-VM Template parameter: Could not fin

d any of the objects specified by name.

At C:\serverset.ps1:15 char:7

+ New-VM <<<< -VMhost $_.VMhost -name $_.VMname -Template $_.Template -Datasto

re $_.Datastore -OSCustomizationSpec ostest20

+ CategoryInfo : ObjectNotFound: (VMware.VimAutom...mplate Templa

te:RuntimePropertyInfo) , ObnRecordProcessingFailedException

+ FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotF

oundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

0 Kudos
Ganapathymanian
Contributor
Contributor
Jump to solution

Hi Guys,

i can able to deploy VM, finally its throwing error stating customization failed. I tried by passing inputs manually instead of CSV file

Connect-VIServer 172.16.0.126

#Get-Content "c:\ipset.csv" | %{

#Import-Csv "C:\ipset.csv" -UseCulture | %{

$custSpec = New-OSCustomizationSpec -name ostest21 -Type NonPersistent -OSType Windows -OrgName “My Organization” -FullName “MyVM3” -Domain “test1.com” –DomainUsername “test1\administrator” –DomainPassword “Ganesh@123”

Write-output $custSpec

$os1 = Get-OSCustomizationSpec ostest21

Write-output $os1

$os2 = $os1 | Get-OSCustomizationNicMapping

Write-output "Get-OSCustomizationNicMapping" $os2

$os3 = $os2 | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress 172.16.0.248 -SubnetMask 255.255.0.0

Write-output "Set-OSCustomizationNicMapping" $os3

New-VM -VMhost 172.16.0.240 -name test5674 -Template win2k3template1 -Datastore "iScsi Lun2" -OSCustomizationSpec ostest21

#}

0 Kudos