[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V
2015-11-25 15:53
891 查看
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
===============
This is what the CSV look like.
![](http://images2015.cnblogs.com/blog/34420/201511/34420-20151125155223874-965557210.png)
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… …
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:\XYZABC\CreateVMs.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.
![](http://images2015.cnblogs.com/blog/34420/201511/34420-20151125155223874-965557210.png)
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… …
相关文章推荐
- Drupal 7.31SQL注入getshell漏洞利用详解及EXP
- 客户端连接服务端
- Shell脚本一枚
- java调用shell脚本,并获得结果集的例子
- Hadoop Shell命令
- 在shell 上执行mongo 查询
- xshell的Quick Command Sets使用--快捷命令操作
- Xshell相关设置
- shell提取指定列并判断提取结果中是否包含指定字符串
- 解决linux的-bash: ./xx.sh: Permission denied
- Shell 常用命令
- PowerShell GUI 之使用visual studio创建GUI (2)
- 用rz、sz命令在Xshell传输文件
- /bin/bash^M: bad interpreter: No such file or directory
- Linux Shell编程变量赋值和引用
- linux shell bash 内置变量参考
- shell判断和比较
- windos7升级powershell到4.0版本步骤
- (1)第一章:shell基本特性
- 《0day安全》学习笔记4——以jmp esp 为跳板的shellcode开发