Automation

 View Only
  • 1.  Powershell syntax to check whether a VM exists

    Posted Nov 09, 2009 10:13 AM

    I've written a Powershell script that prompts the user for a VM host name and stores the value into variable "$desktop". Following this the VM is powered on via the get-vm command i.e. (get-vm -name "$desktop") ..... However, if the hostname does not exist a powershell error is displayed and the script is aborted.

    What I'd like to ask is whether there is a method to query VirtualCenter to check whether the VM hostname exists, prior to issuing get-vm command. Can anyone help with the appropriate syntax?

    Thanks



  • 2.  RE: Powershell syntax to check whether a VM exists
    Best Answer

    Broadcom Employee
    Posted Nov 09, 2009 10:29 AM

    one way of doing this could be:

    $Exists = get-vm -name $desktop -ErrorAction SilentlyContinue
    If ($Exists){
    	Write "VM is there"
    }
    Else {
    	Write "VM not there"
    }

    If you found this information useful, please consider awarding points for Correct or Helpful.

    Alan Renouf



  • 3.  RE: Powershell syntax to check whether a VM exists

    Posted Nov 09, 2009 10:48 AM

    Alan, thanks for the prompt feedback and the issue is now resolved.

    When I initially attempted the code you advised e.g. "$Exists = get-vm -name $desktop", I still recieved the same error I previosuly experienced. However, I then added "-ErrorAction SilentlyContinue", which allows the script to continue without displying the error.

    Thanks