PowerCLI

 View Only
  • 1.  Issue running PowerCLI script

    Posted Aug 27, 2013 06:09 PM

    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
      }
    }

    }



  • 2.  RE: Issue running PowerCLI script

    Posted Aug 27, 2013 08:23 PM

    Are you running this from powercli window or from powershell?  The commands look ok and I even tested some of it in my lab.

    Try running it from powercli command prompt



  • 3.  RE: Issue running PowerCLI script

    Posted Aug 27, 2013 10:24 PM

    Markdjones82,

    Thanks for taking a look. I am invoking this via commandline using powershell.exe -file path\filename.ps1

    Is there a way to do the same to envoke PowerCLI directly from the command line. I would need to run this via a task scheduler.

    Thanks in advance.



  • 4.  RE: Issue running PowerCLI script
    Best Answer

    Posted Aug 28, 2013 08:34 AM

    It should be fine with the add-pssnapin you have there, besides it doesn't choke on the Get-VM cmdlet you execute earlier, so that shouldn't be an issue.

    Make sure you run in a 32bit Powershell. The New-VM cmdlet (as well as a handful of others) will only work within a 32bit Ppowershell. See:

    https://www.vmware.com/support/developer/PowerCLI/PowerCLI51R2/powercli51r2-releasenotes.html

    The following PowerCLI features are supported only on the 32-bit version of Windows PowerShell.

        New-OSCustomizationSpec and Set-OSCustomizationSpec

        New-VM and Set-VM (only when used for applying customization specifications)

    When running against vCenter Server or ESX/ESXi versions earlier than 5.0, the following PowerCLI features are supported only on the 32-bit version of Windows PowerShell.

        Invoke-VMScript

        Copy-VMGuestFile

        New-VMGuestRoute, Get-VMGuestRoute, and Remove-VMGuestRoute

        Get-VMGuestNetworkInterface and Set-VMGuestNetworkInterface

        Set-HardDisk (only when used for resizing guest disk partitions)

    If you just run "powershell.exe -file path\filename.ps1" from a cmd on a 64bit OS, it will run the 64bit Powershell by default.

    Run "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe file path\filename.ps1" instead to invoke a 32bit Powershell.



  • 5.  RE: Issue running PowerCLI script

    Posted Aug 28, 2013 12:24 PM

    MKguy & Markdjones82,

    Running it via 32bit worked like a charm. Thanks to both of you for the help, my life just got easier.

    Cheers!