Hi there,
I am trying to store the vmname and diskpath in a csv file and calling them to add exisiting vmdk's to the virtual machines.
The script works great, but the only problem it loops through twice and adds the same disk twice
I have even attached the CSV file for reference
# load the VMware module
$VimAutoCore = "VMware.VimAutomation.Core"
if ( (Get-PSSnapin -Name $VimAutoCore -ErrorAction SilentlyContinue) -eq $null ) {
Write-Host "loading $VimAutoCore powershell module"
Add-PsSnapin $VimAutoCore
}
#connect to the vCenter Server
$vcenter = Read-Host "Please enter the vCenter Hostname or IP Address?"
#this will prompt the user to enter the user name and password to connect to the vCenter
Write-Host "Connecting to vCenter Server" -ForegroundColor Yellow
Connect-VIServer -Server $vcenter -ErrorAction Stop -Protocol https
#prompt to enter the file path to the CSV file
$csvpath = Read-Host "Please enter the path to CSV file"
$vmlist = Import-CSV -path $csvpath
foreach ($item in $vmlist) {
# Map the variables with csv columns
$vmname = $item.vmname
$diskpath = $item.datastorepath
Write-Host "Attaching the Disk to $vmname" -ForegroundColor Yellow
New-HardDisk -VM $vmname -DiskPath $diskpath
#Write-Host "Powering on Virtual Machines" -ForegroundColor Yellow
#Start-VM -VM $vmname -Confirm:$false -ErrorAction stop
}
Write-Host "Completed" -ForegroundColor Yellow
Disconnect-VIServer $vcenter -Confirm:$false