VMware Cloud Community
marco2601
Contributor
Contributor
Jump to solution

error execute powershell script

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

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Use the Set-PowerCLIConfiguration cmdlet to ignore certificate warnings.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
sjesse
Leadership
Leadership
Jump to solution

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.

0 Kudos
marco2601
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

LucD
Leadership
Leadership
Jump to solution

How do you test your script?
From ISE, VSC, a PS prompt...


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

0 Kudos
sjesse
Leadership
Leadership
Jump to solution

Thank I must have missed that, I'm been including by default since it switched to modules.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
marco2601
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Use the Set-PowerCLIConfiguration cmdlet to ignore certificate warnings.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false


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

0 Kudos
marco2601
Contributor
Contributor
Jump to solution

I could resolve thank you very much for your help

0 Kudos