<#************************************************************************************************************** Purpose: Create Multiple Machines from an XML file (CSV would work as well) XML just sounded cooler Use: Execute the script to load it into your session it will bring you back to a prompt then type in New-Server Somefilenamehere.xml This also has parameters to create from specifc paramenters but you shoud use the New-VM command Instead of this program for command line creation of single machines Tip: The XML file can be created by converting a CSV file to XML using the Command to convert csv - import-csv newserv.csv | export-clixml new.xml I created the CSV in excel saved it as csv and then did the covert above In case you were not paying attention it was b'cause I thought XML was cooler If you want use CSV re-write the line: (FROM) $newxml = Import-Clixml $filename (TO) $newxml = Import-CSV $filename Author: Adrian Fuller CSV File: So my CSV file (Column Headers) Look like the line below (Headers separated by , ) VMHost,Name,Template,Location,Datastore,DiskMB,DiskPath,DiskStorageFormat,MemoryMB,NumCpu,Floppy,CD,GuestId AlternateGuestName,NetworkName,HARestartPriority,HAIsolationResponse,DrsAutomationLevel,VMSwapfilePolicy,Server,Count Description,Confirm,Type,ResourcePool,VApp #> # You really wanted to preload the template info into variables instead of during execution time #$Linux = Get-Template -name "RHEL5_Template3" #$Windows = Get-Template -name "Argent Template New" #************************** Add Snappins & Connect to Virtual Center *********************************** #add-PSSnapin VMware.VimAutomation.Core Connect-VIServer VirtualCenter #Add code to check if the Snapin is already loaded ******** #******************************************************************************************************* #*********************** Get Parameters **************************************************************** function New-Server { [CmdletBinding(DefaultParametersetName="Two")] param ( [Parameter(Mandatory=$True, HelpMessage="Enter an XML filename, eg. servers.xml", ParameterSetName = "One")] [string] $File , [Parameter(Mandatory=$False, HelpMessage="Enter Datacenter, eg. NHQ", ParameterSetName = "One")] [string] $Data_Center, [Parameter(Position=1, Mandatory=$True, HelpMessage="Valid Builds: DC, SQL, or W2008", ParameterSetName = "Two" )] [string] $BuildType, [Parameter(Position=2, Mandatory=$True, HelpMessage="Enter Number of Servers to Build eg. 2", ParameterSetName = "Two" )] [int]$Amount, [Parameter(Mandatory=$True, HelpMessage="Enter Datacenter, eg. DataCenter", ParameterSetName = "Two")] [string] $DataLocation, [Parameter(Mandatory=$True, HelpMessage="Enter Cluster, eg. PRD001", ParameterSetName = "Two")] [string] $DCluster ) #*********************************** Pick the Correct Parameters ************************************************** if ($file -ne $null) { $Global:newxml = Import-CSV $file #Don't think you need Global here as the create happens here # $build = $_.Type; $GuestID = $_.GuestID; $DiskMB = $_.DiskMB; $MemoryMB = $_.MemoryMB; $Name = $_.Name + $count # $Template = $_.Template; $VMHost = $_.VMHost; $Location = $_.Location; $Description = $_.Description # # New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -GuestId $GuestId ` # -DiskMB $DiskMB -MemoryMB $MemoryMB -Description $Description -CD:$cd -Datastore $lun -RunAsync } elseif ($BuildType -ne $null) { $BLD = @{"Name" = $BuildType} $CNT = @{"Count" = $Amount} $LOC = @{"Location" = $DataLocation} $CLU = @{"Cluster" = $DCluster} $Global:newxml = $BLD + $CNT + $LOC + $CLU } # $newxml = Import-Clixml $filename # This is to take input from web XML # You need #**************************************************************************************************************** #********************************Create Folder ********************************************************* # Will fake out some stuff for testing sake (Remember to get a folder name from input variable) # Ok you are creating the folder under current user name and testing to see if it exist in Datacenter $newFolder = [Environment]::UserName #This will create a folder under the current user $compare = Get-Folder -Location NDC -Name $NewFolder if($newFolder -ne $compare.name){ # $newFolder = "ProjectAlpha" # This would be gathered from the XML imput file $Global:mainFolder = Get-Folder -Name VM -Location NDC | New-Folder -Name $newFolder } #*******************************End Create Folder ******************************************************* # Passing the server parameters and also figuring out how many are to be created $newxml | Foreach-Object {[int]$Count = $_.Count while($count -ne 0) { # Get DataCenter #Create a variable on your XML for this variable $DataCenter = Get-Datacenter -Name $_.Location # Pick Host from Cluster ***** # So you would pick up the VmHost Name from the XML file # In this portion you are picking a HOST from the cluster so not all VM's will be dumped on one ESX host # $DVMHost = ( Get-Cluster "NHQNON005" | Get-VMHost )[0] # $X1 |% (Get-Stat -Entity $_.Name -Stat cpu.usage.average -Realtime -MaxSamples 1 | where {$_.Instance -eq ""}).Value # The above line will look for CPU Utilization (Slows the Script down even more and you did not finish the line) $X1 = ( Get-Cluster $_.Cluster | Get-VMHost ) | where-object {$_.State -like "Connected" } | where-object {$_.PowerState -like "PoweredOn"} $num = ($X1.Count - 1) $i = ($X1.Count -1) $i-- #for($i = 0; $i -lt $X1.Count; $i++) { $Global:DVMHost = ( Get-VMHost $x1[$i] ) #Re-setting the Counter in case the requested number of servers are more than the number of ESX hosts if ($i -lt 0) { $i = $X1.Count } #$DVMHost = ( Get-Cluster $_.Cluster | Get-VMHost )[0] #Pre-Auto Picking days #Picking a LUN This will pick the METALUN with the most Space Free $LU1 = Get-Datastore -VMHost $DVMHost | where-object {$_.name -like "META*"} | where-object { $_.name -notlike "*REPO*"} | sort -Descending FreeSpaceMb $LU = $LU1[0].Name $Lun1 = get-datastore $LU #$lun1 = get-datastore reposlun28 #$lun = $lun1[1] # REPOSLUN28 had two things showing up not sure why but that is why you have the [0] array stuff $lun = $lun1 # Create the Servers From XML File #The Template line is commented out The template has to be on the same datacenter - cause you can't use all the other options $cd = [System.Convert]::ToBoolean($_.cd) #Fix this line it causes problems with File #New-VM -Template $_.Template -VMHost $_.VMHost -Location $_.Location -Name $_.Name -Description $_.Description -Datastore $Lun -RunAsync } #Because of things I am not sure of you have to assign all $_. to another Variable see $Name as an example $buildType = $_.Type; $GuestID = $_.GuestID; $DiskMB = $_.DiskMB; $MemoryMB = $_.MemoryMB; $Name = $_.Name + $count;$Folder1 = $_.Folder $Template = $_.Template; $VMHost = $_.VMHost; $Location = $_.Location; $Description = $_.Description; $Templt = $_.Template #**************************** Build Options ********************************************************************* switch ($buildType){ DCTMP {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -GuestId "winNetStandard64Guest" -NetworkName "VLAN_34" ` -DiskMB "20480" -MemoryMB "4096" -NumCpu "2" -Description $Description -CD:$true -Datastore $lun -RunAsync -ErrorAction "SilentlyContinue" ;break} Linux {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -GuestId $GuestId ` -DiskMB $DiskMB -MemoryMB $MemoryMB -Description $Description -CD:$cd -Datastore $lun -RunAsync -ErrorAction "SilentlyContinue" ;break} W2003 {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -GuestId $GuestId ` -DiskMB $DiskMB -MemoryMB $MemoryMB -Description $Description -CD:$cd -Datastore $lun -RunAsync -ErrorAction "SilentlyContinue" ;break} W2008 {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -GuestId $GuestId ` -DiskMB $DiskMB -MemoryMB $MemoryMB -Description $Description -CD:$cd -Datastore $lun -RunAsync -ErrorAction "SilentlyContinue" ;break} Custom {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -GuestId $GuestId ` -DiskMB $DiskMB -MemoryMB $MemoryMB -Description $Description -CD:$cd -Datastore $lun -RunAsync -ErrorAction "SilentlyContinue" ;break} DC {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder ` -Description $Description -Datastore $lun -Template (Get-Template DCTMP) -RunAsync -ErrorAction "SilentlyContinue" ;break} 1 {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -OSCustomizationSpec (Get-OSCustomizationSpec Win2k3x86)` -Description $Description -Datastore $lun -Template (Get-Template Win2K3Entx86_Tmplt) -RunAsync -ErrorAction "SilentlyContinue" ;break} 2 {New-VM -VMHost $DVMHost -Name $Name -Location $MainFolder -OSCustomizationSpec (Get-OSCustomizationSpec Win2k3x86)` -Description $Description -Datastore $lun -Template (Get-Template Win2K3Stdx86_Tmplt) -RunAsync -ErrorAction "SilentlyContinue" ;break} File {New-VM -VMHost $DVMHost -Name $Name -Location $Folder -OSCustomizationSpec (Get-OSCustomizationSpec Win2k3x86)` -Description $Description -Datastore $lun -Template (Get-Template $Template) -RunAsync -ErrorAction "SilentlyContinue" ;break} } [Int]$count-- } } } function Picker{ clear Write-Host "How would you like to Create Servers?" -BackgroundColor Yellow -ForegroundColor Black Write-Host "1. Use a File (XML file with custom attributes" Write-Host "2. Quick Build by specfiing Type of server" Write-Host " (eg. Windows 2008r2 and the amount needed (eg. 10)" Write-Host "Enter Key to exit" -BackgroundColor Yellow -ForegroundColor Black $a = Read-Host switch ($a){ 1 {$fl = Read-Host "Enter File Name" New-Server -file $FL ;break} 2 {Clear Write-host "Please type in the abbreviation as Build type" -background Yellow -ForegroundColor Black Write-Host "DC = Domain Controller" Write-Host "SQL = SQL Server" Write-Host "[1] W2003ENT = Windows 2008r2 STD" Write-Host "[2] W2003STD = Windows 2003 STD" New-Server ;break} } } Picker # This was Fun, sure there are efficiencies that can be had but what the hay