Automation

 View Only
  • 1.  error execute powershell script

    Posted Feb 08, 2019 02:01 PM

    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



  • 2.  RE: error execute powershell script

    Posted Feb 08, 2019 02:11 PM

    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?



  • 3.  RE: error execute powershell script

    Posted Feb 08, 2019 02:12 PM

    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.



  • 4.  RE: error execute powershell script

    Posted Feb 08, 2019 02:15 PM

    No you don't.
    When he is using PowerShell v5, the module autoload feature takes care of that.



  • 5.  RE: error execute powershell script

    Posted Feb 08, 2019 02:18 PM

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



  • 6.  RE: error execute powershell script

    Posted Feb 08, 2019 02:20 PM

    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



  • 7.  RE: error execute powershell script

    Posted Feb 08, 2019 02:32 PM

    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



  • 8.  RE: error execute powershell script
    Best Answer

    Posted Feb 08, 2019 02:34 PM

    Use the Set-PowerCLIConfiguration cmdlet to ignore certificate warnings.

    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false



  • 9.  RE: error execute powershell script

    Posted Feb 08, 2019 02:37 PM

    I could resolve thank you very much for your help



  • 10.  RE: error execute powershell script

    Posted Feb 08, 2019 02:15 PM

    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



  • 11.  RE: error execute powershell script

    Posted Feb 08, 2019 02:16 PM

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