VMware Cloud Community
brandong08
Enthusiast
Enthusiast
Jump to solution

Script to create VM and assign IP

How can I make this script ask the user for the IP information and assign the IP once VM tools is up and running.  Before I added the network configuration part, this script would successfully ask the user some questions and deploy the VM.

#Create the VM #

#$vcenter = Read-Host 'What is your vCenter Name?'

$vcenter = Read-Host 'vCenter Name'

# $cred = Get-Credential

$DomainUser = "domain\administrator"

$DomainPWord = ConvertTo-SecureString -String "Password" -AsPlainText -Force

$VMDomainCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $DomainUser. $DomainPWord

$VMName = Read-Host 'VM Name'

$TemplateName = "2012R2"

$OSSpecName = 'New Server'

$DatastoreCluster = Read-Host 'Datastore Cluster Name?'

$VMhostCluster = Read-Host 'Cluster Name?'

#$FolderName = "VM"

# Script Execution #

# Connect to vCenter #

if($cred -eq $null) {

    Connect-VIServer $vcenter

    }else{

        connect-VIServer $vcenter -Credential $cred

     }

# Compile New VM Info #

$Template = Get-Template -Name $TemplateName

$OSSSpecName = Get-OSCustomizationSpec -Name $OSSpecName

$Datastore = Get-DatastoreCluster -Name $DatastoreCluster

#Assumes DRS

$Cluster = Get-Cluster -Name $VMhostCluster

#$Folder = Get-Folder $FolderName

# Create VM #

#write changes

write-Verbose -Message "Deploying Virtual Machine with Name: [$VMName] using Template: [$TemplateName] and Customization Specification: [$OSSpecName] on Cluster: [$VMhostCluster] and waiting for completion" -Verbose

New-VM -Name $VMName -Template $Template -OSCustomizationSpec $OSSpec -Datastore $Datastore -ResourcePool $Cluster #-Location $Folder

# Nic Settings

$VMNameNetworkSettings = 'netsh interface ip set address "Ethernet0" static x.x.x.x 255.255.255.0 x.x.x.x'

$VMNameDNSSettings =  'netsh interface ip set dnsservers name="Ethernet0" static x.x.x.x primary x.x.x.x secondary'

#power on

Write-Verbose -Message "Virtual Machine $VMname Deployed. Powering On" -Verbose

Start-VM -VM $VMname

Write-Verbose -Message "Getting ready to change IP Settings on VM $FSVMName." -Verbose

Invoke-VMScript -ScriptText $VMNetworkSettings -VM $VMName -GuestCredential $VMDomainCredential

Invoke-VMScript -ScriptText $VMNameDNSSettings -VM $VMName -GuestCredential $VMDomainCredential

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

After the New-VM and the Start-VM the sysprep deployment will start.

You will have to wait for the sysprep to finsih before you can start configuring components in the guest OS.

One way of doing that is to wait for the CustomizationSucceeded event.

See Waiting for OS customization to complete

On a side note, when can't you configure the NIC with the Set-OSCustomizationNicMapping cmdlet ?


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

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

After the New-VM and the Start-VM the sysprep deployment will start.

You will have to wait for the sysprep to finsih before you can start configuring components in the guest OS.

One way of doing that is to wait for the CustomizationSucceeded event.

See Waiting for OS customization to complete

On a side note, when can't you configure the NIC with the Set-OSCustomizationNicMapping cmdlet ?


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

0 Kudos