次の方法で共有


SMA Run book for enabling hyper-v replica after deployment in WAP

 

In Windows Azure Pack you have the ability to link your Automation runbooks to an "Event"

One of the common things that I see is about providing a disaster recovery solution to a customer especially in a multi tenant environment..

During VM creation we link our runbook to the Event as shown

Now everytime a virtual machine is created it will run my EnableHyperVReplicaVM runbook

 

In the runbook code we access a hidden variable called $PSPrivateMetaData

The important thing first is to get the VMM Job ID, as this allows us to monitor completion of the VMM Job before trying to enable hyper-v replica as otherwise it will fail!

Another part in this runbook is we use the Get-AutomationConnection to allow us to execute against the VMM server and use the Service management automation Assets to the fullest!

 

Anyway here is the code! And remember there are hundreds of other ways of doing this! J

 

workflow
EnableHyperVReplicaforVM

{

 

$params
=
$PSPrivateMetaData.params

$VMMJobID
=
$PSPrivateMetaData.VMMJobId

 

$vmmserver
=
Get-AutomationConnection
-Name
'VMMConnection'

$SecurePassword
=
ConvertTo-SecureString
-AsPlainText
-String
$vmmserver.Password -Force

$Credential
=
New-Object
-TypeName
System.Management.Automation.PSCredential
-ArgumentList
$vmmserver.username, $SecurePassword

$replicaserver
=
"svvmasp15.corp.contoso.com"

$masterserver
=
"svvmasp16.corp.contoso.com"

 

$jobFinalState
=
InlineScript {

Import-Module
VirtualMachineManager

 

 

While ((Get-SCJob
-ID
$USING:VMMJobId).Status -eq
'Running')

{

$jobProgress
= (Get-SCJob
-ID
$USING:VMMJobId).ProgressValue

Start-Sleep
-Seconds
3

}

 

$JobState
= (Get-SCJob
-ID
$USING:VMMJobId).Status

Return
$JobState

} -PSComputerName
$VmmServer.ComputerName -PSCredential $Credential

 

$params
=
$params
-split (',') -replace ('"','') -replace ('{','') -replace ('}','')

$vmname
=
InlineScript

{

 

Foreach ($p
in
$USING:params)

{

$key
= ($p
-split (':'))[0]

$value
= ($p
-split (':'))[1]

if(($key
-eq
"Name") -and ($value
-ne
"null"))

{

return
$value

}

 

}

 

 

}

 

 

write-output
"JobSate Result form VMM: $jobFinalstate"

write-output
"VMName: $vmname"

 

$represult
=
inlinescript {

Import-Module
Hyper-V

Enable-VMReplication
–VMName
$using:vmname
–ReplicaServerName
$using:replicaserver
–ReplicaServerPort
80
-authenticationtype
kerberos

start-vminitialreplication
-vmname
$using:vmname

$result
= (get-vm
-name
$using:vmname).ReplicationState

return
$result

} -PSComputerName
$masterServer
-PSCredential
$Credential

if ($represult
-eq
"Enabled")

{

$replicaenabled
=
$true

}

else

{

$replicaenabled
=$false

}

 

 

 

 

write-output
"ReplicaEnabledResult: $replicaenabled"

 

}