PowerCLI Automate multiple Virtual Machines Creation in vCenter using CSV file

Поделиться
HTML-код
  • Опубликовано: 5 май 2023
  • In This Video, you will learn how to automate multiple Virtual Machine build. We are using CSV file to give input for PowerShell script and whatever information you have used in *csv file, it will be used to build the VM's.
    You can use below script and let me know if you are facing any challenges.
    ======Script Here=====
    Connect-VIServer -Server mum-vc01.sitech.com
    $vms = Import-CSV "E:\VMList.CSV"
    foreach ($vm in $vms)
    {
    #Assigning variables as per CSV File
    $VMName =$vm.Name
    $Template = Get-Template -name $vm.Template
    $Cluster = $vm.Cluster
    $Datastore = Get-Datastore -Name $vm.Datastore
    $Custom = Get-OSCustomizationSpec -Name $vm.Customization
    $vCPU = $vm.vCPU
    $Memory = $vm.Memory
    $Network = $vm.Network
    $Location = $vm.Location
    $IPaddress = $vm.IPaddress
    $Subnetmask = $vm.Subnetmask
    $DefaultGateway = $vm.DefaultGateway
    $PrimaryDNS = $vm.PrimaryDNS
    $SecondaryDNS = $vm.SecondaryDNS
    #Assign Network Details to Custom Template
    Get-OSCustomizationSpec $Custom | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IPaddress $IPaddress -Subnetmask $Subnetmask -DefaultGateway $DefaultGateway -Dns $PrimaryDNS,$SecondaryDNS
    #Deploying the VM
    New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -location $location -StorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom
    Start-Sleep -Seconds 10
    #Set vCPU, Memory and Network (VLAN) and Additonal HDD to VM
    $NewVM = Get-VM -NAME $VMName
    $NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
    $NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false
    #Add additonal HDD if present
    #Start the VM to continue the customization process
    Start-VM -VM $NewVM -Confirm:$false
    #Remove Network Details to Custom Template
    #Get-OSCustomizationSpec $Custom | Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false
    }
    ======= Script End ==========
    PowerShell Remotely Uninstall Google Chrome on Multiple Remote Computers
    • PowerShell Remotely Un...
    PowerShell Uninstall WinRAR Software Silently on Multiple Remote Computers
    • PowerShell Uninstall W...
    Find the Current User Logged on Remote Computer and Take Action
    • Find the Current User ...
    Remove User Account or Group from Local Groups on Remote Computers
    • Remove User Account or...
    Automate The Creation of Virtual Machines in VMWare
    • Automate The Creation ...
    PowerCLI Exclude List of VM's From Script Deleting Snapshots in VMware
    • PowerCLI Exclude List ...
    PowerCLI Power On a Suspended VMware Virtual Machine
    • PowerCLI Power On a Su...
    PowerShell Installing software remotely on Multiple Computers
    • PowerShell Installing ...
    PowerShell Send Email if Windows Service is in Stopped State
    • PowerShell Send Email ...
    PowerShell Send email if windows service is not running
    • PowerShell Send email ...
    Get Local Group Members From Remote computers
    • Get Local Group Member...
    Powershell Script To Change Service Account or Update Password
    • Powershell Script To C...
    Powershell How To Change Remote Computer (Workgroup) Local Account Password
    • Powershell How To Chan...
    Delete Virtual Machine Snapshots Older Than Specific Days
    • Delete Multiple Virtua...
    How To Create File Selection Dialog Box in PowerShell
    • How To Create File Sel...
    Add User/Group To The Local Administrators/RDP Group On Multiple Computers
    • Add User/Group To The ...
    Get Windows Updates list from Remote Computer
    • Get Windows Updates li...
    PowerShell Remotely Uninstall Software from Multiple Computers
    • PowerShell Remotely Un...
    PowerShell Search Specific KB Status on Remote Computers
    • PowerShell Search Inst...
    PowerShell Script to Connect Multiple Server's Remote Session
    • PowerShell Script to C...
    PowerCLI Script to power ON dedicated VMs listed in a text file
    • PowerCLI Script to pow...
    PowerCLI Script to power Off dedicated VMs listed in a text file
    • PowerCLI Script to pow...
    Delete Snapshot's from Multiple Virtual Machines
    • Delete Snapshot's from...
    Create Snapshot on Multiple Virtual Machine
    • Create Snapshot on Mul...
    How can I check if a port is open remotely
    • Identify if Windows TC...
    Installing software remotely on Multiple "Workgroup" Computers
    • Installing software re...
    PowerShell Installing software remotely on Multiple Computers
    • PowerShell Installing ...
    Unable to move OU in Active Directory (Access is denied)
    • Unable to move OU in A...
    PowerShell Installing software remotely on Multiple Computers
    • PowerShell Installing ...
    Get Multiple Services Status Remotely | Remotely Start or Stop Services
    • Get Multiple Services ...
    Get Date and Time from Multiple Remote Computers
    • Get Date and Time from...
    PowerCLI - How to get HA restarted VM's List
    • PowerCLI - How to get ...
    Get-Childitem - Search file on Local and Remote Computers
    • Get-Childitem - Search...
  • НаукаНаука

Комментарии • 19

  • @dilpreethayer5238
    @dilpreethayer5238 10 месяцев назад +1

    Really great content

  • @user-vf8wn5sy7h
    @user-vf8wn5sy7h Год назад +1

    Hi Frempong, could you share the command line for additional hard drive
    #Add additonal HDD if present

  • @rakeshramanarayanan5119
    @rakeshramanarayanan5119 Год назад +1

    Hi Swapnil
    Good Day,
    While executing the script we are receiving the below error.
    Get-Template : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then
    try the command again.
    for everything should I do some thing with CSV file ?
    Please advice

    • @SwapnilInfotech
      @SwapnilInfotech  Год назад

      Have you given the template name in the CSV file ? If not then do not use -Template parameters in the script and delete parameter as well as its value.

    • @rakeshramanarayanan5119
      @rakeshramanarayanan5119 Год назад +1

      @@SwapnilInfotech for me all the values coming as null cannot find the argument.
      For template
      For VM name
      For datastore

    • @SwapnilInfotech
      @SwapnilInfotech  Год назад

      Can you recreate the csv file with the proper format and test it, it seems it is not taking value from CSV. Please check and let me know. Post your script as well in case it didn't work.

  • @user-vf8wn5sy7h
    @user-vf8wn5sy7h Год назад

    Hello Brother, I have vSAN datastore and storage format applied thru storage policy for vSAN datastore. How do we change -StorageFormat by default vSAN storage policy instead of Thin provisioning
    #Deploying the VM
    New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -location $location -StorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom
    Start-Sleep -Seconds 10

  • @syedfarooq3208
    @syedfarooq3208 10 месяцев назад

    Hi Sir,
    Can you please make one script for fetch multiple VC RVtool report with encrypted password.

  • @frempongadarkwa2232
    @frempongadarkwa2232 Год назад +1

    how do you deploy only from Templates without custom

    • @SwapnilInfotech
      @SwapnilInfotech  Год назад

      Hi Frempong , you can remove -OSCustomizationSpec $Custom from deploy vm script line. So it won't use custom specs while deploying VM. Below is modified part of the deploy vm script you can use it and let me know if you are facing any issues.
      #Deploying the VM
      New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -location $location -StorageFormat Thin -Datastore $Datastore
      Start-Sleep-Seconds 10

    • @frempongadarkwa2232
      @frempongadarkwa2232 Год назад

      Thanks for your feedback. I just subscribed to your channel

    • @frempongadarkwa2232
      @frempongadarkwa2232 Год назад

      One last thing, how do I deploy a without clusters??

    • @SwapnilInfotech
      @SwapnilInfotech  Год назад

      @@frempongadarkwa2232 You can just remove below like to deploy vm without cluster and it is same for all other parameters. If you don't want to deploy vm with other parameters then you can remove it from command
      -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool)

    • @frempongadarkwa2232
      @frempongadarkwa2232 Год назад

      Okay, thanks very much

  • @frempongadarkwa2232
    @frempongadarkwa2232 Год назад +1

    I tried using your script but i keep on getting this feedback
    Import-CSV : The member "Name" is already present.
    At line:2 char:8
    + $vms = Import-CSV "E:\VMList.CSV"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Import-Csv], ExtendedTypeSystemEx
    ception
    + FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,Micr
    osoft.PowerShell.Commands.ImportCsvCommand

    • @SwapnilInfotech
      @SwapnilInfotech  Год назад

      Can you create new CSV file with proper format. It looks like Name header it is considering twice and giving you that error. Maybe you can try the below line while importing CSV but I would suggest creating a new CSV file as there is a problem with the file.
      Import-Csv -Path "FILEPATH" -Encoding UTF8

    • @frempongadarkwa2232
      @frempongadarkwa2232 Год назад +1

      @@SwapnilInfotech Fixed,working now thanks 🙏

    • @SwapnilInfotech
      @SwapnilInfotech  Год назад

      @@frempongadarkwa2232 ohh awesome, Glad it worked for you.

    • @minhajahmed9373
      @minhajahmed9373 5 месяцев назад

      How can we use portgroup in the input CSV file.