# encoding: utf-8
# api: powershell
# title: ESXiMgmt module sample 5
# description: This sample register all the virtual machines laying as folders on a ESXi host.
# version: 4.1
# type: module
# author: Alexander Petrovskiy
# license: CC0
# x-poshcode-id: 2914
# x-archived: 2011-08-25T00:25:36
# x-published: 2011-08-14T08:37:00
#
# The real business case is as the following: servers I have for tests configured with RAID0 array. After the drive space is over I decided to rebuild disks to RAID1. For this purpose I began copying all the VMs to a Windows host at Friday evening, on saturday I logged into iDRAC, rebuilt the array, reinstalled OS and began copying all the VMs back to now expanded drive space.
# After the copuing finished, I refused to register VMs manually preferring to write a script. That is what I posted today.
#
#######################################################################################################################
# File: ESXiMgmt_register_all_virtual_machines_sample.ps1 #
# Author: Alexander Petrovskiy #
# Publisher: Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com #
# Copyright: © 2011 Alexander Petrovskiy, SoftwareTestingUsingPowerShell.WordPress.Com. All rights reserved. #
# Prerequisites: The module was tested with Vmware ESXi 4.1 U1 on the server side and #
# Vmware PowerCLI 4.1 U1 #
# plink.exe 0.60.0.0 #
# Usage: To load this module run the following instruction: #
# Import-Module -Name ESXiMgmt -Force #
# Please provide feedback in the SoftwareTestingUsingPowerShell.WordPress.Com blog. #
#######################################################################################################################
param([string]$Server,
[string]$User,
[string]$Password,
[string]$DatastoreName,
[string]$Drive
)
# USAGE: .\ESXiMgmt_register_all_virtual_machines_sample.ps1 192.168.1.1 root 123 datastore3 host1ds3
cls
Set-StrictMode -Version Latest
Import-Module ESXiMgmt -Force;
Connect-ESXi -Server $Server -Port 443 `
-Protocol HTTPS -User $User -Password $Password `
-DatastoreName $DatastoreName -Drive $Drive;
dir "$($Drive):" | %{ `
# supposedly, all the *.vmx files have
# the same names as their folders
# like VMName\VMName.vmx
if (Test-Path "$($_.FullName)\$($_.Name).vmx")
{
Register-ESXiVM -Server $Server `
-User $User -Password $Password `
-Path "/vmfs/volumes/$($DatastoreName)/$($_.Name)/$($_.Name).vmx" `
-OperationTImeout 5;
}
}