PowerCLI

 View Only
  • 1.  Need PowerCli script to create Blank VM shell

    Posted Oct 30, 2017 02:34 PM

    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

    }

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



  • 2.  RE: Need PowerCli script to create Blank VM shell

    Posted Oct 30, 2017 08:26 PM

    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++

    }



  • 3.  RE: Need PowerCli script to create Blank VM shell

    Posted Oct 30, 2017 08:27 PM

    in theory this would create two vm's

    MSTEST33

    MSTEST34



  • 4.  RE: Need PowerCli script to create Blank VM shell

    Posted Jan 29, 2019 08:35 AM

    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.