Script: Mass Export VMs from vCenter Using VM Naming Scheme
Amateur PowerShell script. Feel free to improve it and I will re-post giving credit. Performs a mass OVF export of VMs from vCenter when given vCenter IP, VM folder path, and a string of text contained in the VM(s) name. Broken up into two files (probably can be done in one using functions, but again I’m not a PowerShell pro). I took get-vmfolderpath.ps1 from somewhere on the Internet. I cant remember where I found it though.
# This script is very close to working. For whatever reason, when I build the ovftool command for powershell to
# run it returns with an "invalid object" error. However, when I copy the ovftool command built in the script
# and paste it directly into a Powershell window it runs properly.
# How to use:
# .\VMs-to-OVF.ps1 -vcenter <vcenter_IP/DNS> -vmPath </DC/vm/path/to/VM> -vmNamingScheme <test-vm-name|test-vm-*>
# The command we are trying to construct for the ovftool looks like this:
# ovftool vi://my_vc_server/my_datacenter/vm/my_vm_folder/my_vm_name c:\ovfs\
# Parameters that the script is expecting
param (
[string]$vcenter = $(throw "
vCenter address required.`n
Example:`n
.\VMs-to-OVF.ps1 -vcenter <192.168.1.200>`n
.\VMs-to-OVF.ps1 -vcenter <vcenter.test.com>"),
[string]$vmPath = $(throw "
VM path required.`n
Example:`n
.\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n
test/>`n
Note:`n
- vmPath is the same path displayed in the vSphere 'VMs and Templates'`n
view`n
- the vmPath must always include 'vm/' in between the datacenter`n
and next folder"),
[string]$vmNamingScheme = $(throw "
VM naming scheme required.`n
Example:`n
.\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n
test/> -vmNamingScheme <test-vm-1>`n
.\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n
test/> -vmNamingScheme <test-vm-*>`n"),
[string]$exportLocation = $(throw "
Export location required.`n
Example:`n
.\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n
test/> -vmNamingScheme <test-vm-1> -exportLocation 192.168.1.100:\`n
.\VMs-to-OVF.ps1 -vcenter <vcenterIP/DNS> -vmPath </DATACENTER/vm/`n
test/> -vmNamingScheme <test-vm-*> -exportLocation X:\`n")
#[string]$username = $( Read-Host "Please input user name" ),
#[string]$password = $( Read-Host "Please input user password" -AsSecureString)
)
#Connect-VIServer -Server $vcenter
# $VMs is an array of VM names that will be exported
# $vmNamingScheme gives the pattern we are looking for
$VMs = $(get-vm -name $vmNamingScheme | select name | format-list | out-string)
$VMs = $VMs.replace("Name : ","")
$VMs = $VMs.replace(" ","")
$VMs = $VMs.split("`n")
$VMs = $VMs|?{$_ -ne $VMs[1]}
# This loop iterates through the $VMs array and performs an OVF export, to the location specified below, for each VM name in the array
# $vmPath gives the path to the VM and $vmNamingScheme gives the pattern we are looking for
foreach ($VM in $VMs){
if ($VMs -ne $null){
$ovftoolCmd = "&'C:\Program Files\VMware\VMware OVF Tool\ovftool.exe' vi://$vcenter$vmPath$VM $exportLocation"
# Line below assumes default ovftool install path, specifies the user name, target vCenter, location of the VM, and specifies where the OVF should be output to
#&'C:\Program Files\VMware\VMware OVF Tool\ovftool.exe' $ovftoolCmd
$ovftoolCmd
#Invoke-Expression $ovftoolCmd
}
}
File 2: get-vmfolderpath.ps1
# If this returns a CommandNotFoundException then you need to add the current
# directory location to your PATH variable using the command:
# $env:PATH=$env:PATH+";."
get-vm 'cal-tsip-log1' | get-vmfolderpath
function Get-VMFolderPath
{
<#
.Synopsis
Get vm folder path. From Datacenter to folder that keeps the vm.
This function returns vm folder path. As a parameter it takes the
current folder in which the vm resides. This function can throw
either 'name' or 'moref' output. Moref output can be obtained
using the -moref switch.
get-vm 'vm123' | get-vmfolderpath
Function will take folderid parameter from pipeline
.Example
get-vmfolderpath (get-vm myvm123|get-view).parent
Function has to take as first parameter the moref of vm parent
folder.
DC\VM\folfder2\folderX\vmvm123
Parameter will be the folderX moref
.Example
get-vmfolderpath (get-vm myvm123|get-view).parent -moref
Instead of names in output, morefs will be given.
This is the moref of the parent directory for vm.Our starting
point.Can be obtained in serveral ways. One way is to get it
by: (get-vm 'vm123'|get-view).parent
or: (get-view -viewtype virtualmachine -Filter @{'name'=
'vm123'}).parent
.Parameter moref
Add -moref when invoking function to obtain moref values
AUTHOR: Grzegorz Kulikowski
LASTEDIT: 09/14/2012
NOT WORKING ? #powercli @ irc.freenode.net
https://psvmware.wordpress.com
param(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$folderid,
[switch]$moref
)
$folderparent=get-view $folderid
if ($folderparent.name -ne 'vm'){
if($moref){$path=$folderparent.moref.toString()+'\'+$path}
else{
$path=$folderparent.name+'\'+$path
}
if ($folderparent.parent){
if($moref){get-vmfolderpath $folderparent.parent.tostring() -moref}
else{
get-vmfolderpath($folderparent.parent.tostring())
}
}
}else {
if ($moref){
return (get-view $folderparent.parent).moref.tostring()+"\"+$folderparent.moref.tostring()+"\"+$path
}else {
return (get-view $folderparent.parent).name.toString()+'\'+$folderparent.name.toString()+'\'+$path
}
}
}