VMware Cloud Community
Nawals
Expert
Expert

Need PowerCli script to create Blank VM shell

Hello,

I am using below script to create blank VMs shell but I want to create VMs with start the name "TEST33". Can you please suggest where I want to change the value in this script to create VMs shell.

=================================================================================================================================================

#

# PowerCLI to create VMs

# Version 1.0

# Magnus Andersson RTS

#

# Specify vCenter Server, vCenter Server username and vCenter Server user password

$vCenter="vc,test"

$vCenterUser="test"

$vCenterUserPassword="abc@123"

#

# Specify number of VMs you want to create

$vm_count = "2"

#

# Specify number of VM CPUs

$numcpu = "2"

#

# Specify number of VM MB RAM

$MBram = "4096"

#

# Specify VM disk size (in MB)

$MBguestdisk = "30720"

#

# Specify VM disk type, available options are Thin, Thick, EagerZeroedThick

$Typeguestdisk ="Thin"

#

# Specify VM guest OS

$guestOS = "windows7Server64Guest"

#

# Specify vCenter Server datastore

$ds = "ULDC_EA_Cluster01_07"

#

# Specify vCenter Server Virtual Machine & Templates folder

$Folder = "Citrix"

#

# Specify the vSphere Cluster

$Cluster = "CLUSTER_01"

#

# Specify the VM name to the left of the - sign

$VM_prefix = "MSTEST-"

#

# End of user input parameters

#_______________________________________________________

#

write-host "Connecting to vCenter Server $vCenter" -foreground green

Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -WarningAction 0

1..$vm_count | foreach {

$y="{0:D2}" -f $_

$VM_name= $VM_prefix + $y

$ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random

write-host "Creation of VM $VM_name initiated"  -foreground green

New-VM -Name $VM_Name -VMHost $ESXi -numcpu $numcpu -MemoryMB $MBram -DiskMB $MBguestdisk -DiskStorageFormat $Typeguestdisk -

Datastore $ds -GuestId $guestOS -Location $Folder

write-host "Power On of the  VM $VM_name initiated"  -foreground green

Start-VM -VM $VM_name -confirm:$false -RunAsync

}

===================================================================================================================================================

NKS Please Mark Helpful/correct if my answer resolve your query.
Reply
0 Kudos
3 Replies
jpsider
Expert
Expert

Not exactly sure what this piece was doing so I commented it out. ' $y="{0:D2}" -f $_ '

so give the below a try.

#

# PowerCLI to create VMs

# Version 1.0

# Magnus Andersson RTS

#

# Specify vCenter Server, vCenter Server username and vCenter Server user password

$vCenter="vc,test"

$vCenterUser="test"

$vCenterUserPassword="abc@123"

#

# Specify number of VMs you want to create

$vm_count = "2"

#

# Specify number of VM CPUs

$numcpu = "2"

#

# Specify number of VM MB RAM

$MBram = "4096"

#

# Specify VM disk size (in MB)

$MBguestdisk = "30720"

#

# Specify VM disk type, available options are Thin, Thick, EagerZeroedThick

$Typeguestdisk ="Thin"

#

# Specify VM guest OS

$guestOS = "windows7Server64Guest"

#

# Specify vCenter Server datastore

$ds = "ULDC_EA_Cluster01_07"

#

# Specify vCenter Server Virtual Machine & Templates folder

$Folder = "Citrix"

#

# Specify the vSphere Cluster

$Cluster = "CLUSTER_01"

#

# Specify the VM name to the left of the - sign

$VM_prefix = "MSTEST"

#

[int]$VM_prefixNum = 33

# End of user input parameters

#_______________________________________________________

#

write-host "Connecting to vCenter Server $vCenter" -foreground green

Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -WarningAction 0

1..$vm_count | foreach {

# Not sure what this is doing.....    -> $y="{0:D2}" -f $_

$VM_name= $VM_prefix + $VM_prefixNum # You can uncomment this if its needed.# + "-" + $y

$ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random

write-host "Creation of VM $VM_name initiated"  -foreground green

New-VM -Name $VM_Name -VMHost $ESXi -numcpu $numcpu -MemoryMB $MBram -DiskMB $MBguestdisk -DiskStorageFormat $Typeguestdisk -

Datastore $ds -GuestId $guestOS -Location $Folder

write-host "Power On of the  VM $VM_name initiated"  -foreground green

Start-VM -VM $VM_name -confirm:$false -RunAsync

$VM_prefixNum++

}

Reply
0 Kudos
jpsider
Expert
Expert

in theory this would create two vm's

MSTEST33

MSTEST34

Reply
0 Kudos
vijayku
VMware Employee
VMware Employee

so what this $y="{0:D2}" -f $_ does is, it formats the given variable to two decimal points, so the whole scripts works something like this.

$VM_prefix = "MSTEST-"

$vm_count = "2"

and VM name is being derived as

$VM_name= $VM_prefix + $y

so the VM name will end up looking like MSTEST-01 and MSTEST-02, everything else regular powershell stuff.