VMware PowerCLI - suspend loop - vmware

I'm new to PowerCLI but not to PS.
I have a machine with ESXI with a lot of VM's.
I want to iterate through all the VM's and suspend them every day at 11:59 PM.
How can i do that?
Thank you,
Roxbip

at time:11:59 /every:M,T,W,Th,F,S,Su pssuspend.exe vmware-vmx.exe
and make sure you have PsSuspend installed.
(but i still think this question should be migrated to superuser.com , its not about programming.)

You can make use of the Suspend-VM cmdlet to do that.
Personally, I'd create a scheduled task that runs daily at 11:59PM and kicks off a script. The script could look like the following:
Connect-VIServer -Server esxi.fqdn
Get-VM | Suspend-VM -Confirm:$false
Disconnect-VIServer -Confirm:$false
Credential handling is the next issue. You can either include it as part of the script or make use of the credential store.
If you want to use the credential store option, that can look like the following:
New-VICredentialStoreItem -Host esxi.fqdn -User Admin -Password pass

Related

How can I get a list of Vcenter servers in my environment using Powershell or risk api

I’m trying to get a list of Vcenter servers in my environment. Is there a Powershell command let that can get a list of Vcenter servers using Powershell or risk api?
The vCenter server generally serves as the authentication point and source of the API services, so this isn't generally something that's easy to do.
There is one caveat though, and that is when/if Linked Mode is enabled. In those cases you could use PowerCLI (a set of PowerShell modules that are easy to download from the PowerShell Gallery) and use the following commands:
Connect-VIServer vcenter-name.fqdn -AllLinked
$global:DefaultVIServers
To be very clear, the above will not provide all the vCenters in your environment, but only the ones that are in some form of linked mode.

Remove Machines from VMware Horizon View with PowerCLI

We often add and remove machines from a manual Desktop Pool on our Horizon server. Registering the machine is done with an install script. When we're done with a machine we want to un-register it from the server. The only way we have found to do this is to log into the web portal and manually delete each one. This is cumbersome and time-consuming when we have large numbers of machines to un-register.
The machines that will need to be un-registered will have similar names. Is there a way to automate this with PowerCLI?
Of course you can,
$VM = Get-VM -Name nameOfVM
Remove-VM $VM
Full example you can find here
Not sure if you had this question answered already, but there are a couple of ways that you can do this, depending on the version of PowerCLI that you have installed. The easiest way right now is to get the latest version of PowerCLI and make sure that you install the View module along with it. From here, peruse through the View API (https://code.vmware.com/web/dp/explorer-apis?id=58)
VMware also has a helper PSM1 script in their Example gallery, which is available via github: https://github.com/vmware/PowerCLI-Example-Scripts/tree/master/Modules/VMware.Hv.Helper

What is a good API or way to create VMs programatically?

I'm working on a project where I think of distributing tasks to VMs that are dynamically managed (created, destroyed, paused, run processes from host, etc). I was wondering what would be a good approach or API to accomplish the management of the VMs. Below are some examples of what I'm thinking but I wanted to get some guidance on the best approach.
Vagrant-binding: it looked perfect but it out of date and not supported.
Oracle Tools: the vagrant module looks interesting but there isn't much documentation and I'm a bit confused on how to actually use it.
VirtualBox sdk: I'm a bit confused on the setup of this.
As you can see from the examples I was thinking in Java but I'm open to working on other languages. This project academic in nature and I'm a student so I know that this might not be the most practical thing to do but I wanted to see if it possible and what would be best way to accomplish it.
I have recently created a program to manage the VMs at my work. I used object-pascal (Delphi) to create the GUI and then did all the heavy lifting using vboxmanage commands that are ran as a process through cmd but called from my program. Oracle has a nice list of available commands here
For example
List running Vms
VBoxManage.exe list runningvms
Import a VM
VBoxManage.exe import (VM_To_Import_Location) --vsys 0 --vmname (Name_of_VM) --unit 11 --disk (Where_You_Want_VM_Stored)
Start a VM
VBoxManage.exe startvm (Name_Of_VM) --type headless
Take a Snapshot
VBoxManage.exe snapshot (VM_Name) take (Snapshot_Name) -desc "My Snapshot Description"
Restore a Snapshot
Vboxmanage.exe snapshot (VM_Name) restore (Snapshot_Name)
There are many more for pretty much everything you would want to do with a VM.

vmware - revert to snapshot from within the GUEST?

i have virtual machines running on vmware ESXi and vmware workstation.
i need to execute "revert to snapshot" from inside the guest.
i have done so much searching, but all solutions proposed so far suggest doing it from "outside" - either some external machine or the host itself.
other workarounds suggest to enable automatic reverting to snapshot on power off event.
please do not suggest anything in that direction. i really need to execute it from within the guest. for example:
as scheduled task
as batch script (at the end of completing some other tasks)
edit:
this is the reason why i think there must be some way to achieve this: inside the guest there are "vmare tools" running as system service. so i would expect this component to also expose a functionality to trigger the host / hypervisor reverting the current VM to snapshot.
if this is not possible currently it should be implemented as new feature :)
in case it's currently not possible to execute it "from inside": that would also be an "answer" ...
I've actually done this pretty recently, try this:
Install VMware vSphere PowerCLI 5.1 (it's a command line scripting interface for ESX)
Write a script (perhaps in Notepad) that contains the following code:
Connect-VIServer <vCenter Server IP>
Set-VM <VM name> -Snapshot <Snapshot name> -Confirm:$false
This will connect to your vCenter server and revert your VM to the specified snapshot.
Save the script as revert_snapshot.ps1 (PowerShell file extension)
Using Windows Task Schedule, create a new tasks. The General and Triggers tabs are self
explanatory, but the Actions tab is where you'll configure the scheduled tasks to launch
your PowerShell script.
For 'Action' select 'Start a Program'. Under 'Program/script', enter the following:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
For the 'Add arguments' field, you'll specify the path of your PowerShell script:
-psc "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "<path to your script>"
note: vim.psc1 is not available in the latest version of PowerCLI.
Save your task and run it manually as a test. Be patient as sometimes the cmdlet for logging into vCenter (Connect-VIServer) can take a few seconds to connect.

How do you deploy cron jobs to production?

How do people deploy/version control cronjobs to production? I'm more curious about conventions/standards people use than any particular solution, but I happen to be using git for revision control, and the cronjob is running a python/django script.
If you are using Fabric for deploment you could add a function that edits your crontab.
def add_cronjob():
run('crontab -l > /tmp/crondump')
run('echo "#daily /path/to/dostuff.sh 2> /dev/null" >> /tmp/crondump')
run('crontab /tmp/crondump')
This would append a job to your crontab (disclaimer: totally untested and not very idempotent).
Save the crontab to a tempfile.
Append a line to the tmpfile.
Write the crontab back.
This is propably not exactly what you want to do but along those lines you could think about checking the crontab into git and overwrite it on the server with every deploy. (if there's a dedicated user for your project.)
Using Fabric, I prefer to keep a pristine version of my crontab locally, that way I know exactly what is on production and can easily edit entries in addition to adding them.
The fabric script I use looks something like this (some code redacted e.g. taking care of backups):
def deploy_crontab():
put('crontab', '/tmp/crontab')
sudo('crontab < /tmp/crontab')
You can also take a look at:
http://django-fab-deploy.readthedocs.org/en/0.7.5/_modules/fab_deploy/crontab.html#crontab_update
django-fab-deploy module has a number of convenient scripts including crontab_set and crontab_update
You can probably use something like CFEngine/Chef for deployment (it can deploy everything - including cron jobs)
However, if you ask this question - it could be that you have many production servers each running large number of scheduled jobs.
If this is the case, you probably want a tool that can not only deploy jobs, but also track success failure, allow you to easily look at logs from the last run, run statistics, allow you to easily change the schedule for many jobs and servers at once (due to planned maintenance...) etc.
I use a commercial tool called "UC4". I don't really recommend it, so I hope you can find a better program that can solve the same problem. I'm just saying that administration of jobs doesn't end when you deploy them.
There are really 3 options of manually deploying a crontab if you cannot connect your system up to a configuration management system like cfengine/puppet.
You could simply use crontab -u user -e but you run the risk of someone having an error in their copy/paste.
You could also copy the file into the cron directory but there is no syntax checking for the file and in linux you must run touch /var/spool/cron in order for crond to pickup the changes.
Note Everyone will forget the touch command at some point.
In my experience this method is my favorite manual way of deploying a crontab.
diff /var/spool/cron/<user> /var/tmp/<user>.new
crontab -u <user> /var/tmp/<user>.new
I think the method I mentioned above is the best because you don't run the risk of copy/paste errors which helps you maintain consistency with your version controlled file. It performs syntax checking of the cron tasks inside of the file, and you won't need to perform the touch command as you would if you were to simply copy the file.
Having your project under version control, including your crontab.txt, is what I prefer. Then, with Fabric, it is as simple as this:
#task
def crontab():
run('crontab deployment/crontab.txt')
This will install the contents of deployment/crontab.txt to the crontab of the user you connect to the server. If you dont have your complete project on the server, you'd want to put the crontab file first.
If you're using Django, take a look at the jobs system from django-command-extensions.
The benefits are that you can keep your jobs inside your project structure, with version control, write everything in Python and configure crontab only once.
I use Buildout to manage my Django projects. With Buildout, I use z3c.recipe.usercrontab to install cron jobs in deploy or update.
You said:
I'm more curious about conventions/standards people use than any particular solution
But, to be fair, the particular solution will depend in your environment and there is no universal elegant silver bullet. Given that you happen to be using Python/Django, I recommend Celery. It is an asynchronous task queue for Python, which integrates nicely with Django. And, on top of the features that it gives as an asynchronous task queue, it also has specific features for periodic tasks.
I have personally used the django-celery-beat integration and it integrates perfectly with Django settings and behaves correctly in distributed environments. If your periodic tasks are related to Django stuff, I strongly recommend to take a look at Celery I started using it only for certain asynchronous mailing and ended up using it for a lot of asynchronous tasks + periodic sanity checks and other web application maintenance stuff.