Must be having an off moment trying to clone 3 vms from a script and I want the cloned vm name to be original vmName + _backup_ + $date but I can't get it to work in a loop
$VC = 'vc.vsphere.local'
$Cluster = 'Intel-Skull-Canyon'
$Date = (get-date).ToString("yyyyMMdd")
$Folder = 'VM_BACKUPS'
$myVM = 'server0*'
Connect-VIServer $VC -username administrator@vsphere.local -password VMware1!
$VMs = Get-VM $myVM
$vsanDS = Get-Cluster $Cluster | Get-Datastore | Where-Object type -EQ 'VMFS'
foreach ($vm in $VMs){
$BackupName = "$vm + '_Backup_' + $Date"
New-VM -Name $BackupName -Location $Folder -VM $vm -Datastore $vsanDS -ResourcePool $cluster
Start-Sleep -Seconds 5
}
Disconnect-VIServer $VC -Force -Confirm:$false
Remove-Variable * -Force -ErrorAction SilentlyContinue
Which PowerCLI version are you using?
That other thread is relatively old, and not using any of the latest PowerCLI builds.
In prod I would not run this from the ISE, but use the regular PowerShell console.
Load the modules and then run your script.
But are you starting that manually in Prod or from a scheduled task?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok I kind of figured out the naming issue but I'm still getting errors although it does clone
$VC = 'vc.vsphere.local'
$Cluster = 'Intel-Skull-Canyon'
$Date = (get-date).ToString("yyyyMMdd")
$Folder = 'VM_BACKUPS'
$myVM = 'server0*'
Connect-VIServer $VC -username administrator@vsphere.local -password VMware1!
$VMs = Get-VM $myVM
$vsanDS = Get-Cluster $Cluster | Get-Datastore | Where-Object type -EQ 'VMFS'
foreach ($vm in $VMs){
New-VM -Name "$($vm.Name)-clone" -Location $Folder -VM $vm -Datastore $vsanDS -ResourcePool $cluster
Start-Sleep -Seconds 5
}
ERRORS...
New-VM : 10/4/2017 7:48:28 PM New-VM Operation is not valid due to the current state of the object.
At C:\scripts\Clone-Backup-Scripts\Clone-Backup.ps1:17 char:1
+ New-VM -Name "$($vm.Name)-clone" -Location $Folder -VM $vm -Datastore ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
Error can have many causes.
Can you try adding Verbose to the New-VM cmdlet?
Did you already look in the vpxd log to see if there are more details in there?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hey Luc,
I restarted ISE and that resolved it, I read this New/Remove-VM: Operation is not valid due to the current state of the object.
Are you aware of how to avoid this issue, I need to run this in a prod environment on about 100 vm's is it just an issue with ISE, should I just use the native PowerCLI when running in prod?
Which PowerCLI version are you using?
That other thread is relatively old, and not using any of the latest PowerCLI builds.
In prod I would not run this from the ISE, but use the regular PowerShell console.
Load the modules and then run your script.
But are you starting that manually in Prod or from a scheduled task?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
well once I confirm the script is functioning in my lab I will manually execute it from PowerCLI shell in prod this weekend, I'm not confident is ISE I've seen strange issues.
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14409.1012
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1012
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\> Get-PowerCLIVersion
WARNING: The cmdlet "Get-PowerCLIVersion" is deprecated. Please use the 'Get-Module' cmdlet instead.
PowerCLI Version
----------------
VMware PowerCLI 6.5.1 build 5377412
---------------
Component Versions
---------------
VMware Cis Core PowerCLI Component 6.5 build 6230110
VMware VimAutomation Core PowerCLI Component 6.5 build 6234650
VMWare ImageBuilder PowerCLI Component 6.6 build 5299608
VMWare AutoDeploy PowerCLI Component 6.6 build 5299608
VMware Vds PowerCLI Component 6.5 build 5374428
VMware Cloud PowerCLI Component 6.5 build 5375799
VMware HA PowerCLI Component 6.0 build 5314477
VMware HorizonView PowerCLI Component 7.1.0 build 5307191
VMware Licensing PowerCLI Component 6.5 build 5375648
VMware PCloud PowerCLI Component 6.5 build 5376282
VMware Storage PowerCLI Component 6.5 build 5374001
VMware vROps PowerCLI Component 6.5 build 5375723
VMware vSphere Update Manager PowerCLI 6.6 build 5301639
Yes, definitely do not run it from ISE, but go for the PS prompt.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ohh ok wait the latest PowerCLI isn't installable like before right, you import the module from PS console now, is that correct?
Correct, see Updating PowerCLI through the PowerShell Gallery
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Cheers Luc