$EVENT_ID_SUCCESS = 1000 #Use IDs in the range 1 - 1000
$EVENT_ID_SCRIPTERROR = 999 #Then you can use eventcreate.exe to test the MP
#Scripting.FileSystemObject constants
$FOR_READING = 1 #Open a file for reading only. You can#t write to this file.
$FOR_WRITING = 2 #Open a file for writing.
$FOR_APPENDING = 8 #Open a file and write to the end of the file.
$WIN_SRV_2012_OSVer = "6.2"
#******************************************************************************
# FUNCTION: CheckByOSCurrentVersion
# DESCRIPTION: Returns True if the Registry Key for CurrentVersion
# is equal the target OS Versions Number.
# RETURNS: Boolean: True, if build is greater or equal than the given number
#******************************************************************************
function CheckByOSCurrentVersion()
{
$strCurrentOSVer = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
$strCurrentOSVer = $strCurrentOSVer.CurrentVersion
$CheckByOSCurrentVersion = $false
if($strCurrentOSVer -ge $WIN_SRV_2012_OSVer){
$CheckByOSCurrentVersion = $true
}else{
$CheckByOSCurrentVersion = $false
}
return $CheckByOSCurrentVersion;
}
$useCIM = CheckByOSCurrentVersion
#Global vars
#$mobjDebugLog #As TextStream
#$mblnLogSuccessEvent #As String(ToBoolean) (passed in as parameter of script)
#$mstrIdentifier #As String (passed in as parameter of script)
#*********************************************************************************************
# PROCEDURE: Main
# DESCRIPTION: Reads the script parameters, gets the cluster name and
# discovers the containment relationship between a cluster and
# the targeted cluster virtual server.
# PARAMETERS: void
#*********************************************************************************************
Function Main()
{
$objMomScriptAPI = New-Object -comObject MOM.ScriptAPI
#Create new discovery data object
$objDiscoveryData = $objMomScriptAPI.CreateDiscoveryData(0, $strSourceID, $strManagedEntityId)
#Return discovery data
if ($blnSuccess)
{
$objDiscoveryData
}
}
#****************************************************************************************************************
# FUNCTION: DiscoverClusterName
# DESCRIPTION: Discover instances of the relationship class
# #Microsoft.Windows.Server.ClusterSharedVolumeMonitoring.Microsoft.Windows.Cluster.Contains.Microsoft.Windows.Cluster.VirtualServer#.
# PARAMETERS: IN String strTargetComputer: principal name of the targeted #Microsoft.Windows.Cluster.VirtualServer# instance.
# OUT Object objDiscoveryData: initialised DiscoveryData instance
# RETURNS: Boolean: True if successful
#****************************************************************************************************************
Function DiscoverClusterName([string]$strTargetComputer, $objDiscoveryData) #As Boolean
{
#Get the hostname of the target computer from its principal name
if ($strTargetComputer.IndexOf(".") -gt 0)
{
$strTargetComputerShortName = $strTargetComputer.Substring(0, $strTargetComputer.IndexOf("."))
}
else
{
$strTargetComputerShortName = $strTargetComputer
}
#Retrieve the cluster name from WMI and
#discover the relationship
$strClusterName = GetClusterName
if ($strClusterName.Length -gt 0)
{
#Create the cluster name instance
$objClusterNameInstance = $objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.Server.ClusterSharedVolumeMonitoring.Cluster']$")
#Add key property (is hosted on Microsoft.Windows.Cluster.VirtualServer
$objClusterNameInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $strTargetComputer)
#Property ClusterName
$objClusterNameInstance.AddProperty("$MPElement[Name='Microsoft.Windows.Server.ClusterSharedVolumeMonitoring.Cluster']/ClusterName$", $strClusterName)
#add the instance to the discovery data
$objDiscoveryData.AddInstance($objClusterNameInstance)
return $true
}
else
{
return $false
}
}
#****************************************************************************************************************
# FUNCTION: GetClusterName
# DESCRIPTION: Returns the cluster name of the given cluster virtual name
# RETURNS: String strClusterName: string to return the cluster name
#****************************************************************************************************************
Function GetClusterName() #As Boolean
{
$strClusterName = ""
#******************************************************************************
# FUNCTION: CheckOSBuildNumber
# DESCRIPTION: Returns True if the property BuildNumber from the Win32_OperatingSystem
# instance is greater or equal the given build number using the CIMv2 WMI namespace.
# PARAMETERS: IN Long lngBuildNumber: build number to check
# RETURNS: Boolean: True, if build is greater or equal than the given number
#******************************************************************************
Function CheckOSBuildNumber($lngBuildNumber) #As Boolean
{
#Get the computersystem instance
if($useCIM){
$objSWbemObjectSet = Get-Ciminstance -Query "select * from Win32_OperatingSystem" -ErrorAction SilentlyContinue
}else{
$objSWbemObjectSet = Get-WMIObject -Query "select * from Win32_OperatingSystem" -ErrorAction SilentlyContinue
}
foreach ($objSWbemObject in $objSWbemObjectSet)
{
if (($objSWbemObject.BuildNumber -as [int]) -ge $lngBuildNumber)
{
return $true
}
}