Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Name: VM_Image_Deployment.ps1
- Cross vCenter Clone Operation across different vCenter Servers.
- .DESCRIPTION
- This script creates and move a vCenter clone across two or multiple vCenter Servers which can either be part of the
- same or different SSO Domain. It clones the original VM with '-clone' at the end unless told otherwise. Moves the
- clone VM to the selected vCenter's cluster, datstore and assinged the designated portgroup as selected.
- .PARAMETER <Parameter_Name>
- credential
- vcname
- clusters
- datastores
- portgroups
- sourcevmname
- targetvmname
- .INPUTS
- None
- .OUTPUTS
- <Path>/VDIenvironments.csv
- .NOTES
- Version: 0.0.9
- Author: Carlhens Baptiste, David Lang
- Creation Date: 03/25/2019
- Version History
- 0.0.1 2019-03-25 Initial script development.
- 0.0.2 2019-05-09 Switching environment input from csv to vCenter live data.
- 0.0.3 2019-05-10 Implement vCenter server login.
- 0.0.4 2019-06-19 Added clone vm function, error handling and bug fixes.
- 0.0.5 2019-06-20 Fixed windows size, text alignment, error handling and bug fixes.
- 0.0.6 2019-06-21 Implemented AD integration. Added more error handling.
- 0.0.7 2019-06-25 Fixed destination path to match source path for consistency.
- 0.0.8 2019-06-26 Fixed AD integration bug in script
- 0.0.9 2019-07-04 Added option to select a folder from the selected vCenter server folders.
- .EXAMPLE
- powershell.exe -executionpolicy bypass -file "<Path>/VM_Image_Deployment.ps1"
- #>
- #set global variable
- #import environment csv location path
- $filePath = "K:\Scripts\FileToRead"
- $vdiEnvironments = Import-Csv "$filePath\VDIenvironments.csv"
- $lstEnv = $vdiEnvironments.Env | Sort-Object -Unique
- #XAML for GUI
- $inputXML = @"
- <Window x:Class="VM_Image_Deployment.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:VM_Image_Deployment"
- mc:Ignorable="d"
- Title="Image Deployment (Beta Ver 0.0.9)" ResizeMode="NoResize"
- Height="455"
- Width="1010" >
- <Grid Background="#FFEEEEEE">
- <Label Content="Environment"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- />
- <ComboBox x:Name="boxEnvironment"
- Margin="10,25,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- Width="190"
- />
- <Label Content="Source vCenter"
- Margin="0,50,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- />
- <ComboBox x:Name="boxSrcVCenter"
- Margin="10,75,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- Width="190"
- />
- <Label Content="Source VM"
- Margin="0,155,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- />
- <ListBox x:Name="listSrcVMs"
- Margin="10,180,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="150"
- Width="200"
- />
- <Label Content="Snapshot Description"
- Margin="0,331,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- />
- <TextBox x:Name="textSnapshotDescription"
- Text="Type a description of the target VM snapshot."
- TextWrapping="Wrap"
- Margin="10,357,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="54"
- Width="200"
- />
- <Label Content="Target vCenter servers to deploy image"
- Margin="220,0,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- />
- <Grid HorizontalAlignment="Left"
- Background="#FFC8C8C8"
- Margin="230,30,0,0"
- VerticalAlignment="Top"
- Height="330"
- Width="755" >
- <Label Content="Target VCenters"
- Margin="0,0,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- />
- <ListView x:Name="listTgtVCenters"
- SelectionMode="Multiple"
- Margin="10,25,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="295"
- Width="735" >
- <ListView.View>
- <GridView>
- <GridViewColumn Width="30">
- <GridViewColumn.CellTemplate>
- <DataTemplate>
- <CheckBox IsChecked="{Binding RelativeSource={
- RelativeSource AncestorType={x:Type ListViewItem}
- }, Path=IsSelected}"
- />
- </DataTemplate>
- </GridViewColumn.CellTemplate>
- </GridViewColumn>
- <GridViewColumn Header="Name"
- DisplayMemberBinding ="{Binding 'Name'}"
- Width="180"
- />
- <GridViewColumn Header="DC"
- DisplayMemberBinding ="{Binding 'Datacenter'}"
- Width="35"
- />
- <GridViewColumn Header="Target Cluster"
- Width="160" >
- <GridViewColumn.CellTemplate>
- <DataTemplate>
- <ComboBox ItemsSource="{Binding 'Clusters'}"
- SelectedItem="{Binding Path='TargetCluster', Mode=TwoWay}"
- Width="145"
- />
- </DataTemplate>
- </GridViewColumn.CellTemplate>
- </GridViewColumn>
- <GridViewColumn Header="Target Datastore"
- Width="160" >
- <GridViewColumn.CellTemplate>
- <DataTemplate>
- <ComboBox ItemsSource="{Binding 'Datastores'}"
- SelectedItem="{Binding Path='TargetDatastore', Mode=TwoWay}"
- Width="145"
- />
- </DataTemplate>
- </GridViewColumn.CellTemplate>
- </GridViewColumn>
- <GridViewColumn Header="Target PortGroup"
- Width="160" >
- <GridViewColumn.CellTemplate>
- <DataTemplate>
- <ComboBox ItemsSource="{Binding 'Portgroups'}"
- SelectedItem="{Binding Path='TargetPortgroup', Mode=TwoWay}"
- Width="145"
- />
- </DataTemplate>
- </GridViewColumn.CellTemplate>
- </GridViewColumn>
- </GridView>
- </ListView.View>
- </ListView>
- </Grid>
- <Button x:Name="buttonDeploy"
- Content="Deploy"
- Margin="0,0,19,17"
- HorizontalAlignment="Right"
- VerticalAlignment="Bottom"
- Width="75"
- />
- <Label Content="Source VM name"
- Margin="235,365,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- />
- <TextBox x:Name="TextSourceVM"
- TextWrapping="Wrap"
- Margin="235,390,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Width="250"
- Height="20"
- IsEnabled = "False"
- />
- <Label Content="Target VM name"
- Margin="500,365,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- />
- <TextBox x:Name="textTargetVM"
- TextWrapping="Wrap"
- Margin="500,390,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Width="250"
- Height="20"
- />
- <Label Content="Source vCenter Folder"
- Margin="0,102,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- />
- <ComboBox x:Name="boxSrcVCenterFolder"
- Margin="10,127,0,0"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- Height="25"
- Width="190"
- />
- </Grid>
- </Window>
- "@
- ##begin function declaration
- function Get-FormVariables{
- Write-Host "Found the following interactive elements in the form:" -ForegroundColor Cyan
- Get-Variable WPF*
- }
- function TestUserCredential
- {
- #Get user credentials
- $global:credential = Get-Credential -Message "Enter Your Credentials (Domain\Username)"
- if ($credential -eq $Null)
- {
- Write-Host "Please enter your username in the form of Domain\UserName and try again" -BackgroundColor Black -ForegroundColor Yellow
- Break
- }
- #Parse provided user credentials
- $DomainNetBIOS = $credential.username.Split("{\}")[0]
- $UserName = $credential.username.Split("{\}")[1]
- $Password = $credential.GetNetworkCredential().password
- Write-Host "`n"
- Write-Host "Checking Credentials for $DomainNetBIOS\$UserName" -BackgroundColor Black -ForegroundColor White
- Write-Host "***************************************"
- If ($DomainNetBIOS -eq $Null -or $UserName -eq $Null)
- {
- Write-Host "Please enter your username in the form of Domain\UserName and try again" -BackgroundColor Black -ForegroundColor Yellow
- TestUserCredential
- }
- # Checks if the domain in question is reachable, and get the domain FQDN.
- Try
- {
- $DomainFQDN = (Get-ADDomain $DomainNetBIOS).DNSRoot
- }
- Catch
- {
- Write-Host "Error: Domain was not found: " $_.Exception.Message -BackgroundColor Black -ForegroundColor Red
- Write-Host "Please make sure the domain NetBios name is correct, and is reachable from this computer" -BackgroundColor Black -ForegroundColor Red
- Break
- }
- #Checks user credentials against the domain
- $DomainObj = "LDAP://" + $DomainFQDN
- $DomainBind = New-Object System.DirectoryServices.DirectoryEntry($DomainObj,$UserName,$Password)
- $DomainName = $DomainBind.distinguishedName
- If ($DomainName -eq $Null)
- {
- Write-Host "Domain $DomainFQDN was found: True" -BackgroundColor Black -ForegroundColor Green
- $UserExist = Get-ADUser -Server $DomainFQDN -Properties LockedOut -Filter {sAMAccountName -eq $UserName}
- If ($UserExist -eq $Null)
- {
- Write-Host "Error: Username $Username does not exist in $DomainFQDN Domain." -BackgroundColor Black -ForegroundColor Red
- Write-Host "Validate username and re-enter your username in the for form of Domain\UserName and try again" -BackgroundColor Black -ForegroundColor Red
- TestUserCredential
- }
- Else
- {
- Write-Host "User exists in the domain: True" -BackgroundColor Black -ForegroundColor Green
- If ($UserExist.Enabled -eq "True")
- {
- Write-Host "User Enabled: " $UserExist.Enabled -BackgroundColor Black -ForegroundColor Green
- }
- Else
- {
- Write-Host "User Enabled: " $UserExist.Enabled -BackgroundColor Black -ForegroundColor RED
- Write-Host "Enable the user account in Active Directory, Then check again" -BackgroundColor Black -ForegroundColor RED
- Break
- }
- If ($UserExist.LockedOut -eq "True")
- {
- Write-Host "User Locked: " $UserExist.LockedOut -BackgroundColor Black -ForegroundColor Red
- Write-Host "Unlock the User Account in Active Directory, Then check again..." -BackgroundColor Black -ForegroundColor RED
- Break
- }
- Else
- {
- Write-Host "User Locked: " $UserExist.LockedOut -BackgroundColor Black -ForegroundColor Green
- }
- }
- Write-Host "Authentication failed for $DomainNetBIOS\$UserName with the provided password." -BackgroundColor Black -ForegroundColor Red
- Write-Host "Please confirm the password, and try again..." -BackgroundColor Black -ForegroundColor Red
- $global:credential = $Null
- Break
- }
- Else
- {
- Write-Host "SUCCESS: The account $Username successfully authenticated against the domain: $DomainFQDN" -BackgroundColor Black -ForegroundColor Green
- Write-host "`n"
- }
- }
- function xNew-VM{
- param (
- [string]$sourceVCName,
- [string]$sourceClusterName,
- [string]$sourceFolderName,
- [string]$sourceDatastoreName,
- [string]$sourceVMName,
- [string]$destVCName,
- [string]$destClusterName,
- [string]$destFolderName,
- [string]$destDatastoreName,
- [string]$destPortgroupName,
- [string]$destVMName
- )
- $location = 'vm'
- $testfolder = Get-Folder -Server $destVCName -Name $destFolderName -ErrorAction SilentlyContinue
- if (!($testfolder)){
- Write-host "`n"
- Write-warning "Folder $($destFolderName) does not exist in $($destVCName)"
- Write-warning "Folder $($destFolderName) will be created on $($destVCName) to complete task"
- try {
- New-Folder -Server $destVCName -Name $destFolderName -Location $location -ErrorAction Stop
- if ($? -eq $false) {throw $error[0].exception}
- }
- catch [Exception]
- {
- Write-host "`n"
- Write-Error "Unable to create folder $($destFolderName) in $($destVCName)"
- Write-Host "Ending Script"
- Break
- }
- }
- #Destination server variables
- $destHost = Get-Cluster -Server $destVCName -Name $destClusterName | Get-VMHost | `
- Where {$_.ConnectionState -eq "Connected"} | Get-Random -Count 1
- $destFolder = Get-Folder -Server $destVCName -Name $destFolderName
- $destDatastore = Get-Datastore -Server $destVCName -Name $destDatastoreName
- $destPorgroup = Get-VDPortgroup -Server $destVCName -Name $destPortgroupName
- #New VM Variables
- $cloneVM = Get-VM -Name $sourceVMName -Server $sourceVCName
- $cloneVMDatastore = Get-Datastore -Server $sourceVCName -Name $sourceDatastoreName
- $cloneVMLocation = Get-Folder $sourceFolderName -Type VM -Server $sourceVCName
- $cloneVMResourcePool = Get-Cluster $sourceClusterName -Server $sourceVCName
- #Clone the VM to be moved in the source vCenter
- Write-Host "`nCloning $($cloneVM)" -ForegroundColor DarkCyan
- try
- {
- New-VM -Server $sourceVCName -Name $destVMName -VM $cloneVM -Datastore $cloneVMDatastore -Location $cloneVMLocation `
- -DiskStorageFormat Thin -ResourcePool $cloneVMResourcePool -WarningAction SilentlyContinue
- }
- catch
- {
- Write-Error -Message $($_.Exception.Message)
- Break
- }
- #Move the VM to the destination vCenter
- Write-Host "Relocating $($cloneVM) to vCenter $($destVCName)" -ForegroundColor DarkCyan
- try
- {
- Move-VM -Server $sourceVCName -VM $destVMName -VMotionPriority High -Destination $destHost -Datastore $destDatastore `
- -InventoryLocation $destFolder -Portgroup $destPorgroup -Confirm:$false -WarningAction SilentlyContinue
- }
- catch
- {
- Write-Error -Message $($_.Exception.Message)
- Break
- }
- }
- ##end function declaration
- #validate if user credentials are valid or older than 3 hours
- if (!($credential)){
- TestUserCredential
- }
- #load XAML into Form GUI
- $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
- [xml]$xaml = $inputXML
- $reader=(New-Object System.Xml.XmlNodeReader $xaml)
- $form=[Windows.Markup.XamlReader]::Load( $reader )
- #bind XAML elements to PS variables starting with "WPF"
- $xaml.SelectNodes("//*[@Name]") |
- foreach-object {
- "Trying item $($_.Name)"
- try {Set-Variable -Name "WPF$($_.Name)" -Value $form.FindName($_.Name) -ErrorAction Stop}
- catch {Throw}
- }
- Get-FormVariables
- #load "Environments" into box
- $lstEnv | ForEach-Object {$wpFBoxEnvironment.Items.Add($_)}
- #event: when Environment is selected
- $wpFBoxEnvironment.Add_DropDownClosed({
- #empty VCenters inside form
- $wpFBoxSrcVCenter.Items.Clear()
- $wpFListTgtVCenters.Items.Clear()
- #empty Source VMs inside form
- $wpFListSrcVMs.Items.Clear()
- #empty vCenters folder inside form
- $wpFBoxSrcVCenterFolder.Items.Clear()
- #validate VI servers connetion and connect if session doesn't exist
- $VServers = $vdiEnvironments |
- Where-Object Env -like $wpFBoxEnvironment.Text
- #notify use to wait for vCenter server login
- Write-Host "Authenticating to all $($wpFBoxEnvironment.Text) servers. Please wait..." -ForegroundColor Cyan
- ForEach ($VServer in $VServers){
- $sessionID = ($global:DefaultVIServers | Where-Object -FilterScript {$_.Name -like $VServer.Name}).sessionId
- if ($sessionID) {Connect-VIServer -Server $VServer.Name -Session $sessionID}
- if ($credential){
- try
- {
- Connect-VIServer -Server $VServer.Name -Credential $credential -ErrorAction Stop
- if ($? -eq $false) {throw $error[0].exception}
- }
- catch [Exception]
- {
- Write-Warning "$($VServer.Name) is unavailable. See error information for more details."
- Write-Host $_
- }
- }
- }
- $clusters = Get-Cluster | Select-Object Name, Uid
- $clusters | ForEach-Object {
- $_ | Add-Member -MemberType NoteProperty -Name VCName -Value $_.Uid.split('@')[1].split(':')[0]
- }
- $datastores = Get-Datastore | Select-Object -Property Name, DatastoreBrowserPath, FreeSpaceMB, CapacityMB
- $datastores | ForEach-Object {
- $_ |
- Add-Member -MemberType NoteProperty `
- -Name VCName -Value $_.DatastoreBrowserPath.split('\')[1].replace('@443','')
- $_.FreeSpaceMB = [int]$_.FreeSpaceMB
- $_.CapacityMB = [int]$_.CapacityMB
- }
- $portgroups = Get-VDPortgroup | Select-Object VlanConfiguration, Name, NumPorts, Uid | ? VlanConfiguration -notlike "*0-4094*"
- $portgroups | ForEach-Object {
- $_ | Add-Member -MemberType NoteProperty -Name VCName -Value $_.Uid.split('@')[1].split(':')[0]
- $_.NumPorts = [int]$_.NumPorts
- }
- $ErrorActionPreference = "SilentlyContinue"
- $vCenters = @()
- Foreach ($vCenter in $VDIEnvironments) {
- $vCenter |
- Add-Member -MemberType NoteProperty -Name TargetCluster -Value (
- $clusters | ? VCName -like $vCenter.Name
- )[0].Name -Force
- $vCenter |
- Add-Member -MemberType NoteProperty -Name Clusters -Value $(
- $clusters | ? VCName -like $vCenter.Name
- ).Name -Force
- $vCenter |
- Add-Member -MemberType NoteProperty -Name TargetDatastore -Value $(
- $datastores | ? VCName -like $vCenter.Name | Sort-Object -Descending FreeSpaceMB
- )[0].Name -Force
- $vCenter |
- Add-Member -MemberType NoteProperty -Name Datastores -Value $(
- $datastores | ? VCName -like $vCenter.Name
- ).Name -Force
- $vCenter |
- Add-Member -MemberType NoteProperty -Name TargetPortgroup -Value (
- $portgroups | ? VCName -like $vCenter.Name | Sort-Object -Descending NumPorts
- )[0].Name -Force
- $vCenter |
- Add-Member -MemberType NoteProperty -Name Portgroups -Value $(
- $portgroups | ? VCName -like $vCenter.Name
- ).Name -Force
- $vCenters += $vCenter
- }
- $ErrorActionPreference = "Continue"
- #fill VCenters inside form from selected Environment
- $SelectedVCenters = $VCenters |
- Where-Object Env -like $WPFboxEnvironment.Text |
- Where-Object Datastores -ne $Null
- $SelectedVCenters | ForEach-Object {
- $WPFboxSrcVCenter.Items.Add($_.Name)
- $WPFlistTgtVCenters.Items.Add($_)
- $WPFlistTgtVCenters.SelectedItems.Add($_)
- }
- })
- #event: when Source VCenter is selected
- $wpFBoxSrcVCenter.Add_DropDownClosed({
- #empty vCenters folder inside form
- $wpFBoxSrcVCenterFolder.Items.Clear()
- #empty Source VMs inside form
- $wpFListSrcVMs.Items.Clear()
- $srcVCFolders = Get-Folder -Server $wpFBoxSrcVCenter.Text -Type VM | Select-Object Name
- #fill vCenter folder inside form
- $srcVCFolders | ForEach-Object {
- $wpFBoxSrcVCenterFolder.Items.Add($_.Name)
- }
- })
- #event: when Source vCenter Folder is selected
- $wpFBoxSrcVCenterFolder.Add_DropDownClosed({
- #empty Source VMs inside form
- $wpFListSrcVMs.Items.Clear()
- #get VMs from selected VCenter
- if ($global:DefaultVIServers | Where-Object -FilterScript {$_.Name -like $wpFBoxSrcVCenter.Text}){
- $srcVMs = Get-VM -Server $wpFBoxSrcVCenter.Text -Location $wpFBoxSrcVCenterFolder.text | Select-Object Name
- #fill Source VMs inside form
- $srcVMs | ForEach-Object {
- $wpFListSrcVMs.Items.Add($_.Name)
- }
- }
- })
- #event: when Source VM is selected
- $wpFListSrcVMs.Add_SelectionChanged({
- $wpFTextSourceVM.Text = $wpFListSrcVMs.SelectedItem
- $wpFtextTargetVM.Text = $wpFListSrcVMs.SelectedItem + '-clone'
- })
- #event: add click handler
- $wpFButtonDeploy.add_Click{
- Clear-Host
- Write-Host "Source Environment:"$wpFBoxEnvironment.Text
- Write-Host "Source VCenter:"$wpFBoxSrcVCenter.Text
- Write-Host "Source VM:"$wpFListSrcVMs.SelectedItem
- Write-Host "`nCDC Target VM name:"$wpFTextSourceVM.Text"`
- `tSDC Target VM name:"$wpFTextTargetVM.Text
- Write-Host "Snapshot Description:"$wpFTextSnapshotDescription.Text
- Write-Host "`nTarget VCenters:"
- $TargetVCs = $wpFListTgtVCenters.SelectedItems | Sort-Object Name
- Write-Host -ForegroundColor Black -BackgroundColor White `
- "`tVC Name".PadRight(25) `
- "`tDatacenter".PadRight(10) `
- "`tTarget Cluster".PadRight(40) `
- "`tTarget Datastore".PadRight(40) `
- "`tTarget Portgroup".PadRight(40) `t
- Foreach ($TargetVC in $TargetVCs) {
- Write-Host -ForegroundColor White -BackgroundColor DarkGray `t `
- $TargetVC.Name.PadRight(25) `
- $TargetVC.Datacenter.PadRight(15) `
- $TargetVC.TargetCluster.PadRight(40) `
- $TargetVC.TargetDatastore.PadRight(40) `
- $TargetVC.TargetPortgroup.PadRight(40)
- }
- #clone vm to each vcenters and snapshot
- Foreach ($TargetVC in $TargetVCs) {
- if ($TargetVC -match $wpFBoxSrcVCenter.Text){
- continue;
- }
- #function to clone and move VM to all VI servers other than source
- #snapshot the clone
- $objVC = New-Object PSObject -Property @{
- SourceVCName = $wpFBoxSrcVCenter.Text
- SourceVMName = $wpFListSrcVMs.SelectedItem
- SourceClusterName = (Get-Cluster -VM $wpFListSrcVMs.SelectedItem -Server $wpFBoxSrcVCenter.Text).Name
- SourceFolderName = $wpFBoxSrcVCenterFolder.Text
- SourceDatastoreName = (Get-DataStore -VM $wpFListSrcVMs.SelectedItem -Server $wpFBoxSrcVCenter.Text).Name
- SourceParentSnapExist = (Get-Snapshot -vm $wpFListSrcVMs.SelectedItem -Server $wpFBoxSrcVCenter.Text).IsCurrent
- DestVCName = $TargetVC.Name
- DestVMName = $wpFtextTargetVM.Text
- DestClusterName = $TargetVC.TargetCluster
- DestFolderName = $wpFBoxSrcVCenterFolder.Text
- DestDatastoreName = $TargetVC.TargetDatastore
- DestPortgroupName = $TargetVC.TargetPortgroup
- SnapShotName = (Get-Date -Format s)
- SnapShotDescription = $wpFTextSnapshotDescription.Text
- }
- #clone and migrate the VM to target vCenter servers
- xNew-VM -sourceVCName $objVC.SourceVCName -sourceClusterName $objVC.SourceClusterName `
- -sourceFolderName $objVC.SourceFolderName -sourceDatastoreName $objVC.SourceDatastoreName `
- -sourceVMName $objVC.SourceVMName -destVCName $objVC.DestVCName -destClusterName $objVC.DestClusterName `
- -destFolderName $objVC.DestFolderName -destDatastoreName $objVC.DestDatastoreName `
- -destPortgroupName $objVC.DestPortgroupName -destVMName $objVC.DestVMName
- #snapshot the target clone after successful migration
- Write-host "Taking snashopt of the destination VM" -ForegroundColor DarkCyan
- try {
- New-Snapshot -Server $objVC.DestVCName -VM $objVC.DestVMName -Name $objVC.SnapShotName -Description $objVC.SnapShotDescription
- }
- catch
- {
- Write-Host "An error occured:"
- Write-Host $_
- }
- }
- #rename the source vm if the environment is production
- #snapshot source vm
- Write-host "`nTaking snashopt of the source VM" -ForegroundColor DarkCyan
- if (!($objVC.SourceParentSnapExist)){
- try {
- New-Snapshot -Server $objVC.SourceVCName -VM $objVC.SourceVMName -Name $objVC.SnapShotName -Description $objVC.SnapShotDescription
- }
- catch
- {
- Write-Host "An error occured:"
- Write-Host $_
- }
- }else {
- Write-host "`A snapshot already exist. Skipping this task." -ForegroundColor DarkCyan -BackgroundColor White
- }
- Write-host "`n"
- Write-host "Task complete. VM has been deployed to all available vCenter servers." -ForegroundColor DarkGray
- }
- #show window
- $form.ShowDialog() | Out-Null
- #on window close
- $wpFListTgtVCenters.SelectedItems |
- Sort-Object Name |
- Select-Object Env, Name, Datacenter, TargetCluster, TargetDatastore, TargetPortgroup |
- Format-Table -AutoSize
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement