The time of the day (HH:MM) that the fragmentation check should be run
SchedulerDaysOfWeekMask
int
$Config/SchedulerDaysOfWeekMask$
Days Of Week mask
The day(s) that the fragmentation check should be run. The values for the days are Sunday (1), Monday (2), Tuesday (4), Wednesday (8), Thursday (16), Friday (32) and Saturday (64). To specify multiple days, add the values for the days together. For example, for Monday, Wednesday, and Friday, specify 42 (2+8+32).
FilePercentFragmentationThreshold
double
$Config/FilePercentFragmentationThreshold$
File Percent Fragmentation Threshold
If the "Use OS Recommendation" is set to "False" then this value will be used as the threshold for fragmentation levels.
UseOSRecommendation
bool
$Config/UseOSRecommendation$
Use OS Recommendation
This parameter determines whether the fragmentation level check will use the default threshold determined by the operating system or not. If this parameter is set to "False" then the value from "File Percent Fragmentation Threshold" will be used.
#---------------------------------------------------------------------------
# Returns WMI Instance requested. Tries to execute WMI query a N times.
#---------------------------------------------------------------------------
Function WMIGetInstanceExTryN
{
param ([string]$sTargetComputer,
[string]$sNamespace,
[string]$sInstanceQuery,
[int]$N)
for ($i = 0; $i -lt $N; $i++)
{
$error.Clear();
$oInstance = Get-CimInstance -ComputerName $sTargetComputer -Namespace $sNamespace -Query ("Select * from "+$sInstanceQuery) -ErrorAction SilentlyContinue
if ($error.Count -gt 0)
{
if ($i -eq ($N-1))
{
ThrowScriptError ("The class name '" + $sInstanceQuery + "' returned no instances. Please check to see if this is a valid WMI class name.") $error[0]
}
}
else
{
break;
}
sleep -m 1000
}
$error.Clear();
$oInstance = Get-CimInstance -ComputerName $sTargetComputer -Namespace $sNamespace -Query ("Select * from "+$sInstanceQuery) -ErrorAction SilentlyContinue
if ($error.Count -gt 0)
{
ThrowScriptError ("The class name '" + $sInstanceQuery + "' returned no instances. Please check to see if this is a valid WMI class name.") $error[0]
}
return $oInstance
}
#---------------------------------------------------------------------------
# Connect to WMI.
#---------------------------------------------------------------------------
Function WMIConnect
{
param ([string]$sTargetComputer,
[string]$sNamespace)
$error.Clear()
# !!! Refactoring comment:
# Original VBScript only tries to connect to the namespace. Piping to get only the first one saves time.
$oWMI = Get-CimClass -ComputerName $sTargetComputer -Namespace $sNamespace -ErrorAction SilentlyContinue | select -First 1
if ($error.Count -gt 0)
{
$msg = "Unable to open WMI Namespace 'winmgmts:\\" + $sTargetComputer + "\" + $sNamespace + "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists."
ThrowScriptError $msg $error[0]
}
}
#---------------------------------------------------------------------------
# Executes the WMI query and returns the result set.
#---------------------------------------------------------------------------
Function WMIExecQuery
{
param ([string]$sTargetComputer,
[string]$sNamespace,
[string]$sQuery)
$error.Clear()
# !!! Refactoring comment:
# Original VBScript only tries to connect to the namespace. Piping to get only the first one saves time.
$oWMI = Get-CimClass -ComputerName $sTargetComputer -Namespace $sNamespace -ErrorAction SilentlyContinue | select -First 1
if ($error.Count -gt 0)
{
$msg = "Unable to open WMI Namespace 'winmgmts:\\" + $sTargetComputer + "\" + $sNamespace + "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists."
ThrowScriptError $msg, $error[0]
}
$oQuery = Get-CimInstance -ComputerName $sTargetComputer -Namespace $sNamespace -Query $sQuery -ErrorAction SilentlyContinue
if ($error.Count -gt 0)
{
ThrowScriptError ("The Query '" + $sQuery + "' returned an invalid result set. Please check to see if this is a valid WMI Query.") $error[0]
}
return $oQuery
}
#---------------------------------------------------------------------------
# Executes the WMI query and returns the result set, no abort version.
#---------------------------------------------------------------------------
Function WMIExecQueryNoAbort
{
param ([string]$sTargetComputer,
[string]$sNamespace,
[string]$sQuery)
#---------------------------------------------------------------------------
# Creates an event and sends it back to the mom server.
#---------------------------------------------------------------------------
Function ThrowScriptErrorNoAbort
{
param ([string]$sMessage,
[System.Management.Automation.ErrorRecord]$oErr)
# Retrieve the name of this (running) script
$ScriptFileName = $MyInvocation.ScriptName
#---------------------------------------------------------------------------
# Creates an event and sends it back to the mom server.
#---------------------------------------------------------------------------
Function ThrowScriptError
{
param ([string]$sMessage,
[System.Management.Automation.ErrorRecord]$oErr)
ThrowScriptErrorNoAbort $sMessage $oErr
exit
}
#---------------------------------------------------------------------------
# Verifies that number of arguments is correct
#---------------------------------------------------------------------------
Function VerifyNumberOfArguments
{
param ($NumberOfArguments)
if ($args.Length -ne $NumberOfArguments)
{
$sArgs = ""
foreach ($argument in $args)
{
$sArgs += " {" + $argument + "}"
}
ThrowScriptError ("Invalid number of arguments (" + $args.Length + " instead of " + $NumberOfArguments + "). Arguments:" + $sArgs) $null
}
}
#---------------------------------------------------------------------------
# Outputs to file and echo for debugging purposes
#---------------------------------------------------------------------------
Function TraceLogMessage
{
param ([string]$sMessage)
Write-Host $sMessage
If ($g_DebugFlag -eq $true)
{
# Retrieve the name of this (running) script
$ScriptFileName = $MyInvocation.ScriptName
#---------------------------------------------------------------------------
# Verifies the expression. If equals to False then generates an error and quits the script
# Usage:
# Verify Not WMISet Is Nothing, "WMISet is invalid!"
# Verify WMISet.Count = 1, "Invalid quantity of services with name 'Server' (qty = " & WMISet.Count & ")."
#---------------------------------------------------------------------------
Function Verify
{
param ([bool]$bBool,
[string]$sMessage)
If ($bBool -eq $false)
{
ThrowScriptError $sMessage $null
}
}
Function GetRegistryKeyValue
{
param ([string]$keyPath,
[string]$key)
#Copyright (c) Microsoft Corporation. All rights reserved.
# Parameters that should be passed to this script
# 0 Computer (FQDN)
Function Main()
{
# Fragmentation analysis requires lots of time and consumes lots of CPU.
# So it is important to trace such activity in events log in order to be able
# to understand what is going on.
$g_DebugFlag = $true