I have the following script that clones me a VM when I run it from powercli version 6.5 it works correctly but when I try from powershell version 5 it shows me the following error.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
##Clonar VM
##Variables
$vcenter_server ="ip"
$vcenter_user ="user"
$vcenter_pwd ="passwd"
$VMsource = "SRV-SERVER"
$esxi = "172.16.1.51"
$newVMclone = "$($VMsource)-clone-$((Get-Date).ToString('MMddyyyy'))"
$oldCloneName = "$($VMsource)-clone-$((Get-Date).AddDays(0).ToString('MMddyyyy'))"
$ds = Get-Datastore | where{$_.Type -eq 'VMFS' -and $_.ExtensionData.Summary.MultipleHostAccess} | Sort-Object -Property FreeSpaceGB -Descending | select -First 1
$esx = Get-Cluster -VM $VMsource | Get-VMHost | Get-Random
##Connect to vCenter
connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd
##Elimina VM de hasta 7 dias
Get-VM -Name $oldCloneName -ErrorAction SilentlyContinue | Remove-VM -DeletePermanently:$true -Confirm:$false
##Clone VM, disco virtual tipo thick y carpeta de almacenaiento de la VM
New-VM -VM $VMsource -Name $newVMclone -VMHost $esxi -DiskStorageFormat Thin -Location "Laboratorios"
## get the .Net View object of the clone VM
$viewCloneVM = Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device -Filter @{"Name" = $sVMsource}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PS C:\Users\Marco\Desktop> .\clonacion.ps1
Get-Datastore : The term 'Get-Datastore' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Marco\Desktop\clonacion.ps1:10 char:7
+ $ds = Get-Datastore | where{$_.Type -eq 'VMFS' -and $_.ExtensionData. ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Datastore:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Use the Set-PowerCLIConfiguration cmdlet to ignore certificate warnings.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
PowerCLI 6.5 and PowerShell 5 are not two different environments.
PowerCLI is a set of modules (in PSSnapin in the old days) that provide cmdlets in PowerShell to work with a vSPhere environment.
Can you perhaps show some screenshots of what you mean by PowerCLI 6.5 and PowerShell
PS: is there a specific reason why you are running this rather old version of PowerCLI?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You need to include the powercli modules at the top. Look at this page for different options
VMware PowerCLI Forum - VMware {code}
I generally run Get-Modules -ListAvailable *VM* | Import modules at the top, but this loads alot of modules which in some cases you might need.
Well what I want to do is create a task scheduled in Windows so that the script is executed but before that it is testing if the script is executed in power shell
No you don't.
When he is using PowerShell v5, the module autoload feature takes care of that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
How do you test your script?
From ISE, VSC, a PS prompt...
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank I must have missed that, I'm been including by default since it switched to modules.
The only reason you would do that is when you didn't install the PowerCLI modules in a folder that is mentioned in $env:PSModulePath
Update: to further clarify, you then also need to give the full path to the modules
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If I already noticed that I was using a very old version of PowerCli, update to a newer version and the script could be executed just showed me a vSphere certificate error
Use the Set-PowerCLIConfiguration cmdlet to ignore certificate warnings.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I could resolve thank you very much for your help