When using a single CSV line, the below script works perfectly to create a VM but when I try to wrap it with the "foreach ($Build in $BuildVar) {}" it fails:
#############
# Variables #
#############
$BuildVar = Import-csv "C:\Builds\VMBuildAutomation\BuildVarsVeeam.csv"
$BuildVar | Foreach-Object {
$IPadd = $_.IPadd
$Mask = $_.Mask
$Gateway = $_.Gateway
$DNS1 = $_.DNS1
$DNS2 = $_.DNS2
$Vlan = $_.Vlan
$Tplate = $_.Tplate
$CustOS = $_.CustOS
$Cluster = $_.Cluster
$VMserver = $_.VMserver
$OU = $_.OU
$Disk1 = $_.Disk1
$Disk2 = $_.Disk2
$WO = $_.WO
$Notes = $_.Notes
$Cores = $_.Cores
$Memory = $_.Memory
$vCenter = $_.vCenter
}
##########################################################################################################################################################
# Connect to vCenter #
##########################################################################################################################################################
#add-pssnapin vmware.vimautomation.core
Connect-VIServer $vcenter
foreach ($Build in $BuildVar) {
##########################################################################################################################################################
# Configuring Customization & Deploy VM #
##########################################################################################################################################################
$datastore = Get-Cluster -Name $Cluster | Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending:$true | Select-Object -First 1
$vHost = Get-Cluster $Cluster | Get-VMHost | Sort-Object -Property MemoryUsageGB -Descending:$false | Select-Object -First 1
$OSSpecs = Get-OSCustomizationSpec -Name $CustOS
$NicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec $OSSpecs.Name
Start-Sleep -Seconds 1
Remove-OSCustomizationNicMapping –OSCustomizationNicMapping $NicMapping -Confirm:$false
Start-Sleep -Seconds 1
$NicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec $OSSpecs.Name
New-OSCustomizationNicMapping -OSCustomizationSpec $OSSpecs.Name | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IPadd -SubnetMask $Mask -DefaultGateway $Gateway -Dns $DNS1, $DNS2
New-VM -Name $VMserver -Template $Tplate -OSCustomizationSpec $OSSpecs -VMHost $vhost -Datastore $datastore
$Nic = Get-NetworkAdapter -VM $VMserver
Set-NetworkAdapter -NetworkAdapter $Nic -NetworkName $Vlan -Confirm:$false
Start-VM $VMserver
Start-Sleep 15
Do {
Start-Sleep -Seconds 60
ipconfig.exe /flushdns
nslookup.exe $IPadd
}
Until ((Test-NetConnection -ComputerName $VMserver).PingSucceeded -eq $true)
Do {Start-Sleep -Seconds 5}
Until ((Test-NetConnection -ComputerName $VMserver -Port 445).TcpTestSucceeded -eq $true)
Do {Start-Sleep -Seconds 5}
Until ((Get-Service -ComputerName $VMserver -Name 'VMTools').Status -eq 'Running')
##########################################################################################################################################################
# Add notes vCenter #
##########################################################################################################################################################
Get-VM -Name $VMserver | Set-VM -Notes "$Notes" -Confirm:$false
##########################################################################################################################################################
#Change CD Volume from D to Z #
##########################################################################################################################################################
Invoke-Command -ComputerName $VMserver -ScriptBlock {
Get-CimInstance Win32_LogicalDisk | ?{ $_.DriveType -eq 5} | select DeviceID
Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" |Set-WmiInstance -Arguments @{DriveLetter='z:'}
}
}
Don't understand dot-sourcing enough to give that a go here and i'm not sure how to tackle this issue.