VMware Cloud Community
DjinjiRinji
Enthusiast
Enthusiast
Jump to solution

Clone VM

Hi I am new to powercli but I am needing a solution for a critical VM.

Is there any script with features below?

1) Create a new clone from online machine
2) Delete the old clone (if exists) but first check the new clone was successfully created
3) Select the best datastore to put the new clone
4) Select the best host to put the new clone
5) Send an e-mail if the task was successful or failed

Thanks for help...

Kindly mark as solved if your questions are answered.
>>>>>
Guillermo R
LinkedIn: https://www.linkedin.com/in/gramallo
Web: http://bakingclouds.com/
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You need to give a bit more information, for example how do you determine what is an old clone.

1) Use the New-VM cmdlet with the VM parameter

2) Get-VM -Name MyVM | Remove-VM -DeletePermanently -Confirm:$false

3) If you use a Datastore Cluster (vSPhere 5), this is supported on the New-VM cmdlet

4) Are your hosts in a cluster ? If yes, DRS will do that for you

5) Use the Send-MailMessage cmdlet


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

You need to give a bit more information, for example how do you determine what is an old clone.

1) Use the New-VM cmdlet with the VM parameter

2) Get-VM -Name MyVM | Remove-VM -DeletePermanently -Confirm:$false

3) If you use a Datastore Cluster (vSPhere 5), this is supported on the New-VM cmdlet

4) Are your hosts in a cluster ? If yes, DRS will do that for you

5) Use the Send-MailMessage cmdlet


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jack84
Contributor
Contributor
Jump to solution

Hi LucD,

I've been reading several of your posts on similar topics, trying to figure out how to automate the deletion of a clone from the previous evening to make way for the new clone.  I have the commands necessary for executing it within PS.  However, I am failing to understand how one would automate the execution of a .ps1 script with Scheduled Tasks.  I believe I am not creating my script properly.  I'm sure that I am missing syntax of some sort.  When I execute the commands manually in PS, it works fine.  If you wouldn't mind walking me through accomplishing such a task from start to finish?  Forgive me for my ignorance. 

Thanks,

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem, did you already look at Alan's Running a PowerCLI Scheduled task  post ?

It contains the basics for running scripts as scheduled tasks.

Try to follow those instructions and let me know what doesn't work eventually.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jack84
Contributor
Contributor
Jump to solution

Yes I have looked at it.  I attempted to do what he talks about.  However, when I run my script the window pops up and disappears and I don’t see the VM deleting itself from within viclient.   I had the connect-viserver command line first then the  get-vm | remove-vm line.  I tested both individually to confirm they hold proper syntax but nothing happens when I run them as a script.  I believe my script is lacking something.

Thanks,

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you also add the Add-PSSnapin cmdlet Alan mentions ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jack84
Contributor
Contributor
Jump to solution

Yes, I tried it by added it into the schedule task arguments and by putting it as the first line of my script.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Some more points to check:

  • did you add the -Confirm:$false parameter to the Remove-VM cmdlet ?
  • did you test the script with the same account as the one you use to run the scheduled task ?
  • any messages ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jack84
Contributor
Contributor
Jump to solution

Here is a sample of my script:

connect-viserver -server x.x.x.x -user admin -password password
get-vm -name *clone* | remove-vm -DeletePermanently -confirm:$false

I am attempting to run this script directly from the PowerCLI console.  My Execution Policy is set to RemoteSigned.  I get 10 informational events logged into the Windows PowerShell log each time I attempt to run the script from PowerCLI.  8 are ID 600, 1 400, and 1 403.

If I run the commands manually it works fine.  I am using the same account that I run the script with as I run them individually.

Thanks,

- Jack

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

These Informational events normally don't indicate errors, but diagnostics from the run of the PowerShell engine.

If I understand it correctly, those lines wort when executed from the PowerCLI prompt. but not when you run them through a scheduled task.

Did you add the Add-PSSnapin to the script, before the COnnect-VIServer line, like Alan says in his post ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jack84
Contributor
Contributor
Jump to solution

Wow, so I believe I figured it out.  My script name was .sp1...  Once I changed it to .ps1, that seemed to resolve the issue.  LucD, thanks for all your help!

Reply
0 Kudos