July 29, 2008 - Johan

VMware Powershell script – remove connected iso

In the light of the offically released Virtual Infrastructure Toolkit I decided to create a simple powershell script. I’m planning to create more powershell scripts and post them on virtualfuture.info.

The first one is a script which can remove the connected iso files from the CD drive. Some administrators do not disconnect a CD drive when the have finished use the ISO, the side effects of this behaviour are: the VM regurally polls the CD drive which creates a slight overhead and a VMotion may fail. This script can run unattended e.g. every night so even when an ISO file remains mounted it is ejected everynight. Here’s the script:

Connect-VIServer -Server myVIserver (connect to the VI server)
get-cddrive -VM * |where {$_.ISOPATH -ne $null}|set-cddrive  -nomedia

I think the script speaks for itself.

So if anyone thinks of something to script and don’t know how please leave a comment and maybe we can find a way to script it.

Scripts ESX / Powershell / script / Toolkit / VMware /

Comments

  • Rob Mokkink says:

    Het moet zijn connect-viserver

    • Hi Rob,

      Thanx for the comment a typo can easily be made. In reply of your comment are there any automated tasks you would to have scripted or do you have by any means usefull scripts ?

      Johan

      • Rob Mokkink says:

        Hi Johan,

        I created a couple of scripts, for example the HBA loadbalance script, see http://blogs.vmware.com/vipowershell/2008/07/storage-path-lo.html

        Currently i am busy creating a script that read a .csv file, creates a VM based on a template. But it can also poweroff and remove a VM and redeploy it.

        Also the script has the possibility to be put in a resourcepool or in the clusters default resourcepool.
        Ask Matthijs about it, he is my current collegue at project we are working on right now.

        • Hi Rob,

          I have read the storage-path-lo article on the VMware blog and found in an facinating process.

          The one you are working is just the function Sven asked about, I am axious to see what’s the result of that. 🙂

          I prefer the simple oneliner scripting thingies because they show the real power of powershell and are much easier to understand for people who are starting with scripting / powershell

        • Chris Woods says:

          Hi Rob

          Hows the script to read from a csv file and create VM’s based on a template going. I’ve got as far as creating the VM’s from a csv file but struggling with creating them from a template.

  • Sven Huisman says:

    Hi Johan,

    Can you create a powershell script that disconnects the network interfaces in all virtual machines? This can be usefull in case of a virus-outbreak.

    Another script that would be usefull is a script that reads out a CSV or TXT file (which contains certain parameters like VM-name, memory size, network, template to use) and then uses those parameters to deploy a new VM.

    • Hi Sven,

      That’s easy:
      First you need to connect to a VI or ESX server with the Connect-VIServer (like Rob pointed out)commandlet

      next you use the get-networkadapter commandlet en pipes the output to the set-networkadapter
      the complete commandline will be:
      Get-NetworkAdapter -VM * |set-networkadapter -connected $false

      the otherone I will explain in a future article

      grtx Johan

  • Sven Huisman says:

    Johan,

    how about this:
    list al VM’s which has snapshots, then delete those snapshots.

    • Hi Sven,

      What I like about powershell is the simplicity of the commands and the ease to create powerfull single line scripts, aka one-liners.

      That’s easy as well but it took a little work:
      first I put all the snapshots into a single variable with this command:
      $snap = get-snapshot -vm * this variable can be used as input for the actual removal:
      remove-snapshot -snapshot $snap.

      you can combine the two commands on a single line:
      $snap=get-snapshot -vm * ; remove-snapshot -snapshot $snap

      on the otherhand I cannot emphasize the importace of being carefull with such powerfull commands !

      that’s all

  • Marq says:

    Is it at all possile to that the CD Removal ISO Script and report on VMs that have a CD ISO mounted — I don’t want to remove just identify

  • saurabh says:

    Please let me know where do we have to Run these poweshell scripts in Vcentre or Individual ESX hosts