Batch Enable-VMReplication in PowerShell 3.0

hyper-v_replicaToday was the last day I user wizard Hyper-V to activate the VM replication .. Why? Since today i did something i should have done a long time ago.. I made a script for enable VM replication for all machines on one server. The script going to selecting all ”running” machines, that not have replication enabled at the moment. Happy POSH:ing!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
$ReplicaServer = "LAB-HYPERV02"
$StartTime = "20:00"
$ReplicaServerPort  = 80
$ReplicaServerAuthType = "Kerberos"
$ReplicationFrequencySec = 15*60 # 15x60 equals 900 secunds = 15 minutes.
 
$InitialReplicationDelayMinutes = 30
$InitialReplicationStartTime = Get-Date $StartTime
 
$AutoResynchronizeIntervalStart = "18:30:00"
$AutoResynchronizeIntervalEnd = "06:00:00"
 
#$VMHostInfo = Get-VMHost -ComputerName $ReplicaServer
$ReplicaServerInfo = Get-VMReplicationServer -ComputerName $ReplicaServer
$ReplicaDestPath = $ReplicaServerInfo.DefaultStorageLocation
 
 
$VMs = Get-VM | ? { ($PSItem.ReplicationState -eq "Disabled") -AND ($PSItem.State -eq "Running")}
 
$VMsDiskSizeTotal = (($VMs | % { Get-VMHardDiskDrive -VMName $PSItem.Name | Get-VHD }) | measure -Property FileSize -Sum).Sum
$ReplicaServerDiskSizeFree = (Invoke-Command -ArgumentList $ReplicaDestPath -ScriptBlock {param($ReplicaDestPath) Get-Volume -DriveLetter $($ReplicaDestPath.Substring(0,1))} -ComputerName $ReplicaServer).SizeRemaining
 
 
 
#Check Diskspace before replication.
Write "Checking disk space on $ReplicaServer : $($VMsDiskSizeTotal/1TB) TB Selected for replication. $($ReplicaServerDiskSizeFree/1TB) TB Avalible on destination server"
if ( ($ReplicaServerDiskSizeFree) -AND ($VMsDiskSizeTotal -le $ReplicaServerDiskSizeFree) )
    {
 
    Write "Checking disk space avalible on $ReplicaServer"    
 
    foreach ($VM in $VMs)
        {
 
        Write "Enabeling VM Replication on VM $($VM.Vmname)"
 
        Enable-VMReplication `
            -VMName $VM.VMName `
            -ReplicaServerName $ReplicaServer `
            -ReplicaServerPort $ReplicaServerPort `
            -AuthenticationType $ReplicaServerAuthType `
            -ReplicationFrequencySec $ReplicationFrequencySec `
            -AutoResynchronizeEnabled $true `
            -AutoResynchronizeIntervalStart $AutoResynchronizeIntervalStart `
            -AutoResynchronizeIntervalEnd $AutoResynchronizeIntervalEnd `
            -CompressionEnabled $true `
            -RecoveryHistory 0 `
            -Confirm:$false
 
        Write "Enabeling Intitial Replication on  $($VM.VMname) at $InitialReplicationStartTime"
        Start-VMInitialReplication -VMName $VM.VMName -InitialReplicationStartTime $InitialReplicationStartTime
        $InitialReplicationStartTime = $InitialReplicationStartTime.AddMinutes($InitialReplicationDelayMinutes)
 
        # Debug
        #break
 
        }
    }
    else
        {
        Write "Checking disk space not avalible on $ReplicaServer . script failed."
        }
This entry was posted in Hyper-V, Powershell. Bookmark the permalink.

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *

*


5 × fem =

23 942 Spam Comments Blocked so far by Spam Free Wordpress

Följande HTML-taggar och attribut är tillåtna: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>