Solved

Script for automating vmware

Posted on 2008-10-24
10
746 Views
Last Modified: 2012-05-05
We have two VMware ESX servers in production, and also use the Virtual Center for management. I downloaded and installed the vmware VI3 toolkit and Windows Powershell so that we can automate such things as deleting and cloning our virtual servers. I can open the toolkit command window and execute the commands with no problem, but how would I make a script that I can schedule? I tried making one in notepad and giving it a ps1 extension, but the only thing that happens is a command window pops up then goes away immediately. I am totally new to scripting with powershell/vmware. The bad thing is, I can't even see what is going on because I don't know how to "pause" the script to see what is happening.
Here is what I put into notepad and saved as script.ps1 :

connect-esx -server 192.168.1.249 -user root -password vmware
remove-vm (get-vm "eservice clone") -deletefromdisk
0
Comment
Question by:ilguybob
  • 5
  • 4
10 Comments
 
LVL 18

Accepted Solution

by:
BSonPosh earned 500 total points
ID: 22799283
try adding this to the top of your script

add-pssnapin VMware.VimAutomation.Core -ea 0
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22799307
I do not believe there is a connect-esx. Perhaps you are looking for connect-viserver or get-vc

(as a side note... probably not a good idea to store your password in clear text. Look here on other options http://bsonposh.com/archives/338)
0
 
LVL 12

Expert Comment

by:S00007359
ID: 22800019
you'd be better of with some scripting tools, where you can compile etc, to see/debug your scripts. Trying using this link: http://www.primalscript.com/
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:ilguybob
ID: 22821780
The first suggestion appeared to work, but I have the virtual machine that was deleted still in the inventory, but it is in italics and says orphaned in parenthesis. I know I can remove it manually, but is there another script entry I could put in to do this for me?
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22821843
You can use Remove-Inventory to remove it.

Type this at the prompt for more on the cmdlet
get-help Remove-Inventory -full

You may even be able to do this
get-vm <name> | remove-Inventory
0
 
LVL 1

Author Comment

by:ilguybob
ID: 22823812
If I do that after the delete command, I get an error that the vm cannot be found, even though it says it is there and orphaned. If I run the remove-inventory command first, it gives the same "orphaned" state and I canot run the delete command (vm not found).
Maybe I am looking at this the wrong way? We are trying to come up with an automated way to clone our vm servers and delete the old ones once a week to a different esx server. Works great if we do it manually. If we schedule it, it cannot run because there is not enough space on that server to have more than one copy of the cloned vm's. It seems like this would be a pretty common thing to do and I am surprised there is no automated way of doing it.
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22823889
Perhaps I was confused. I thought you were just trying to cleanup.

What you want is Remove-VM

Remove-VM VMName -DeleteFromDisk
0
 
LVL 1

Author Comment

by:ilguybob
ID: 22908915
That works, but always prompts me to make sure I want to execute it. I tried adding /y -y and just plain y at the end of the delete command. Is there a parameter that will execute the command without prompting?
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22922627
Try this

Remove-VM VMName -DeleteFromDisk -Confirm:$false
0
 
LVL 1

Author Closing Comment

by:ilguybob
ID: 31509771
Bingo! Thanks a lot!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vmware PowerCLI script 4 97
Virtual Machine Consolidation needed status 6 99
How to Shrink a VMware VMDK 4 71
vSphere HA Warning 1 41
Will try to explain how to use the VMware feature TAGs in the VMs and create Veeam Backup Jobs using TAGs. Since this article is too long, I will create second article for the Veeam tasks.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Teach the user how to convert virtaul disk file formats and how to rename virtual machine files on datastores. Open vSphere Web Client: Review VM disk settings: Migrate VM to new datastore with a thick provisioned (lazy zeroed) disk format: Rename a…
This Micro Tutorial walks you through using a remote console to access a server and install ESXi 5.1. This example is showing remote access and installation using a Dell server. The hypervisor is the very first component of your virtual infrastructu…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question