{"id":1512,"date":"2016-12-20T08:01:29","date_gmt":"2016-12-20T07:01:29","guid":{"rendered":"http:\/\/vthink.fr\/en\/?p=1512"},"modified":"2016-12-18T10:23:48","modified_gmt":"2016-12-18T09:23:48","slug":"powercli-script-deploy-vms-with-random-configurations","status":"publish","type":"post","link":"https:\/\/vthink.fr\/en\/2016\/12\/powercli-script-deploy-vms-with-random-configurations\/","title":{"rendered":"[Powercli] Script : deploy VMs with random configurations"},"content":{"rendered":"\n<!-- Quick Adsense WordPress Plugin: http:\/\/quickadsense.com\/ -->\n<div class=\"e93ad9a1ff002511692efbd236903a83\" data-index=\"1\" style=\"float: left; margin: 10px 10px 10px 0;\">\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- ads1 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:inline-block;width:300px;height:250px\"\r\n     data-ad-client=\"ca-pub-9785518987307332\"\r\n     data-ad-slot=\"8809267401\"><\/ins>\r\n<script>\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\n<\/div>\n<p><img loading=\"lazy\" class=\" wp-image-224 alignleft\" src=\"https:\/\/i1.wp.com\/vthink.fr\/wp-content\/uploads\/powercli.png?resize=109%2C106&#038;ssl=1\" alt=\"\" width=\"109\" height=\"106\" srcset=\"https:\/\/i1.wp.com\/vthink.fr\/wp-content\/uploads\/powercli.png?w=170&amp;ssl=1 170w, https:\/\/i1.wp.com\/vthink.fr\/wp-content\/uploads\/powercli.png?resize=50%2C50&amp;ssl=1 50w\" sizes=\"(max-width: 109px) 100vw, 109px\" data-recalc-dims=\"1\" \/><\/p>\n<p>[Powercli] Script : deploy VMs with random configuration<\/p>\n<p>How to deploy VMs with random configurations based on a template for your lab or infrastructure<\/p>\n<p><!--more--><\/p>\n<p>For my lab, i need really often to deploy several virtual machines very quickly based on a VM or a template.<\/p>\n<p>This is why, i have create a script to deploy VMs based on a template with random configurations (vCPU\/Memory) and for your infrastructure too, like your host and datastore. The idea was to create VMs as fast as possible on all my infrastructure to generate a random load.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush:powershell\">##########################################################################\r\n## Version : 1.0                                                        ##\r\n## Created by : Julien VARELA                                           ##\r\n## Release Date : 12\/12\/2016                                            ##\r\n## Name : DeployVMLAb.ps1                                               ##\r\n##                                                                      ##\r\n##########################################################################\r\n##Verif des modules VMware\r\n$PSNAP= Get-PSSnapin VMware.VimAutomation.Core -erroraction \u2018silentlycontinue\u2019\r\nif ($? -eq $false)\r\n{\r\n##Load VMware Modules\r\nWrite-Host \" Loading VMware modules \"\r\nasnp vmware.*\r\n}\r\nElse\r\n{\r\nWrite-host \" VMware modules are already loaded \"\r\n}\r\n\r\n$vCenter = Read-Host \"vCenter FQDN\"\r\n$login = $host.ui.PromptForCredential(\"User\/Password\", \"please type your log in and password \", \" \", \" NetBiosUserName \")\r\n\r\n$VMs=read-host \"Number of VMs to deploy\"\r\n\r\nconnect-viserver -server $vcenter -credential $login\r\n\r\n$i=1 # Initialize variable to 1.\r\n\r\n[string]$NamingConvention = \"LABTEST-VM-\" # Naming convention for your VMs\r\n[string]$folderLoc = \"Discovered virtual machine\" # Name of the folder where the VMs will be deployed\r\n$SourceVM = \"W2K12R2_Template\" #Template Name\r\n$OsConfiguration =Get-OSCustomizationSpec -name \"W2K12R2_CUSTOM\" #Name of the customization spec\r\n\r\nwhile ($i -le $VMs) {\r\n    $NumCPUs = (Get-Random -Minimum 1 -Maximum 6) #vCPU configuration (Between 1 and 6)\r\n    $MemoryGB = (\"4\",\"6\",\"8\",\"10\",\"12\") | Get-Random #Memory configuration (Bewteen 4 and 12 GB)\r\n\r\n    $targetDatastore = Get-Datastore | Where {($_.ExtensionData.Summary.MultipleHostAccess -eq \"True\") -and ($_.FreeSpaceMB -gt \"50000\")} | Get-Random\r\n    $targetVMhost = Get-VMHost | Where { $_.ConnectionState -eq \"Connected\" } | Get-Random # Select a connected host\r\n\r\n\r\n    $VMName = $NamingConvention + $i\r\n\r\n\r\n    if ((Get-VM $VMName -ErrorAction SilentlyContinue).Name -eq $VMName) { #Check if VM already existe\r\n        Write-Host \"$VMName already exist, task will be skipped\" -ForegroundColor Yellow\r\n        }\r\n    else {\r\n        Write-Host \"Deployment of the VM $VMName in the folder $folderLoc ...\" -ForegroundColor Green\r\n\r\n#Create our VM\r\n        $task = New-VM -name $VMName -Template $SourceVM -OSCustomizationSpec $OsConfiguration -Datastore $targetDatastore -VMHost $targetVMhost -RunAsync #VM creation based on the template\r\n        while($task.ExtensionData.Info.State -eq \"running\"){\r\n  sleep 2\r\n  $task.ExtensionData.UpdateViewData('Info.State')\r\n}\r\n        $task = Set-VM $VMName -NumCpu $NumCPUs -MemoryGB $MemoryGB -Confirm:$false -RunAsync #VM configuration\r\n        while($task.ExtensionData.Info.State -eq \"running\"){\r\n  sleep 2\r\n  $task.ExtensionData.UpdateViewData('Info.State')\r\n}\r\nsleep 3\r\n        $task =Start-VM -VM $VMName -Confirm:$false # Start of the VM\r\n        while($task.ExtensionData.Info.State -eq \"running\"){\r\n  sleep 2\r\n  $task.ExtensionData.UpdateViewData('Info.State')\r\n}\r\n        }\r\n    $i++\r\n}\r\n\r\n<\/pre>\n<p>Once the script has been executed, you can see the current task in your vCenter task tab.<br \/>\n<img data-attachment-id=\"1507\" data-permalink=\"https:\/\/vthink.fr\/en\/2016\/12\/powercli-script-deployer-des-vms-avec-des-configurations-aleatoires\/vcentertask\/#main\" data-orig-file=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?fit=1200%2C166&amp;ssl=1\" data-orig-size=\"1200,166\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"vCenterTask\" data-image-description=\"\" data-medium-file=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?fit=300%2C42&amp;ssl=1\" data-large-file=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?fit=700%2C97&amp;ssl=1\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1507\" style=\"font-family: inherit; font-style: inherit;\" src=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?resize=700%2C97&#038;ssl=1\" alt=\"vCenter Task\" width=\"700\" height=\"97\" srcset=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?w=1200&amp;ssl=1 1200w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?resize=300%2C42&amp;ssl=1 300w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?resize=768%2C106&amp;ssl=1 768w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?resize=1024%2C142&amp;ssl=1 1024w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?resize=220%2C30&amp;ssl=1 220w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?resize=460%2C64&amp;ssl=1 460w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/vCenterTask.png?resize=50%2C7&amp;ssl=1 50w\" sizes=\"(max-width: 700px) 100vw, 700px\" data-recalc-dims=\"1\" \/><\/p>\n<!-- Quick Adsense WordPress Plugin: http:\/\/quickadsense.com\/ -->\n<div class=\"e93ad9a1ff002511692efbd236903a83\" data-index=\"1\" style=\"float: left; margin: 10px 10px 10px 0;\">\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- ads1 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:inline-block;width:300px;height:250px\"\r\n     data-ad-client=\"ca-pub-9785518987307332\"\r\n     data-ad-slot=\"8809267401\"><\/ins>\r\n<script>\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\n<\/div>\n\n<p>Et Voila, you will have VMs with different memory\/cpu configurations.<\/p>\n<p><img data-attachment-id=\"1508\" data-permalink=\"https:\/\/vthink.fr\/en\/2016\/12\/powercli-script-deployer-des-vms-avec-des-configurations-aleatoires\/listvmcreated\/#main\" data-orig-file=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?fit=1200%2C47&amp;ssl=1\" data-orig-size=\"1200,47\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ListVMcreated\" data-image-description=\"\" data-medium-file=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?fit=300%2C12&amp;ssl=1\" data-large-file=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?fit=700%2C27&amp;ssl=1\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1508\" src=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?resize=700%2C27&#038;ssl=1\" alt=\"VM cr\u00e9\u00e9es\" width=\"700\" height=\"27\" srcset=\"https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?w=1200&amp;ssl=1 1200w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?resize=300%2C12&amp;ssl=1 300w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?resize=768%2C30&amp;ssl=1 768w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?resize=1024%2C40&amp;ssl=1 1024w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?resize=220%2C9&amp;ssl=1 220w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?resize=460%2C18&amp;ssl=1 460w, https:\/\/i2.wp.com\/vthink.fr\/wp-content\/uploads\/ListVMcreated.png?resize=50%2C2&amp;ssl=1 50w\" sizes=\"(max-width: 700px) 100vw, 700px\" data-recalc-dims=\"1\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>You can find the script here :\u00a0<a href=\"https:\/\/vthink.fr\/wp-content\/uploads\/DeployVMLAb.ps1_-1.zip\">DeployVMLAb.ps1<\/a><\/p>\n<p>Enjoy !<\/p>\n<p>&nbsp;<\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>[Powercli] Script : deploy VMs with random configuration How to deploy VMs with random configurations based on a template for your lab or infrastructure<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1088,1026,1580],"tags":[1886,1036,1037,1096,1887,1097,1134,1319,1888,1131,1642,1592,1043,1252,1105,1110,1111,1889,1058,1324,1890,1148,1823,1067,1644,1069,1072,1073,1120,1075,1076,1121,1122,1598,1891,1329],"_links":{"self":[{"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/posts\/1512"}],"collection":[{"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/comments?post=1512"}],"version-history":[{"count":5,"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/posts\/1512\/revisions"}],"predecessor-version":[{"id":1519,"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/posts\/1512\/revisions\/1519"}],"wp:attachment":[{"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/media?parent=1512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/categories?post=1512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vthink.fr\/en\/wp-json\/wp\/v2\/tags?post=1512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}