SharePoint 管理パックの構成

Microsoft.SharePoint.Library.ConfigSharePoint (Task)

この管理タスクは、上書き管理パックの存在を確実にして、「実行」アカウントをサーバーに関連付け、プロキシ設定を有効にして、検出を開始することによって、管理パックを構成します。

Element properties:

TargetMicrosoft.SharePoint.Library.SPFarmGroup
AccessibilityInternal
CategoryOperations
EnabledTrue
RemotableFalse
Timeout300

Member Modules:

ID Module Type TypeId RunAs 
PA WriteAction Microsoft.SharePoint.Library.PSTaskWithoutRunAs Default

Source Code:

<Task ID="Microsoft.SharePoint.Library.ConfigSharePoint" Accessibility="Internal" Enabled="true" Target="Microsoft.SharePoint.Library.SPFarmGroup" Timeout="300">
<Category>Operations</Category>
<WriteAction ID="PA" TypeID="Microsoft.SharePoint.Library.PSTaskWithoutRunAs">
<ScriptName>AdminTask.ps1</ScriptName>
<ScriptBody><Script>$global:_RootConfigElement = "Configuration"
$global:_DefaultDiscoveryRunAsProfile = "Microsoft.SharePoint.Library.AdminAccount"
$global:_DefaultTimeoutSeconds = "60"

$global:_MOSSMPName = "Microsoft.SharePoint.Library"
$global:_OverrideMPName = $global:_MOSSMPName + ".Override"
$global:_OverrideMPFriendlyName = "SharePoint Management Pack Overrides"
$global:_OverrideMPVersion = "1.0.0.0"

$global:idlMPRefed = @()
$global:bOverrideMPChanged = $false
$global:mpOverride = $null

function global:Handle-Error([System.Object] $Arguments)
{
if (! $error)
{
return
}

$ErrMsg = ""
$IsExit = $true

if ("System.String" -eq $Arguments.GetType().FullName)
{
$ErrMsg = $Arguments
}
else
{
$ErrMsg = $Arguments[0]
$IsExit = $Arguments[1]
}

$ErrorActionPreference = "Continue"
$error | foreach {$host.ui.WriteErrorLine($_)}
$host.ui.WriteErrorLine($ErrMsg)

if ($IsExit)
{
if ($global:bOverrideMPChanged)
{
$global:mpOverride.RejectChanges()
}
exit(1)
}
else
{
$error.Clear()
$ErrorActionPreference = "SilentlyContinue"
}
}

function global:Accept-Overrides([System.Object] $mp)
{
$mp.AcceptChanges()
if ($error)
{
$mp.RejectChanges()
Handle-Error("Failed to update override in management pack {0}" -f $mp.Name)
}
}

function global:Ensure-ManagementPackReference([System.Object] $mpref)
{
$mp = $mpref.GetElement().GetManagementPack()

$idMP = $mp.Id
if ($global:idlMPRefed -notcontains $idMP)
{
$global:idlMPRefed += @($idMP)
$refmp = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPackReference($mp)
$global:mpOverride.References.Add("Ref_" + $idMP.ToString("N"), $refmp)
$global:bOverrideMPChanged = $true
}
Handle-Error("Failed to ensure management reference")
}

function global:Get-ConfigurationOverride([System.Object] $Arguments)
{
$sType = $Arguments[0]
$overrides = $Arguments[1]
$sConfig = $Arguments[2]
foreach ($override in $overrides)
{
if ($override.GetType().Name -eq $sType -and $override.Parameter -eq $sConfig)
{
return $override
}
}
return $null
}

function global:Get-DiscoveryConfigurationOverride([System.Object] $Arguments)
{
switch($global:operationsManagerVersionMajor)
{
6
{
return Get-ConfigurationOverride("MonitoringDiscoveryConfigurationOverride", $Arguments[0], $Arguments[1])
}
7
{
return Get-ConfigurationOverride("ManagementPackDiscoveryConfigurationOverride", $Arguments[0], $Arguments[1])
}
default { throw("Not supported Operations Manager version '$($global:operationsManagerVersionMajor)'.") }
}
}

function global:Get-MonitorConfigurationOverride([System.Object] $Arguments)
{
switch($global:operationsManagerVersionMajor)
{
6
{
return Get-ConfigurationOverride("MonitorConfigurationOverride", $Arguments[0], $Arguments[1])
}
7
{
return Get-ConfigurationOverride("ManagementPackMonitorConfigurationOverride", $Arguments[0], $Arguments[1])
}
default { throw("Not supported Operations Manager version '$($global:operationsManagerVersionMajor)'.") }
}
}

function global:Ensure-OverrideManagementPack([System.Object] $Arguments)
{
$mg = $Arguments[0]
$mp = $Arguments[1]
$mpAlias = $Arguments[2]

if ($mp.Sealed)
{
$refmp = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPackReference($mp)
$global:mpOverride = $mg.GetManagementPacks($global:_OverrideMPName)[0]
if ($error)
{
$error.Clear()
write-output("Create override management pack $global:_OverrideMPName")
$global:mpOverride = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPack($global:_OverrideMPName, $global:_OverrideMPFriendlyName, (new-object System.Version($global:_OverrideMPVersion)), $mg)
$global:mpOverride.References.Add($mpAlias, $refmp)
$mg.ImportManagementPack($global:mpOverride)
Handle-Error("Failed to create override management pack $global:_OverrideMPName")
$global:idlMPRefed += @($global:mpOverride.Id, $mp.Id)
}
else
{
write-output("Found override management pack $global:_OverrideMPName version {0}" -f $global:mpOverride.Version)
$global:idlMPRefed += @($global:mpOverride.Id)
if (! $global:mpOverride.References.ContainsValue($refmp))
{
write-output("Add {0} reference to the override managment pack" -f $mpName)
$global:mpOverride.References.Add($mpAlias, $refmp)
$global:idlMPRefed += @($mp.Id)
$global:bOverrideMPChanged = $true
}
}
}
else
{
write-output("Management pack $global:_MOSSMPName is not sealed. Overrides can only live within the same management pack")
$global:mpOverride = $mp
$global:idlMPRefed += @($mp.Id)
}

foreach ($refmp in $global:mpOverride.References.Values)
{
$global:idlMPRefed += @($refmp.GetManagementPack().Id)
}
}

function global:Create-ConfigurationOverride([System.Object] $Arguments)
{
$mpe = $Arguments[0]
$bMonitor = $Arguments[1]
$overrideName = $Arguments[2]
$sConfig = $Arguments[3]
$val = $Arguments[4]
$msg = $Arguments[5]

if ($bMonitor)
{
$override = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitorConfigurationOverride($global:mpOverride, $overrideName)
$override.Monitor = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference``1[Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitor]]::Op_Implicit($mpe)
Ensure-ManagementPackReference($override.Monitor)
}
else
{
$override = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPackDiscoveryConfigurationOverride($global:mpOverride, $overrideName)
$override.Discovery = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference``1[Microsoft.EnterpriseManagement.Configuration.ManagementPackDiscovery]]::Op_Implicit($mpe)
Ensure-ManagementPackReference($override.Discovery)
$override.Module = $mpe.DataSource
}
$override.Context = $mpe.target
Ensure-ManagementPackReference($mpe.target)
$override.Parameter = $sConfig
$override.Value = $val
$global:bOverrideMPChanged = $true

if ($msg -eq $null)
{
$msg = "Create '" + $sConfig +"' configuration override with value $val for " + $mpe.Name
}

write-output($msg)
}

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

$null = [System.Reflection.Assembly]::Load("Microsoft.EnterpriseManagement.OperationsManager, Version=7.0.5000.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")
$global:operationsManagerVersionMajor = 7
if ($error)
{

$error.Clear()
$null = [System.Reflection.Assembly]::Load("Microsoft.EnterpriseManagement.OperationsManager, Version=6.0.4900.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")
Handle-Error("Failed to load Microsoft.EnterpriseManagement.OperationsManager.dll")
$global:operationsManagerVersionMajor = 6
}

$mg = new-object Microsoft.EnterpriseManagement.ManagementGroup("localhost")
Handle-Error("Failed to connect to local management group")

function global:Get-HealthServiceReference([System.Object] $Arguments)
{
$references = $Arguments[0]
$idHealtService = $Arguments[1]
$ref = $null

foreach ($reference in $references)
{
if ($reference.HealthServiceId -eq $idHealthService)
{
$ref = $reference
break
}
}

return $ref
}

function global:Get-PropertyOverride([System.Object] $Arguments)
{
$sType = $Arguments[0]
$overrides = $Arguments[1]
$sProperty = $Arguments[2]
foreach ($override in $overrides)
{
if ($override.GetType().Name -eq $sType -and $override.Property -eq $sProperty)
{
return $override
}
}
return $null
}

function global:Get-DiscoveryPropertyOverride([System.Object] $Arguments)
{
switch($global:operationsManagerVersionMajor)
{
6
{
return Get-PropertyOverride("MonitoringDiscoveryPropertyOverride", $Arguments[0], $Arguments[1])
}
7
{
return Get-PropertyOverride("ManagementPackDiscoveryPropertyOverride", $Arguments[0], $Arguments[1])
}
default { throw("Not supported Operations Manager version '$($global:operationsManagerVersionMajor)'.")}
}
}

function global:Get-MonitorPropertyOverride([System.Object] $Arguments)
{
switch($global:operationsManagerVersionMajor)
{
6
{
return Get-PropertyOverride("MonitorPropertyOverride", $Arguments[0], $Arguments[1])
}
7
{
return Get-PropertyOverride("ManagementPackMonitorPropertyOverride", $Arguments[0], $Arguments[1])
}
default { throw("Not supported Operations Manager version '$($global:operationsManagerVersionMajor)'.") }
}
}

function global:Get-Workflows([System.Object] $Arguments)
{
$mp = $Arguments[0]
$sID = $Arguments[1]
$bMonitor = $Arguments[2]

$mpel = @()
$sl = $sID.Split(';')
foreach ($s in $sl)
{
$s = $s.Trim()
if ($s -eq "")
{
continue
}
$mpe = $null
if ($bMonitor)
{
$mpe = $mp.GetMonitor($s)
$error.Clear()
if ($mpe -eq $null)
{
$mpe = $mp.GetMonitor($s)
}
}
else
{
$mpe = $mp.GetDiscovery($s)
$error.Clear()
if ($mpe -eq $null)
{
$mpe = $mp.GetDiscovery($s)
}
}
$mpel += @($mpe)
}

return $mpel
}

function global:Get-EffectiveConfigurationValue([System.Object] $Arguments)
{
$mg = $Arguments[0]
$mpe = $Arguments[1][0]
$sConfig = $Arguments[2]
$sDefVal = $Arguments[3]

$target = $mpe.Target
$criteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringOverrideCriteria("TargetId='" + $mpe.Id.ToString() + "' AND ContextId='" + $target.Id.ToString() + "'")
$overrides = $mg.GetMonitoringOverrides($criteria)
$override = $null
$bMonitor = $false
if ($mpe.GetType().Name -eq "MonitoringDiscovery" -or $mpe.GetType().Name -eq "ManagementPackDiscovery")
{
$override = Get-DiscoveryConfigurationOverride($overrides, $sConfig)
}
else
{
$bMonitor = $true
$override = Get-MonitorConfigurationOverride($overrides, $sConfig)
}
if ($override -ne $null)
{
return $override.Value
}
else
{
$xmlConfig = $null
if ($bMonitor)
{
[xml] $xmlConfig = "&lt;Config&gt;" + $mpe.Configuration + "&lt;/Config&gt;"
}
else
{
[xml] $xmlConfig = "&lt;Config&gt;" + $mpe.DataSource.Configuration + "&lt;/Config&gt;"
}
$sVal = $xmlConfig.Config.$sConfig
if ([System.String]::IsNullOrEmpty($sVal))
{
return $sDefVal
}
else
{
return $sVal
}
}
}

function global:Set-EffectiveConfigurationValue([System.Object] $Arguments)
{
$mg = $Arguments[0]
$mpel = $Arguments[1]
$sConfig = $Arguments[2]
$val = $Arguments[3]
$htOverride = $Arguments[4]

foreach ($mpe in $mpel)
{
$target = $mpe.Target
$overrides = $htOverride[$mpe.Id.ToString()]
$override = $null
$bMonitor = $false

if ($mpe.GetType().Name -eq "MonitoringDiscovery" -or $mpe.GetType().Name -eq "ManagementPackDiscovery")
{
$override = Get-DiscoveryConfigurationOverride($overrides, $sConfig)
}
else
{
$bMonitor = $true
$override = Get-MonitorConfigurationOverride($overrides, $sConfig)
}

$xmlConfig = $null
if ($bMonitor)
{
[xml] $xmlConfig = "&lt;Config&gt;" + $mpe.Configuration + "&lt;/Config&gt;"
}
else
{
[xml] $xmlConfig = "&lt;Config&gt;" + $mpe.DataSource.Configuration + "&lt;/Config&gt;"
}
$valDef = $xmlConfig.Config.$sConfig

if ($valDef -eq $null)
{
if ($bMonitor)
{
$xmlConfig = "&lt;Config&gt;" + $mpe.TypeID.GetElement().Configuration + "&lt;/Config&gt;"
$valDef = $xmlConfig.Config.$sConfig
}
if ($valDef -eq $null)
{
write-output($mpe.Name + " does not have configuration $sConfig")
return
}
}

if ($override -eq $null)
{
if ($valDef -ne $val)
{

$sName = "Override." + [guid]::NewGuid().ToString("N")
Create-ConfigurationOverride($mpe, $bMonitor, $sName, $sConfig, $val)
}
}
else
{
if ($override.Value -ne $val)
{
$mp = $override.GetManagementPack()
if ($valDef -ne $val)
{
$override.Value = $val
$override.Status = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingUpdate
write-output("Change '" + $sConfig + "' configuration override to $val for " + $mpe.Name)
}
else
{
$override.Status = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingDelete
write-output("Delete '" + $sConfig + "' configuration override for " + $mpe.Name)
}
if ($mp -eq $global:mpOverride)
{
$global:bOverrideMPChanged = $true
}
else
{
Accept-Overrides($mp)
}
}
}
}
}

function global:Ensure-Enabled([System.Object] $Arguments)
{
$mg = $Arguments[0]
$mpel = $Arguments[1]
$bEnabled = $Arguments[2]
$htOverride = $Arguments[3]

$IsEnabled = "false"
if ($bEnabled)
{
$IsEnabled = "true"
}

foreach ($mpe in $mpel)
{
$target = $mpe.Target
$sId = $mpe.Id.ToString()
$criteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringOverrideCriteria("TargetId='" + $sId + "' AND ContextId='" + $target.Id.ToString() + "'")
$overrides = $mg.GetMonitoringOverrides($criteria)
$htOverride.Add($sId, $overrides)
$override = $null
$bMonitor = $false
if ($mpe.GetType().Name -eq "MonitoringDiscovery" -or $mpe.GetType().Name -eq "ManagementPackDiscovery")
{
$override = Get-DiscoveryPropertyOverride($overrides, "Enabled")
}
else
{
$bMonitor = $true
$override = Get-MonitorPropertyOverride($overrides, "Enabled")
}

if ($override -eq $null)
{
$sEnabled = $mpe.Enabled
if ($sEnabled -eq "onEssentialMonitoring")
{
$sEnabled = "true"
}
if ($sEnabled -ne $IsEnabled)
{

$sName = "Override." + [guid]::NewGuid().ToString("N")
if ($bMonitor)
{
$override = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitorPropertyOverride($global:mpOverride, $sName)
$override.Monitor = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference``1[Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitor]]::Op_Implicit($mpe)
Ensure-ManagementPackReference($override.Monitor)
$override.Property = [Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitorProperty]::Enabled
}
else
{
$override = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPackDiscoveryPropertyOverride($global:mpOverride, $sName)
$override.Discovery = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference``1[Microsoft.EnterpriseManagement.Configuration.ManagementPackDiscovery]]::Op_Implicit($mpe)
Ensure-ManagementPackReference($override.Discovery)
$override.Property = [Microsoft.EnterpriseManagement.Configuration.ManagementPackWorkflowProperty]::Enabled
}
$override.Context = $target
Ensure-ManagementPackReference($target)
$override.Value = $IsEnabled
$global:bOverrideMPChanged = $true
write-output("Create 'Enabled' property override with value $IsEnabled for " + $mpe.Name)
}
}
else
{
if ($override.Value -ne $IsEnabled)
{
$mp = $override.GetManagementPack()
if ($mpe.Enabled -ne $IsEnabled)
{
$override.Value = $IsEnabled
$override.Status = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingUpdate
write-output("Change 'Enabled' property override to $IsEnabled for " + $mpe.Name)
}
else
{
$override.Status = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingDelete
write-output("Delete 'Enabled' property override for " + $mpe.Name)
}
if ($mp -eq $global:mpOverride)
{
$global:bOverrideMPChanged = $true
}
else
{
Accept-Overrides($mp)
}
}
}
}
}

$sConfig = $Args[0]
write-output("Load configuration file $sConfig")
[System.Xml.XmlDocument] $xdConfig = new-object System.Xml.XmlDocument
$xdConfig.Load($sConfig)
Handle-Error("Failed to load configuration file $sConfig")

$mpMOSS = $mg.GetManagementPacks($global:_MOSSMPName)[0]
Handle-Error("Failed to find management pack $global:_MOSSMPName")
write-output("Configure $global:_MOSSMPName version " + $mpMOSS.Version)

Ensure-OverrideManagementPack($mg, $mpMOSS, "MOSS")

$admin = $mg.GetAdministration()
$servers = $admin.GetAllManagementServers()
$agents = $admin.GetAllAgentManagedComputers()
$htHealthServices = @{}
$htReferences = @{}
$xnl = $xdConfig.SelectNodes("/$global:_RootConfigElement/Association")
foreach ($xnAssociation in $xnl)
{
$bAgentType = ($xnAssociation.Type -eq "Agent")
$sProfile = $global:_DefaultDiscoveryRunAsProfile
$sAccount = $xnAssociation.Account

$criteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringSecureReferenceCriteria("Name='$sProfile'")
$raProfile = $mg.GetMonitoringSecureReferences($criteria)[0]
$idProfile = $raProfile.Id
Handle-Error("Failed to find RunAs profile $sProfile")

$references = $htReferences.Get_Item($idProfile)
if ($references -eq $null)
{
$references = $mg.GetMonitoringSecureDataHealthServiceReferenceBySecureReferenceId($idProfile)
$htReferences.Add($idProfile, $references)
}
Handle-Error("Failed to get existing RunAs associations for $sProfile")

switch($global:operationsManagerVersionMajor)
{
6
{
$criteria = new-object Microsoft.EnterpriseManagement.Monitoring.Security.MonitoringSecureDataCriteria("Name='$sAccount'")
$raAccount = $mg.GetMonitoringSecureData($criteria)[0]
}
7
{
$criteria = new-object Microsoft.EnterpriseManagement.SecureDataCriteria("Name='$sAccount'")
$raAccount = $mg.Security.GetSecureData($criteria)[0]
}
default { throw("Not supported Operations Manager version '$($global:operationsManagerVersionMajor)'.") }
}
if ($raAccount -eq $null)
{
throw("Account $sAccount doesn't exist")
}
else
{
$idAccount = $raAccount.Id
}
Handle-Error("Failed to find RunAs account $sAccount")

$slMachineFilters = $()
$xnlMachines = $xnAssociation.SelectNodes("Machine")
foreach ($xnMachine in $xnlMachines)
{
$slMachineFilters += @($xnMachine.Name)
}
Handle-Error("Failed to read in machine filters")

$slHealthServices = $htHealthServices.Get_Item($sProfile)
if ($slHealthServices -eq $null)
{
$slHealthServices = @()
}

$machines = $null
if ($bAgentType)
{
$machines = $agents
}
else
{
$machines = $servers
}
foreach ($machine in $machines)
{
$sMachine = $machine.Name
$idHealthService = $machine.HostedHealthService.Id
if ($slHealthServices -contains $idHealthService)
{
write-output("More than one Association elements for $sProfile match $sMachine. The first account is used")
continue
}
foreach ($sFilter in $slMachineFilters)
{
if ($sMachine -match $sFilter)
{
$reference = Get-HealthServiceReference($references, $idHealthService)
if ($reference -eq $null)
{
$reference = new-object Microsoft.EnterpriseManagement.Monitoring.Security.MonitoringSecureDataHealthServiceReference($idAccount, $idProfile, $idHealthService)
$mg.InsertMonitoringSecureDataHealthServiceReference($reference)
Handle-Error("Failed to associate $sAccount to $sMachine for $sProfile")
write-output("Account $sAccount is associated to $sMachine for $sProfile")
}
elseif ($reference.MonitoringSecureDataId -ne $idAccount)
{
$reference.MonitoringSecureDataId = $idAccount
$reference.Update()
Handle-Error("Failed to update account on $sMachine for $sProfile")
write-output("Account on $sMachine for $sProfile is updated to $sAccount")
}

$slHealthServices += @($idHealthService)
break
}
Handle-Error("$sFilter (Name attribute of Machine node) is not a valid regular expression.")
}
}

$htHealthServices.Set_Item($sProfile, $slHealthServices)
}

foreach ($idProfile in $htReferences.Keys)
{
$references = $htReferences[$idProfile]
$raProfile = $mg.GetMonitoringSecureReference($idProfile)
$sProfile = $raProfile.Name
$slHealthServices = $htHealthServices.Get_Item($sProfile)
foreach ($reference in $references)
{
$idHealthService = $reference.HealthServiceId
if ($slHealthServices -notcontains $idHealthService)
{
$mg.DeleteMonitoringSecureDataHealthServiceReference($reference)
$sMachine = $admin.GetComputerHealthServiceByHealthServiceId($idHealthService).Name
Handle-Error("Failed to remove RunAs association to $sMachine from $sProfile")
write-output("RunAs association to $sMachine is removed from $sProfile")
}
}
}

$sProfile = $global:_DefaultDiscoveryRunAsProfile
$slHealthServices = $htHealthServices.Get_Item($sProfile)
foreach ($agent in $agents)
{
$sMachine = $agent.Name
$idHealthService = $agent.HostedHealthService.Id
if ($slHealthServices -contains $idHealthService -and ! $agent.ProxyingEnabled.Value)
{
$agent.ProxyingEnabled = $true
$agent.ApplyChanges()
Handle-Error("Failed to allow " + $sMachine + " as a proxy")
write-output("Allow " + $sMachine + " as a proxy")
}
}

$dtBase = [DateTime]::Now
$discoveriesMOSS = $null
$monitorsMOSS = $null
$xnl = $xdConfig.SelectNodes("/$global:_RootConfigElement/WorkflowCycle")
foreach ($xnCycle in $xnl)
{
$dtCycle = $null
$s = $xnCycle.BaseStartTime
switch ($s.Split(':').Length)
{
1 {$dtCycle = $dtBase.AddSeconds([System.Int32]::Parse($s))}
2 {$dtCycle = [DateTime]::Parse($s)}
default {throw "BaseStartTime value is in an unsupported format"}
}
Handle-Error("BaseStartTime value $s is undefined or invalid")
if ($dtCycle.Second -ne 0)
{
$dtCycle = $dtCycle.AddSeconds(60 - $dtCycle.Second)
}

$s = $xnCycle.Length
$cSecondsCycle = [System.Int32]::Parse($s)
if ($cSecondsCycle -le 0)
{
throw "Cycle length must be greater than 0"
}
if ($cSecondsCycle -ne [System.Int32]($cSecondsCycle/60) * 60)
{
throw "Cycle length must be in whole minutes (times of 60)"
}
Handle-Error("Length value $s is undefined or invalid")

$s = $xnCycle.Spacing
$cSecondsSpacing = [System.Int32]::Parse($s)
Handle-Error("Spacing value $s is undefined or invalid")

$s = $xnCycle.WorkflowTimeout
$cSecondsDefaultTimeout = $null
if (! [System.String]::IsNullOrEmpty($s))
{
$cSecondsDefaultTimeout = [System.Int32]::Parse($s)
if ($cSecondsDefaultTimeout -le 0)
{
throw "Timeout must be greater than 0"
}
Handle-Error("WorkflowTimeout value $s is invalid")
}

$cMaxTimes = 1
$cSeconds = 0
$olWF = @()
$ilTimes = @()
$ilTimeout = @()
$xnlWF = $xnCycle.SelectNodes("Workflow")
foreach ($xn in $xnlWF)
{
$mpel = $null

$sID = $xn.Id
if ([System.String]::IsNullOrEmpty($sID))
{
throw("Workflow: " + $xn.get_OuterXml())
}
Handle-Error("Workflow ID is undefined")

$bMonitor = $false
$s = $xn.Type
switch ($s)
{
"Monitor" {$bMonitor = $true}
"Discovery" {}
default {
throw "Unsupported workflow type $s"
Handle-Error("Workflow type $s is not supported")
}
}

$mp = $mpMOSS
$s = $xn.MP
if (! [System.String]::IsNullOrEmpty($s))
{
$matchedMps = $null
$matchedMps = $mg.GetManagementPacks($s)
if ($matchedMps -eq $null -or $matchedMps.Count -eq 0)
{
Write-Output("Cannot find the management pack $s. Skipped.")
$Error.Clear()
continue
}
$mp = $matchedMps[0]
}
[System.Object[]] $mpel = Get-Workflows($mp, $sID, $bMonitor)
Handle-Error("Failed to find workflows $sID in " + $mp.Name)
$olWF += ,$mpel

$s = $xn.Times
$cTimes = [System.Int32]::Parse($s)
if ($cTimes -gt 0)
{
if ($cSecondsCycle -ne [System.Int32]($cSecondsCycle / $cTimes) * $cTimes)
{
throw "Times $s can not divide cycle length $cSecondsCycle"
}
elseif ($cTimes -gt $cTimesMax)
{
$cTimesMax = $cTimes
}
}
Handle-Error("Times value $s is invalid")
$ilTimes += @($cTimes)

$cSecondsTimeout = 0
$s = $xn.Timeout
if (! [System.String]::IsNullOrEmpty($s))
{
$cSecondsTimeout = [System.Int32]::Parse($s)
if ($cSecondsTimeout -le 0)
{
throw "Timeout must be greater than 0"
}
Handle-Error("Timeout value $s is invalid")
}
elseif ($cSecondsDefaultTimeout -ne $null)
{
$cSecondsTimeout = $cSecondsDefaultTimeout
}
else
{
$cSecondsTimeout = Get-EffectiveConfigurationValue($mg, $mpel, "TimeoutSeconds", $global:_DefaultTimeoutSeconds)
$cSecondsTimeout = [System.Int32]::Parse($cSecondsTimeout)
Handle-Error("Failed to get effective TimeoutSeconds configuration value for $sID")
}
$ilTimeout += @($cSecondsTimeout)

if ($cTimes -gt 0)
{
$cSeconds += [System.Math]::Ceiling(($cSecondsTimeout + $cSecondsSpacing) / 60) * 60
}
}

$cSeconds *= $cTimesMax
if ($cSeconds -gt $cSecondsCycle)
{
throw "Cycle length $cSecondsCycle is not long enough to ensure the order of workflows"
Handle-Error("Please change cycle length to no less than $cSeconds or decrease times, timeout values, and/or spacing")
}

$cWorkflows = $olWF.Count
$cSeconds = 0
for ($i = 0; $i -lt $cWorkflows; $i++)
{
$htOverride = @{}
$mpel = $olWF[$i]
if ($ilTimes[$i] -gt 0)
{

Ensure-Enabled($mg, $mpel, $true, $htOverride)
Handle-Error("Failed to enable workflow")
$dt = $dtCycle.AddSeconds($cSeconds)
Set-EffectiveConfigurationValue($mg, $mpel, "SyncTime", $dt.ToString("HH:mm", [System.Globalization.CultureInfo]::InvariantCulture), $htOverride)
Handle-Error("Failed to set SyncTime")
Set-EffectiveConfigurationValue($mg, $mpel, "IntervalSeconds", ($cSecondsCycle / $ilTimes[$i]), $htOverride)
Handle-Error("Failed to set IntervalSeconds")
Set-EffectiveConfigurationValue($mg, $mpel, "TimeoutSeconds", $ilTimeout[$i], $htOverride)
Handle-Error("Failed to set TimeoutSeconds")
$cSeconds += [System.Math]::Ceiling(($ilTimeout[$i] + $cSecondsSpacing) / 60) * 60
}
else
{
Ensure-Enabled($mg, $mpel, $false, $htOverride)
Handle-Error("Failed to disable workflow")
}
}
}

if ($global:bOverrideMPChanged)
{
$global:bOverrideMPChanged = $false
Accept-Overrides($global:mpOverride)
}

write-output("SharePoint management pack configuration completed successfully")</Script></ScriptBody>
<Arguments>Microsoft.SharePoint.Library.mp.config</Arguments>
<WorkingDirectory>%ProgramFiles%\System Center Management Packs\</WorkingDirectory>
<TimeoutSeconds>300</TimeoutSeconds>
</WriteAction>
</Task>