VHD Oversubscription Probe

Microsoft.SystemCenter.OperationsManager.Storage.VHD.Oversubscription.Probe (ProbeActionModuleType)

Probe provider for VHD oversubscription

Knowledge Base article:

Summary

This probe is used for getting VHD subscription status.

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
PS ProbeAction Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
TimeoutSecondsint$Config/TimeoutSeconds$Timeout (sec)Expecting time (in seconds) that the module wait to finish the execution.

Source Code:

<ProbeActionModuleType ID="Microsoft.SystemCenter.OperationsManager.Storage.VHD.Oversubscription.Probe" Accessibility="Internal">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" maxOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" maxOccurs="1" name="VMMServer" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<ProbeAction ID="PS" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe">
<ScriptName>GetFileShareSubscription.ps1</ScriptName>
<ScriptBody><Script># Creates sets of property bag data with File share to it's VHD subscription percentage
# Script parameters
# - $vmmServer - server computer name
# - $domain,$username,$pass - VMM Server Connection Account

param( [String] $vmmServer, $domain, $username, $pass)

$GenericInformationEvent = 8507;
$GenericFailureEvent = 8508;
$CredentialNullCode = 8502;

$vhdsByFileShare = @{}
$fileShareCapacityByFileShareID = @{}

Function AddTraceMessage
{
param($message)
$timeStamp = (get-date -format "HH:mm:ss:fff");
$script:traceMsg = $script:traceMsg + "`n[" + $timeStamp + "] " + $message;
}

try
{
$errorActionPreference = 'stop' | Out-Null

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

AddTraceMessage -message "Storage monitoring script GetFileShareSubscription.ps1 started."

$cred = $null;

if($domain -AND $username -AND $pass)
{
#Append domain to get domain\username
$domainUser = $domain + "\" + $username;

#Create Cred object
$securePass = ConvertTo-SecureString -AsPlainText $pass -force
$cred = New-Object System.Management.Automation.PSCredential $domainUser, $securePass;
}

if ($cred -eq $null)
{
$momAPI.LogScriptEvent(("Credentials are null for user: " + $domainUser), $CredentialNullCode, 1, $error);
return;
}

$error.Clear();

$session = New-PSSession -ComputerName $vmmServer -Authentication Default -Credential $cred;
if ( ($error -ne $null) -and ($error.Count -ne 0) )
{
if ($session -ne $null)
{
Remove-PSSession $session;
$session = $null;
}
return;
}

AddTraceMessage -message ("Creating new PS session with VMM server: " + $vmmServer)

$scVMMServer = Invoke-Command -Session $session -ArgumentList $vmmServer -ScriptBlock {$module = get-module -name "virtualmachinemanager"; if ($module -eq $null) {Import-Module "virtualmachinemanager"; } $vmm = Get-SCVMMServer -ComputerName $args[0]; $vmm};
if ($scVMMServer -eq $null)
{
AddTraceMessage -message ("VMM Server was null" + $error)
Remove-PSSession $session;
$session = $null
return;
}

AddTraceMessage -message ("Created new PS session with VMM server: " + $vmmServer)

# storage block that is invoked on remote PS session to VMM server
$sb = {
Function AddTraceMessage
{
param($message)
$timeStamp = (get-date -format "HH:mm:ss:fff");
$global:blockTraceMsg = $global:blockTraceMsg + "`n[RemoteSession]: " + "[" + $timeStamp + "]" + $message;
}

Try
{
$evt = New-Object System.Diagnostics.Eventlog("Application");
$evt.Source = "System Center Operations Manager Storage Monitoring";
$errorevent = [System.Diagnostics.EventLogEntryType]::Error;
$infoevent = [System.Diagnostics.EventLogEntryType]::Information;

$error.Clear()

$global:blockTraceMsg = "";

$result = @{}
$vhdsByFileShare = @{}
$fileShareCapacityByFileShareID = @{}

AddTraceMessage -message "getting VHDs to compute subscription percentage."
#Get vhds
$vhds = Get-SCVirtualHardDisk -All | Sort-Object -Property Name -Unique | Where-Object {($_.HostType -ne 'LibraryServer') -and ($_.FileShare -ne $null)}
if($vhds -ne $null)
{
AddTraceMessage -message ("found total " + $vhds.Count.ToString() + " VHDs.")
}

foreach ($vhd in $vhds)
{
$fileshareID = $vhd.FileShare.ID.ToString()

if ($vhdsByFileShare.ContainsKey($fileshareID) -eq $false)
{
$fileShareCapacityByFileShareID.Add($fileshareID, $vhd.FileShare.CapacityMB)
}

if ($vhdsByFileShare.ContainsKey($fileshareID))
{
$vhdsByFileShare[$fileshareID] += $vhd
}
else
{
$vhdsByFileShare.Add($fileshareID, @($vhd));
}
}

AddTraceMessage -message "fetched share to VHDs mapping."

# generate script block output
$result.Add("vhdsByFileShare",$vhdsByFileShare)
$result.Add("fileShareCapacityByFileShareID", $fileShareCapacityByFileShareID)
$result
}
Finally #disconnect VMM in remote session before exiting
{
if ($vmm -ne $null)
{
$vmm.Disconnect();
$vmm = $null;
}

if ($error -ne $null -and $error.Count -gt 0)
{
if ($error[0].Exception -ne $null)
{
$evt.WriteEntry($error[0].Exception.ToString(), $errorevent, 8508);
}
}
if ($global:blockTraceMsg -ne $null -and $global:blockTraceMsg.Length -lt 31839)
{
$evt.WriteEntry($global:blockTraceMsg, $infoevent, 8508);
}
}};

AddTraceMessage -message ("Invoking command to get file share subscription from vmm server: " + $vmmServer)

$fileShareSubscriptionDetails = Invoke-Command -Session $session -ScriptBlock $sb

if ($fileShareSubscriptionDetails -eq $null)
{
AddTraceMessage -message ("Error found in script block to get physical disk details")
if ($session -ne $null)
{
Remove-PSSession $session;
$session = $null;
}
return;
}
else
{
AddTraceMessage -message "computing fileshare total subscription."

$vhdsByFileShare = $fileShareSubscriptionDetails["vhdsByFileShare"];
$fileShareCapacityByFileShareID = $fileShareSubscriptionDetails["fileShareCapacityByFileShareID"];

AddTraceMessage -message ("Total shares fetched: " + $vhdsByFileShare.Count.ToString())

# for each file share, create property bag of share to subscription
if( ($vhdsByFileShare -eq $null) -or ($fileShareCapacityByFileShareID -eq $null))
{
AddTraceMessage -message "no fileshare to VHDs mapping found.";
return;
}

foreach($fileShareToVHD in $vhdsByFileShare.GetEnumerator())
{
$vhds = $fileShareToVHD.Value;
$fileshareID = $fileShareToVHD.Key;

#Create Storage Spaces data
$propertyBag = $momAPI.CreatePropertyBag()

$subscription = 0;
foreach ($vhd in $vhds)
{
$subscription += ($vhd.MaximumSize/1MB);
}

[int] $fileShareSubscriptionPercentage = 0;

$fileShareCapacity = $fileShareCapacityByFileShareID[$fileshareID];

if( ($fileShareCapacity -ne $null) -and ($fileShareCapacity -gt 0))
{
$fileShareSubscriptionPercentage = ($subscription * 100) / $fileShareCapacity;
}

$propertyBag.AddValue("FileShareId", $fileshareID);
$propertyBag.AddValue("SubscriptionPercentage", $fileShareSubscriptionPercentage.ToString());

AddTraceMessage -message ($fileshareID.ToString() + " " + $fileShareSubscriptionPercentage.ToString())

$propertyBag
}
}

AddTraceMessage -message "Storage monitoring script GetFileShareSubscription.ps1 completed."
}
Catch
{
AddTraceMessage -message $_.Exception.Message
}
Finally
{
if($session -ne $null)
{
Remove-PSSession $session;
}

if ($vmm -ne $null)
{
$vmm.Disconnect();
$vmm = $null;
}

if ($error -ne $null -and $error.Count -gt 0)
{
$momAPI.LogScriptEvent("GetFileShareSubscription.ps1", $GenericFailureEvent, 1, $error);
}

if ( ($script:traceMsg -ne $null) )
{
$momAPI.LogScriptEvent("GetFileShareSubscription.ps1", $GenericInformationEvent, 0, $script:traceMsg);
}
}</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>VMMServer</Name>
<Value>$Config/VMMServer$</Value>
</Parameter>
<Parameter>
<Name>Domain</Name>
<Value>$RunAs[Name="PROV2Library!Microsoft.SystemCenter.VirtualMachineManager.2012.VMMServerConnectionRunAsProfile"]/Domain$</Value>
</Parameter>
<Parameter>
<Name>Username</Name>
<Value>$RunAs[Name="PROV2Library!Microsoft.SystemCenter.VirtualMachineManager.2012.VMMServerConnectionRunAsProfile"]/UserName$</Value>
</Parameter>
<Parameter>
<Name>Pass</Name>
<Value>$RunAs[Name="PROV2Library!Microsoft.SystemCenter.VirtualMachineManager.2012.VMMServerConnectionRunAsProfile"]/Password$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<StrictErrorHandling>true</StrictErrorHandling>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="PS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<TriggerOnly>true</TriggerOnly>
</ProbeActionModuleType>