#*************************************************************************
# Script Name - Discover-PrimaryReplicaVM.ps1
# Author - - Progel spa
# Version - 1.0 24.09.2007
# Purpose -
#
# Assumptions -
#
#
# Parameters - TraceLevel
# - ComputerName
# - SourceId
# - ManagedEntityId
# Command Line - .\test.ps1 4 "serverName" '{1860E0EB-8C21-41DA-9F35-2FE9343CCF36}' '{1860E0EB-8C21-41DA-9F35-2FE9343CCF36}'
# If discovery must be added the followinf parameters
# SourceId ($ MPElement $ )
# ManagedEntityId ($ Target/Id $)
#
# Output properties
#
# Status
#
# Version History
# 1.0 21.02.2015 DG First Release
#
#
# (c) Copyright 2015, Progel spa, All Rights Reserved
# Proprietary and confidential to Progel srl
#
#*************************************************************************
# Get the named parameters
param([int]$traceLevel=$(throw 'must have a value'),
[string]$computerName=$(throw 'must have a value'),
[string]$sourceID=$(throw 'must have a value'),
[string]$ManagedEntityId=$(throw 'must have a value'))
Function Throw-EmptyDiscovery
{
param($SourceId, $ManagedEntityId)
$oDiscoveryData = $g_API.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
Log-Event $FAILURE_EVENT_ID $EVENT_TYPE_WARNING "Exiting with empty discovery data" $TRACE_INFO
$oDiscoveryData
If($traceLevel -eq $TRACE_DEBUG)
{
#just for debug proposes when launched from command line does nothing when run inside OpsMgr Agent
$g_API.Return($oDiscoveryData)
}
}
Function Throw-KeepDiscoveryInfo
{
param($SourceId, $ManagedEntityId)
$oDiscoveryData = $g_API.CreateDiscoveryData(0,$SourceId,$ManagedEntityId)
#Instead of Snapshot discovery, submit Incremental discovery data
$oDiscoveryData.IsSnapshot = $false
Log-Event $FAILURE_EVENT_ID $EVENT_TYPE_WARNING "Exiting with null non snapshot discovery data" $TRACE_INFO
$oDiscoveryData
If($traceLevel -eq $TRACE_DEBUG)
{
#just for debug proposes when launched from command line does nothing when run inside OpsMgr Agent
$g_API.Return($oDiscoveryData)
}
}
#endregion
#Start by setting up API object.
$P_TraceLevel = $TRACE_VERBOSE
$g_Api = New-Object -comObject 'MOM.ScriptAPI'
#$g_RegistryStatePath = "HKLM\" + $g_API.GetScriptStateKeyPath($SCRIPT_NAME)
$vms = Get-VM | where {$_.ReplicationMode.Value__ -ne 0} #use the enum codes instead of labels 0 = 'None'
$discoveryData = $g_api.CreateDiscoveryData(0, $sourceId, $managedEntityId)
foreach($vm in $vms) {
$replica = Get-VMReplication -VM $vm
#I use labels and codes for discovery so they're UI friendly, if in the future I need to group objects I will add a code property. Labels are locale dependent
$oInstance = $discoveryData.CreateClassInstance("$MPElement[Name='QND.Hyperv.2012R2.ReplicaVM']$")
$oInstance.AddProperty("$MPElement[Name='QND.Hyperv.2012R2.VM']/VirtualMachineId$", $vm.VMId.ToString())
$oInstance.AddProperty("$MPElement[Name='QND.Hyperv.2012R2.ReplicaVM']/PrimaryServerName$", $replica.PrimaryServerName)
$oInstance.AddProperty("$MPElement[Name='QND.Hyperv.2012R2.ReplicaVM']/ReplicaServerName$", $replica.ReplicaServerName)
$oInstance.AddProperty("$MPElement[Name='QND.Hyperv.2012R2.ReplicaVM']/ReplicationMode$", $vm.ReplicationMode.ToString())
$oInstance.AddProperty("$MPElement[Name='QND.Hyperv.2012R2.ReplicaVM']/RelationshipType$", $replica.RelationshipType.ToString())
$oInstance.AddProperty("$MPElement[Name='QND.Hyperv.2012R2.ReplicaVM']/FrequencySec$", $replica.FrequencySec)
$discoveryData.AddInstance($oInstance)
}
$discoveryData