I am using a modified script I found on the internet to attempt the automated creation of multiple VMs from a CSV file. I currently am experiencing trouble having the script pull the VM names from the 'name' column from within the CSV file.
The script I am working off of looks like this:
# Specify vCenter Server, vCenter Server username and vCenter Server user password
write-host “Please specify vCenter Server and enter credentials” -foreground green
$vc = read-Host "Connect to which vCenter Server?"
write-host “Connecting to vCenter Server $vc” -foreground green
$CSVPath = "$ScriptRoot\PROD_VMRequestTEST.xlsx.csv"
Connect-VIServer -Server $vc
$CSVFile = Import-Csv -Path $CSVPath
Import-Csv -Path $CSVPath
$VM_Name = "$CSVFile.name"
#
# Specify number of VMs you want to create
#$vm_count = “500“
#
# Specify number of VM CPUs
$numcpu = “$CSVFile.numcpu“
#
# Specify number of VM GB RAM
$MBram = “$CSVFile.memorygb“
#
# Specify VM disk size (in MB)
#$MBguestdisk = “4096“
#
# Specify VM disk type, available options are Thin, Thick, EagerZeroedThick
#$Typeguestdisk =”Thin“
#
# Specify VM guest OS
#$guestOS = “winNetStandardGuest“
#
# Specify vCenter Server datastore
#$ds = “VCDX56-DS01“
#
# Specify vCenter Server Virtual Machine & Templates folder
$Folder = “Discovered virtual machine”
#
# Specify the vSphere Cluster
$Cluster = “vSphere 5.5 RND“
#
# Specify the VM name to the left of the – sign
#$VM_prefix = ““
#
# End of user input parameters
#_______________________________________________________
#
1..$vm_count | foreach {
$ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random
write-host “Creation of VM $VM_name initiated” -foreground green
New-VM -Name $VM_Name -VMHost $ESXi <#-NumCPU $numcpu -MemoryMB $MBram -DiskMB $MBguestdisk -DiskStorageFormat $Typeguestdisk -Datastore $ds -GuestId $guestOS#> -Location $Folder
write-host “Power On of the VM $VM_name initiated” -foreground green
Start-VM -VM $VM_name -confirm:$false -RunAsync
}
The CSV headers I am attempting to have the CSV file read from are formatted as:
name vlan numcpu memorygb
I have the sections pertaining to the remaining columns commented out currently while I troubleshoot the current issue with the VM names being read from the CSV file.
The VMs are currently being created as " .name" ((blank space).name) in vCenter and I would like to determine if this is a formatting issue or if my script is missing any information relating to this.
Thank you for any information you may provide
The logic of that script is not clear to me.
You are reading (imnporting) the specifications from a CSV file -> ok
Then you extract from that imported data, the Name property -> ok
But then you are trying to create $vm_count (not initialised) times a VM, and as the name of that VM you give the complete array of names from the CSV that was imported earlier.
If I interpret it correctly, what you are trying to do, could be done as
The Import-Csv will read the CSV file and send each row as an object.
We loop (% alias for ForEach-Object) through all these rows.
A row object is represented as $_ inside the loop, so we can refer to a property of that row with the standard notation, i.e. $_.Name.
# Specify vCenter Server, vCenter Server username and vCenter Server user password
Write-Host 'Please specify vCenter Server and enter credentials' -foreground green
$vc = Read-Host 'Connect to which vCenter Server?'
Write-Host “Connecting to vCenter Server $vc” -ForegroundColor green
Connect-VIServer -Server $vc
$CSVPath = "$ScriptRoot\PROD_VMRequestTEST.xlsx.csv"
# Specify the vSphere Cluster
$Cluster = 'vSphere 5.5 RND'
$esx = Get-Cluster $Cluster | Get-VMHost -State connected
Import-Csv -Path $CSVPath | %{
Write-Host “Creation of VM $_.Name initiated” -ForegroundColor green
New-VM -Name $_.Name -VMHost ($esx | Get-Random)
Write-Host “Power On of the VM $_.name initiated” -ForegroundColor green
Start-VM -VM $_.Name -confirm:$false -RunAsync
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The logic of that script is not clear to me.
You are reading (imnporting) the specifications from a CSV file -> ok
Then you extract from that imported data, the Name property -> ok
But then you are trying to create $vm_count (not initialised) times a VM, and as the name of that VM you give the complete array of names from the CSV that was imported earlier.
If I interpret it correctly, what you are trying to do, could be done as
The Import-Csv will read the CSV file and send each row as an object.
We loop (% alias for ForEach-Object) through all these rows.
A row object is represented as $_ inside the loop, so we can refer to a property of that row with the standard notation, i.e. $_.Name.
# Specify vCenter Server, vCenter Server username and vCenter Server user password
Write-Host 'Please specify vCenter Server and enter credentials' -foreground green
$vc = Read-Host 'Connect to which vCenter Server?'
Write-Host “Connecting to vCenter Server $vc” -ForegroundColor green
Connect-VIServer -Server $vc
$CSVPath = "$ScriptRoot\PROD_VMRequestTEST.xlsx.csv"
# Specify the vSphere Cluster
$Cluster = 'vSphere 5.5 RND'
$esx = Get-Cluster $Cluster | Get-VMHost -State connected
Import-Csv -Path $CSVPath | %{
Write-Host “Creation of VM $_.Name initiated” -ForegroundColor green
New-VM -Name $_.Name -VMHost ($esx | Get-Random)
Write-Host “Power On of the VM $_.name initiated” -ForegroundColor green
Start-VM -VM $_.Name -confirm:$false -RunAsync
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference