zoukankan      html  css  js  c++  java
  • [PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

    Hello everyone, this is the third post of the series. .

     

    Background

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

    In my solution, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Without scripting, manually complete related tasks would be very bored and easy to make mistakes. I started using PowerShell and it really helped ease the pain.

     

    What can this one do

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

    I need to create about 20 VMs, they have different settings, such as number of CPU, Memory amount, Virtual Disk Size, Location, etc. I don’t want to mannually configure them all in GUI. So…

    This script will read a CSV file, and get the configuration for each VM, and then automatically create all the VMs based on your requirement.

    As always, everything in one click!

     

    Logic Intro

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

    Nothing special, just read CSV and create VMs one after another.

    The hard part is the usage of each of the cmdlet involved. Took me most of the time.

    Note the (Invoke-Expression $vairable.member), this is special.

     

    Script is here

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

    $b = Import-Csv D:XYZABCCreateVMs.csv -Header VMName, Processor, RAM, VHDSize, VMLocation, VirtualSwtich
    
    Foreach ( $vm in $b ) 
    {    
        New-Item -Path $vm.VMLocation -ItemType directory -force -ErrorAction SilentlyContinue
        New-VM $vm.VMName -path $vm.VMLocation
        New-VHD -Path ("{0}/{1}/{1}.vhdx" -f $vm.VMLocation, $vm.VMName) -SizeBytes (Invoke-Expression $vm.VHDSize)
        Add-VMHardDiskDrive -VMName $vm.VMName -ControllerType SCSI -ControllerNumber 0 -path ("{0}/{1}/{1}.vhdx" -f $vm.VMLocation, $vm.VMName)
        Get-VM $vm.VMName | Set-VMMemory -DynamicMemoryEnabled 0 -StartupBytes (Invoke-Expression $vm.RAM)
        Get-VM $vm.VMName | Set-VMProcessor -count $vm.Processor
        Get-VM $vm.VMName | Add-VMNetworkAdapter -SwitchName $vm.VirtualSwtich
    }
     

    This is what the CSV look like.

    image

     

    Further

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

    I know SCVMM should be better at this task. I will learn it, and post update on this topic as soon as I tuned how to make SCVMM able to do the same thing on a windows failover cluster.

     

    Please forgive me for not listing all the articles that supported me on writing out this script. There are so many… …

  • 相关阅读:
    贝叶斯思想的实质之我见
    强化学习基础概念理解
    Thinkpad x200用户只能放弃生化危机5(PC版), 希望能全速运行星际争霸2!
    This is it
    今天自己掏腰包去买联通iPhone有几位?
    今天是我的生日:)
    2009已经到来 / 2009 Just the Beginning
    好评如潮的PS3游戏《抵抗2 Resistance2》你玩了吗?
    生化危机5 / BIOHAZARD5 简直就是一款完美的印钞机?(+2009.4.9)
    一部好电影《第九区 District 9》
  • 原文地址:https://www.cnblogs.com/awpatp/p/4994929.html
Copyright © 2011-2022 走看看