Hello all,
I am very new at PowerCLI and I am looking for someone who can help me with a PowerCLI script I am trying to run. I found the below script online created by a person named Ben on Geekynetworks.
When I try and run the script it errors out at the New-VM part:
New-VM : A positional parameter cannot be found that accepts argument '-'.
At C:\VCBScripts\clonetest.ps1:18 char:8
Here is the script, can someone tell me what is wrong in it?
Thanks in advance!
}
#To allow script to be run as batch job
add-pssnapin VMware.VimAutomation.Core
#declare array of VM names to clone
$src_vm_names = @("VM_1", "VM_2")
#connect to server
Connect-VIServer "vcenter" -User username -Password password
#Get current date in YYYYMMDD format
$date = get-date -uformat "%Y%m%d"
foreach($src_vm_name in $src_vm_names){
$src_vm = Get-VM $src_vm_name
#create a new clone of the VM
New-VM -Name ($src_vm_name+"_clone_"+$date) -VM $src_vm -Datastore "remote_datastore" -VMHost "remote_host" -notes $date
#Permanently delete all clones older than 7 days
$clones = get-VM ($src_vm_name+"_clone_*")
$expirydate = Get-date (Get-date).addDays(-7) -uformat "%Y%m%d"
foreach($clone in $clones){
if($clone.notes -lt $expirydate){
Remove-VM $clone -DeletePermanently -confirm:$false
}
}
}