Difference between revisions of "Script Extracts and Examples (PowerCLI)"

Jump to navigation Jump to search
→‎Virtual Machine: Added "Migrate VMs Between vCentres"
m (→‎Add Attributes to VMs from CSV: Forgot to end source)
(→‎Virtual Machine: Added "Migrate VMs Between vCentres")
Line 677: Line 677:
Stop-Transcript
Stop-Transcript
</source>
</source>
=== Migrate VMs Between vCentres ===
The following script will migrate virtual machines from one vCentre to another, so long as both have a shared datastore.
<source lang="Powershell">
<# ========================================================================================================================================
  Virtual Machine IntraVC Migrater
  =========================================================================================================================================
  Simon Strutt        Jan 2012
  =========================================================================================================================================
  Moves VM's from one vCentre to another (requires VMs to be on storage visible to both vCentres)
Version 1
  - Initial creation
 
  ========================================================================================================================================#>
 
$SourceVC = "uka-sr-vcent-10"
$SourceVapp = "GBR 4 Labs"
$DestVC = "ukb-sr-vcent-10"
$DestCluster = "UKA_LabManager"
$DestFolder = "PSC 3.5.5"
$UserFile = "User.fil"
$PassFile = "Pass.fil"
# Functions ---------------------------------------------------------------------------------
function Log ($text) {
    $stamp = (Get-Date).ToString("HH:mm:ss.fff")
    Write-Host "$stamp | $text"
}
# Business part of script -------------------------------------------------------------------
Start-Transcript -Path VM-Move-VC.log -Append
# Load password credential from encrypted file
try {
    $pass = Get-Content $PassFile -errorAction Stop | ConvertTo-SecureString
    $user = Get-Content $UserFile -errorAction Stop
    $cred = New-Object System.Management.Automation.PsCredential($user, $pass)
} catch {
    Log "ERROR: Failed to load credentials to use"
    Log $_
    Exit
}
# Disconnect any existing VI Server sessions
if ($DefaultVIServers.Count) {
    Log("Disconnect existing vCentre server connections...")
    Disconnect-VIServer -Server * -Force -Confirm:$false
}
# Connect to source VC
try {
    Log "Connecting to $SourceVC"
    $VCconn = Connect-VIServer -Server $SourceVC -Credential $cred -errorAction Stop
} catch {
    Log("Unable to connect to vCentre - " + $_)
    Exit
}
# Get list of VMs to move
$VMs = Get-VM -Location (Get-vApp $SourceVapp) | Sort
Log "VMXs to reregister..."
$VMs2Move = @()
foreach ($vm in $VMs) {
    $vm2move = "" | Select Name, Path
    $vm2move.Name = $vm.name
    $vm2move.Path = $vm.ExtensionData.Config.Files.VmPathName
    $VMs2Move += $vm2move
    Log ($vm2move.Name + " " + $vm2move.Path)
}
#$VMs | Get-View | %{$_.Config.Files.VmPathName} | Sort
# Unregister VMs
foreach ($vm in $VMs) {
    Log ("Unregister " + $vm.Name)
    Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false
}
Disconnect-VIServer -Server $VCconn -Confirm:$false
# Connect to destination VC
try {
    Log "Connecting to $DestVC"
    $VCconn = Connect-VIServer -Server $DestVC -Credential $cred -errorAction Stop
} catch {
    Log("Unable to connect to vCentre - " + $_)
    Exit
}
# Register VMs
foreach ($vm in $VMs2Move) {
    Log ("Register " + $vm.Name)
    New-VM -VMFilePath $vm.Path -VMHost (Get-Cluster $DestCluster | Get-VMHost | Get-Random) -Location (Get-Folder $DestFolder)
}
Disconnect-VIServer -Server $VCconn -Confirm:$false
Stop-Transcript
<source>


== ESX ==
== ESX ==

Navigation menu