LucD
Leadership
Leadership

You're missing some closing braces in a couple of code blocks.

Try like this

$tgtEsxName = "ESXI HOST" #ESXI Host you want to clone to

$tgtDatastoreName = "DATASTORE" #Datastore you want to clone to

$Vc = "mcb-vcenter" #Name of Vcenter Server

$vcUser = "USER" # Username for VCENTER

$vcPass = "PASSWORD"# Password for VCENTER

#set up email

$emailSMTP = "IP.IP.IP.IP" #Ip address of SMTP server

$emailfrom = "CLONING@YOURDOMAIN.COM" #From Email

$emailRCpt = "ADMINS@YOURDOMAIN.COM" #Recipient email

$smtp = New-Object Net.Mail.SmtpClient -arg $emailSMTP

# add snapin for VMware

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer -Server $vc -User $vcUser -Password $vcPass # log into server

# import CSV File

$importFrom = "CSV"

$vmPath = "C:\CLONES\CLONES.CSV" #path of CSV

$vmList = Import-csv $vmPath # Retrieve the CSV file

$Lines = Get-Content $vmPath | Measure-Object #count the lines

#count line ins CSV file

$Total = $Lines.Count - 1

#initiate Counters

$i = 0

$ttl = 1

#start loop

While ($ttl -le $Total) {

  # set date format

  $date = Get-Date -Format "MM/dd/yyyy HH:mm"

}

Function doDate() {

  $now = Get-Date -Format "ddd MMM yyyy HH:mm:ss"

  Write-Host -NoNewline "`n$now | "

}

$VM = Get-VM $vmList[$i].Name

$fromVMname = $vm

$newvmname = "$fromVmname $date"

Write-Host "Creating clone of $VM ($ttl of $Total)"

#Command to clone VM

New-VM -Name ($newvmname) -VM ($fromvmname) -Datastore ($tgtDatastoreName) `

-VMHost ($tgtEsxName) -Location "Remote_clones"

$test = Get-Vm -name $newvmname

#if statement for if Vm is not found

if (!$test){

  #vm not found

 

}

else{

  #else Statement used if the VM is found

  $path = "C:\CLONES\$vm.txt"#path of log file REPLACE EVERYTHING BEFORE\$VM.TXT

  $deleteVM = Get-Content $path #pull line from log file

  Remove-VM $deleteVM -deletepermanently -confirm:$false #remove old clone

  Clear-Content $path #Delete content of log file

  $newvmname | Out-File $path #put name of new server in log file

}

$i++

$ttl++


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos