$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.
$SCRIPT_SUCCESS = 0
$SCRIPT_WITH_ERROR = 1
$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
#$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 and creates the cluster disks discovery data.
# PARAMETERS: void
#*********************************************************************************************
Function Main()
{
# Targeted at Microsoft.Windows.Cluster.VirtualServer (based on Microsoft.Windows.Server.Computer)
$objMomScriptAPI = New-Object -comObject MOM.ScriptAPI
#Create new discovery data object
if ([System.Convert]::ToBoolean($mblnLogSuccessEvent) -eq $true)
{
$objMomScriptAPI.LogScriptEvent((Split-Path $MyInvocation.ScriptName -Leaf), $EVENT_ID_SUCCESS, $EVENT_TYPE_INFORMATION,"before create discovery data")
}
if ($intSuccess -eq $SCRIPT_SUCCESS)
{
if ([System.Convert]::ToBoolean($mblnLogSuccessEvent) -eq $true)
{
$objMomScriptAPI.LogScriptEvent((Split-Path $MyInvocation.ScriptName -Leaf), $EVENT_ID_SUCCESS, $EVENT_TYPE_INFORMATION, "-- Cluster Disks Discovery Script -- Script executed successfully.")
}
#Return discovery data
$objDiscoveryData
}
else
{
$objMomScriptAPI.LogScriptEvent((Split-Path $MyInvocation.ScriptName -Leaf), $EVENT_ID_SCRIPTERROR, $EVENT_TYPE_WARNING, "-- Cluster Disks Discovery Script -- An error occurred while running the script.")
}
}
#****************************************************************************************************************
# FUNCTION: DiscoverClusterDisks
# DESCRIPTION: Discover instances of the class #Microsoft.Windows.Server.ClusterDisksMonitoring.ClusterDisk# using WMI.
# PARAMETERS: IN String strTargetComputer: principal name of the targeted #Microsoft.Windows.Cluster.VirtualServer# instance.
# IN String strClusterName: the cluster containing the cluster shared volume
# OUT Object objDiscoveryData: initialised DiscoveryData instance
# RETURNS: Integer: 0 if successful and 1 if fails
#****************************************************************************************************************
Function DiscoverClusterDisks($strTargetComputer, $strClusterName, $objDiscoveryData) #As Integer
{
$blnUseResource = $false
#Connect to WMI NS \\.\root\MSCluster
$error.Clear()
# !!! Refactoring comment:
# Original VBScript only tries to connect to the namespace. Piping to get only the first one saves time.
if($useCIM){
$oWMI = Get-CimClass -Namespace $WMI_MSCLUSTER_NAMESPACE -ErrorAction SilentlyContinue | select -First 1
}else{
$oWMI = Get-WMIObject -Namespace $WMI_MSCLUSTER_NAMESPACE -Class $WMI_MSCLUSTER_CLUSTER_RESOURCE -ErrorAction SilentlyContinue | select -First 1
}
if ($error.Count -eq 0)
{
if($useCIM){
$objClusterResNetworks = Get-CimInstance -Namespace $WMI_MSCLUSTER_NAMESPACE -Query ("select * from " + $WMI_MSCLUSTER_CLUSTER_RESOURCE + " where Type = 'Network Name'") -ErrorAction SilentlyContinue
}else{
$objClusterResNetworks = Get-WMIObject -Namespace $WMI_MSCLUSTER_NAMESPACE -Query ("select * from " + $WMI_MSCLUSTER_CLUSTER_RESOURCE + " where Type = 'Network Name'") -ErrorAction SilentlyContinue
}
if ($error.Count -gt 0)
{
return $SCRIPT_WITH_ERROR
}
foreach ($objClusterResNet in $objClusterResNetworks)
{
$strDnsName = $objClusterResNet.PrivateProperties.DnsName
#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
}
$blnUseResource = ($strTargetComputerShortName -ieq $strDnsName)
if ($blnUseResource)
{
foreach ($objResGroupAsc in (Get-CimAssociatedInstance -InputObject $objClusterResNet -Association $WMI_MSCLUSTER_RESOURCEGROUP_TO_RESOURCE))
{
$strGroupName = $objResGroupAsc.Name
}
}
}
#****************************************************************************************************************
# FUNCTION: CreateDiscoveryData
# DESCRIPTION: Create discovery data for
# <ClassType ID="Microsoft.Windows.Server.ClusterDisksMonitoring.ClusterDisk" Accessibility="Internal" Abstract="false" Base="Windows!Microsoft.Windows.ApplicationComponent" Hosted="true" Singleton="false">
# <Property ID="ClusterResourceName" Type="string" Key="true" CaseSensitive="false" Length="256" MinLength="0" />
# <Property ID="ClusterDiskName" Type="string" Key="false" CaseSensitive="false" Length="256" MinLength="0" />
# <Property ID="PartitionName" Type="string" Key="false" CaseSensitive="false" Length="256" MinLength="0" />
# <Property ID="PartitionFileSystem" Type="string" Key="false" CaseSensitive="false" Length="256" MinLength="0" />
# <Property ID="PartitionSize" Type="int" Key="false" />
# <Property ID="ClusterName" Type="string" Key="false" CaseSensitive="false" Length="256" MinLength="0" />
# <Property ID="VolumeLabel" Type="string" Key="false" CaseSensitive="false" Length="256" MinLength="0" />
# </ClassType>
#
# PARAMETERS: IN Object objClusterResource: the cluster disks as SWbemObject of type MSCluster_Disk.
# IN Object objClusterDiskPartition: the associated disk partition as SWbemObject of type MSCluster_DiskPartition.
# IN Object objClusterResource: the associated cluster resource as SWbemObject of type MSCluster_Resource.
# IN String strTargetComputer: the principal name of the targeted cluster virtual server hosting the cluster shared volume,
# i.e. the cluster name and not the current owner!
# IN String strClusterName: the cluster containing the cluster shared volume
# OUT Object objDiscoveryData: initialised DiscoveryData instance
# RETURNS: Integer: 0 if successful or 1 if Error
#****************************************************************************************************************
Function CreateDiscoveryData($objClusterDisk, $objClusterDiskPartition, $objClusterResource,
$strTargetComputer, $strClusterName, $objDiscoveryData) #As Integer
{
$error.Clear()
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
#Create the cluster shared volume instance hosted on the targeted cluster virtual server
$objCSVInstance = $objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.Server.ClusterDisksMonitoring.ClusterDisk']$")