Microsoft Windows Server 2016 Storage Ongoing Jobs Script Probe Action

Microsoft.Windows.Server.10.0.Storage.StorageSpacesDirect.OngoingJobs.GetPropertyBagProbeAction (ProbeActionModuleType)

Probe action type used to trigger storage ongoing jobs script.

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityInternal
RunAsMicrosoft.Storage.Library.MonitoringRunAsProfile
InputTypeSystem.BaseData
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
Script ProbeAction Microsoft.Windows.PowerShellPropertyBagProbe Default

Source Code:

<ProbeActionModuleType ID="Microsoft.Windows.Server.10.0.Storage.StorageSpacesDirect.OngoingJobs.GetPropertyBagProbeAction" RunAs="MicrosoftStorageLibrary!Microsoft.Storage.Library.MonitoringRunAsProfile" Accessibility="Internal">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="StorageClassName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="StorageObjectUniqueID" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ObjectId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TargetId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" maxOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="Script" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe">
<ScriptName>GetOngoingJobsScript.ps1</ScriptName>
<ScriptBody><Script>

param([string] $storageClassName, [string] $targetInstanceUniqueID, [string] $ObjectId, [string] $TargetId)



#general variables
#-----------------
$errorActionPreference = 'Stop'

$errEvent = [System.Diagnostics.EventLogEntryType]::Error;
$infoEvent = [System.Diagnostics.EventLogEntryType]::Information;
$GenericInformationEvent = 8671;
$GenericFailureEvent = 8672;
$DiscoveryGenericInformationEvent = 8861;
$DiscoveryGenericFailureEvent = 8862;
$script:traceMsg = "";
$ObjectNotFoundCode = -2146233087
$msft_namespace = "root\microsoft\windows\storage"
$cluster_namespace = "root\mscluster"

# Guid length for Virtual Disk
$guidLength = 38
$PadLength = 1 #Object Id ended with double quote


if ($false -eq [string]::IsNullOrEmpty($DebugMode))
{
$DebugMode = [string]::Equals("true",$DebugMode,[System.StringComparison]::OrdinalIgnoreCase)
}
else
{
$DebugMode = $false
}

$momAPI = New-Object -comObject 'MOM.ScriptAPI'

#Common Functions
Function Get-Guid([string]$sguid)
{
if ([string]::IsNullOrEmpty($sguid))
{
return $null
}

$guid = [guid]::NewGuid()
if ($false -eq [guid]::TryParse($sguid,[ref] $guid))
{
return $null;
}

return $guid
}

Function GetDiskIdFilterFromVdObjectId([string]$ObjectId)
{
if ($true -eq [string]::IsNullOrEmpty($ObjectId))
{
return $null
}

$Length = $ObjectId.Length
#check guid length - 38 in {guid} format
$rGuidLength = $guidLength + $PadLength
if ($Length -lt $rGuidLength) {return $null;}

$sguid = $ObjectId.Substring($Length - $rGuidLength,$guidLength)

$guid = Get-Guid -sguid $sguid

if ($null -eq $guid) {return $null;}

return "DiskId='\\\\?\\Disk{$guid}'"
}

Function GetVolumesFromVd($vd)
{
$errorActionPreference = "Stop"

$Filter = GetDiskIdFilterFromVdObjectId -ObjectId $vd.ObjectId
if ($null -eq $Filter ) {return $null;}

$partitions = Get-CimInstance -ClassName "MSFT_Partition" -Namespace $msft_namespace -Filter $Filter | Where-Object {$_.AccessPaths -ne $null}
$volumes = @{}

foreach ($partition in $partitions)
{
$Volume = Get-CimAssociatedInstance -Association "MSFT_PartitionToVolume" -InputObject $partition -Namespace $msft_namespace
if ($null -ne $Volume -and "CSVFS" -eq $Volume.FileSystem )
{
$volumes[$Volume] = 1;
}
}

return $volumes
}

Function AddTraceMessage
{
param($message)

$errorActionPreference = 'SilentlyContinue'
$timeStamp = (get-date -format "HH:mm:ss:fff");
$script:traceMsg = $script:traceMsg + "`n[" + $timeStamp + "] " + $message;
}

Function Log-FinalDebugData([int]$FailureEvent,[int]$InformationEvent,$exception,[string]$SCRIPT_NAME,[string]$traceMsg,[bool]$DebugMode = $false,$message = [string]::Empty)
{
$ErrorActionPreference = "SilentlyContinue"

if ($null -ne $exception)
{
if ([string]::Empty -eq $message) {$message = "Error occured during script execution:"}
$momAPI.LogScriptEvent($SCRIPT_NAME, $FailureEvent, 1, "$message $($exception.Message)");
}

if ($script:traceMsg -ne $null -and $true -eq $DebugMode)
{
$momAPI.LogScriptEvent($SCRIPT_NAME,$InformationEvent, 0, $traceMsg);
}
}

Function Check-CimError($Exception)
{
$errorActionPreference = 'SilentlyContinue'
$result = $false

if ($null -eq $Exception)
{
return $result
}

$error.Clear()
$type = $Exception.GetType()

if (0 -ne $error.Count)
{
$error.Clear()
return $result
}

if ("Microsoft.PowerShell.Cmdletization.Cim.CimJobException" -eq $type.FullName -and $ObjectNotFoundCode -eq $Exception.HResult)
{
$result = $true
}

return $result
}

Function Get-FaultException($exception,$IsObjectExist)
{
if ($false -eq $IsObjectExist)
{
$IsError = Check-CimError -Exception $exception
if ($true -eq $IsError)
{
$exception = $null
}

}

return $exception

}

Function Load-Module ([string] $ModuleName)
{
if ([string]::IsNullOrEmpty($ModuleName) )
{
return $false
}

$ErrorActionPreference="SilentlyContinue"
$error.Clear()

$retval = $false
$Tmodule = Get-Module -Name $ModuleName

########Check for powershell 1.0
if ($error.Count -ne 0)
{
$Exception = $error[0].Exception

if ($null -ne $Exception)
{
$type = $Exception.GetType()
if ([System.Management.Automation.CommandNotFoundException] -eq $type)
{
$error.Clear()
return $retval
}
}

$error.Clear()
}

if ($null -eq $Tmodule)
{
$Tmodule = Get-Module -Name $ModuleName -ListAvailable
if ($error.Count -ne 0){ return $retval;}
if ($null -eq $Tmodule) {return $retval;}

Import-Module $ModuleName
if ($error.Count -eq 0){ $retval = $true;}

}
else
{
$retval = $true
}

return $retval

}

AddTraceMessage -message "Start on Node: $env:COMPUTERNAME"
$IsStorageModuleReady = Load-Module -ModuleName "Storage"

if ($false -eq $IsStorageModuleReady)
{
if ($error.Count -ne 0)
{
$message = "Cannot initializa Storage module. Error: " + $error[0].ToString()

}
else
{
$message = "Cannot load Storage module."
}

AddTraceMessage -message $message

}

#Load cimcmdlets
$IsCimReady = Load-Module -ModuleName "CimCmdLets"





#param([string] $storageClassName, [string] $targetInstanceUniqueID, [string] $ObjectId, [string] $TargetId)

$scriptName = "GetOngoingJobsScript.ps1"
#-----------------


#Create Storage Metrics property bag data
$storageObject = $null
$IsStorageObjectExist = $false

if ($false -eq $IsStorageModuleReady)
{
$momAPI.LogScriptEvent($scriptName, $GenericFailureEvent, 1, $script:traceMsg);
exit
}

Try
{
$error.Clear();

if ($storageClassName -eq "MSFT_StorageSubSystem")
{
$storageObject = Get-StorageSubsystem -UniqueID $targetInstanceUniqueID
$IsStorageObjectExist = $true
}
elseif($storageClassName -eq "MSFT_Volume")
{
$storageObject = Get-Volume -UniqueID $targetInstanceUniqueID
$IsStorageObjectExist = $true
}
elseif ($storageClassName -eq "MSFT_FileShare")
{
$storageObject = Get-FileShare -UniqueID $targetInstanceUniqueID
$IsStorageObjectExist = $true
}

if ($storageObject -eq $null)
{
#Throw [System.NullReferenceException] -join("Can't find Storage Object with UniqueID = ", $targetInstanceUniqueID);
return
}

#------------
$ongoingJobs = @()
$ongoingJobsList = @()
$ongoingJobs += $storageObject | Get-StorageHealthAction
if ($ongoingJobs -ne $null -and $ongoingJobs.Count -gt 0)
{
foreach ($ongoingJob in $ongoingJobs)
{
$propertyBag = $momAPI.CreatePropertyBag()
$propertyBag.AddValue("StorageObjectUniqueID", $targetInstanceUniqueID);
$propertyBag.AddValue("ObjectId", $ObjectId);
$propertyBag.AddValue("TargetId", $TargetId);
$ongoingJobsList += $ongoingJob.UniqueID;
$propertyBag.AddValue("UniqueID", $ongoingJob.UniqueID);
$propertyBag.AddValue("Reason", $ongoingJob.Reason);
$propertyBag.AddValue("Description", $ongoingJob.Description);
$propertyBag.AddValue("Status", $ongoingJob.Status);
$propertyBag.AddValue("PercentComplete", $ongoingJob.PercentComplete);
$propertyBag.AddValue("PropertyBagType", "Item");
$propertyBag
}
}
$propertyBag = $momAPI.CreatePropertyBag()
$propertyBag.AddValue("Reason", $ongoingJob.Reason);
$propertyBag.AddValue("Description", "");
$propertyBag.AddValue("Status", "");
$propertyBag.AddValue("PercentComplete", "");
$propertyBag.AddValue("StorageObjectUniqueID", $targetInstanceUniqueID);
$propertyBag.AddValue("TargetId", $TargetId);
$propertyBag.AddValue("ObjectId", $ObjectId);
$propertyBag.AddValue("PropertyBagType", "List");
$listOfObjects = $ongoingJobsList -join ','
$propertyBag.AddValue("UniqueID", $listOfObjects);
$propertyBag
#------------
}
Catch
{
#If storage object doesn't exist SCOM may have lag in discovery so ignore this case
$exception = Get-FaultException -exception $_.Exception -IsObjectExist $IsStorageObjectExist
AddTraceMessage -message $exception.Message
}
Finally
{
if ($null -ne $exception)
{
$momAPI.LogScriptEvent($scriptName, $GenericFailureEvent, 1, ("TargetId: " + $targetInstanceUniqueID + "`n" + $exception.Message));
}

if ($script:traceMsg -ne $null)
{
#Debug
#$momAPI.LogScriptEvent($scriptName, $GenericInformationEvent, 0, $script:traceMsg);
}
}</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>storageClassName</Name>
<Value>$Config/StorageClassName$</Value>
</Parameter>
<Parameter>
<Name>targetInstanceUniqueID</Name>
<Value>$Config/StorageObjectUniqueID$</Value>
</Parameter>
<Parameter>
<Name>ObjectId</Name>
<Value>$Config/ObjectId$</Value>
</Parameter>
<Parameter>
<Name>TargetId</Name>
<Value>$Config/TargetId$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<StrictErrorHandling>true</StrictErrorHandling>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="Script"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<InputType>System!System.BaseData</InputType>
</ProbeActionModuleType>