jvm2016
Hot Shot
Hot Shot

create vm using rest api

Hi Luc,

i ham trying to create vm using

com.vmware.vcenter.vm

can you please suggest if this is the right way of creating spec.i want to use create method.

$cred=Get-Credential

connect-cisserver -server vcsa-01a.corp.local -Credential $cred

$vmapi=Get-CisService -Name "com.vmware.vcenter.vm"

$create_spec=$vmapi.Help.create.spec.CreateExample()

$create_spec.guest_os="RHEL_8_64"

$create_spec.name="test"

$create_spec.cpu=@("count=2";"cores_per_socket=1")

#$create_spec.cpu

$create_spec.memory=@("size_MiB=1024","hot_add_enabled=$true")

$create_spec.disks="10GB"

$create_spec.nics="2"

$vmapi.create($create_spec)

0 Kudos
2 Replies
LucD
Leadership
Leadership

It is a bit more complex than that.

You have to specify the placement and disks and nics need a hash table of their own.

$dsName = 'vsanDatastore'

$clusterName = 'cluster'

$folderName = 'TestFolder'

$pgName = 'TestPG'

$vmName = 'TestVM'


$cred = Get-Credential

Connect-CisServer -Server vcsa-01a.corp.local -Credential $cred


$clusterapi = Get-CisService -Name 'com.vmware.vcenter.cluster'

$dsapi = Get-CisService -Name 'com.vmware.vcenter.datastore'

$folderapi = Get-CisService -Name 'com.vmware.vcenter.folder'

$netapi = Get-CisService -Name 'com.vmware.vcenter.network'

$vmapi=Get-CisService -Name 'com.vmware.vcenter.vm'


$create_spec=$vmapi.Help.create.spec.Create()

$create_spec.placement = @{

    datastore = ($dsapi.list() | where{$_.Name -eq $dsName}).datastore.Value

    cluster = ($clusterapi.list() | where{$_.Name -eq $clusterName}).cluster.Value

    folder = ($folderapi.list() | where{$_.Name -eq $folderName}).folder.Value

}

$create_spec.guest_os="RHEL_8_64"

$create_spec.name = $vmName

$create_spec.cpu=@{

    count = 2

    cores_per_socket = 1

}

$create_spec.memory=@{

    size_MiB = 1024

    hot_add_enabled = $true

}

$create_spec.disks = @(

    @{

        new_vmdk = @{

            capacity = 10GB

        }

    }

)

$create_spec.nics = @(

    @{

        type = 'E1000'

        backing = @{

            network = ($netapi.list() | where{$_.Name -eq $pgName}).network.Value

            type = 'STANDARD_PORTGROUP'

        }

    },

    @{

        type = 'E1000'

        backing = @{

            network = ($netapi.list() | where{$_.Name -eq $pgName}).network.Value

            type = 'STANDARD_PORTGROUP'

        }

    }

)


$vmapi.create($create_spec)


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

thanks Luc i m checking this .

0 Kudos