Automation

 View Only
  • 1.  How to set ip and disk in VM

    Posted Mar 29, 2023 10:42 AM

    Hi Team,

    I'm newbie to using PowerCLi, here is my script to create multiple VMs using powercli, and i don't know how to set ip and hard disk for VMs can anyone help me? All gusetOS VMs is ubuntu.

    Connect-VIServer 192.168.1.172 -Credential (Get-Credential)

    $vms = Import-CSV "E:\powercli\deployvms.csv"

    foreach ($vm in $vms){
    #Assign Variables
    $Template = Get-Template -Name $vm.Template
    $Cluster = $vm.Cluster
    $Datastore = Get-Datastore -Name $vm.Datastore
    $vCPU = $vm.vCPU
    $Memory = $vm.Memory
    $Network = $vm.Network
    $VMName = $vm.Name

    #Where the VM gets built
    New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -StorageFormat Thin -Datastore $Datastore
    Start-Sleep -Seconds 5

    #Where the vCPU, memory, and network gets set
    $NewVM = Get-VM -Name $VMName
    $NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
    $NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false
    Start-VM -VM $VMName -Confirm:$false -RunAsync
    }


  • 2.  RE: How to set ip and disk in VM

    Posted Mar 29, 2023 11:46 AM

    Adding a hard disk can be done with the New-Harddisk cmdlet.
    You can also create one or more hard disks on the New-VM cmdlet with the DiskGB parameter. 

    Setting the IP address is something that needs to be done inside the Guest OS.
    You can use an OSCustomizationSpec (see New-OSCustomizationSpec) and refer to it on the New-VM cmdlet with the OSCustomizationSpec parameter.
    See for example Solved: Re: Deploy a vm with CLI and OSCustomizationNicMap... - VMware Technology Network VMTN

    Another option is to launch a script inside the Guest OS via Invoke-VMScript.
    See for example BYE DEBIAN/UBUNTU GUEST CUSTOMIZATION, HELLO POWERCLI AND BASH



  • 3.  RE: How to set ip and disk in VM

    Posted Mar 30, 2023 02:19 AM

    I use DiskGB parameter with template then this error appears:

    New-VM : Parameter set cannot be resolved using the specified named parameters.
    At E:\powercli\Create VM.ps1:14 char:1
    + New-VM -Name $VMName -DiskGB 40 -Template $Template -ResourcePool $vm ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-VM], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

    Here is my script I use to create new vm with template:

    Connect-VIServer 192.168.1.172 -Credential (Get-Credential)
    #Assign Variables
    $vmhost = Get-VMhost -Name 192.168.1.171
    $Template = get-template -name ubuntu2004-template
    $Cluster = Get-Cluster -Name FTECH
    $Datastore = Get-Datastore -Name DellR620-171-datastore2
    $VMname = 'test1'
    $vCPU = '4'
    $Memory = '8'
    $Network = 'vlan90'

    #Where the VM gets built
    New-VM -Name $VMName -DiskGB 40 -Template $Template -ResourcePool $vmhost -StorageFormat Thin -Datastore $Datastore

    #Where the vCPU, memory, and network gets set
    $NewVM = Get-VM -Name $VMName
    $NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
    $NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false


  • 4.  RE: How to set ip and disk in VM

    Posted Mar 30, 2023 07:34 AM

    That is because Template and DiskGB belong to 2 different parametersets of the New-VM cmdlet.
    You can't use both parameters together.
    If you need to use Template, you will have to add any additional harddisks after the New-VM with New-Harddisk.