$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 shared volume discovery data.
# PARAMETERS: void
#*********************************************************************************************
Function Main()
{
$objMomScriptAPI = New-Object -comObject MOM.ScriptAPI
#Create new discovery data object
$objDiscoveryData = $objMomScriptAPI.CreateDiscoveryData(0, $strSourceID, $strManagedEntityId)
#****************************************************************************************************************
# FUNCTION: DiscoverClusterSharedVolumes
# DESCRIPTION: Discover instances of the class #Microsoft.Windows.Server.ClusterSharedVolumeMonitoring.ClusterSharedVolume# 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 DiscoverClusterSharedVolumes([string]$strTargetComputer, [string]$strClusterName, $objDiscoveryData) #As Integer
{
#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_SHARED_VOLUME -ErrorAction SilentlyContinue | select -First 1
}
if ($error.Count -eq 0)
{
#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
}
$blnCreateDiscoveryData = ($strTargetComputerShortName -ieq $strClusterName)
#****************************************************************************************************************
# FUNCTION: CreateDiscoveryData
# DESCRIPTION: Create discovery data for
# <ClassType ID="Microsoft.Windows.Server.ClusterSharedVolumeMonitoring.ClusterSharedVolume" Accessibility="Internal" Abstract="false" Base="Windows!Microsoft.Windows.ApplicationComponent" Hosted="true" Singleton="false">
# <Property ID="ClusterSharedVolumeName" Type="string" Key="true" CaseSensitive="false" Length="256" MinLength="0" />
# <Property ID="FriendlyVolumeName" 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 objClusterSharedVolume: the cluster shared volume as SWbemObject of type MSCluster_ClusterSharedVolume.
# 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($objClusterSharedVolume, $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.ClusterSharedVolumeMonitoring.ClusterSharedVolume']$")