locked
Exception calling "AcceptChanges" with "0" argument(s) RRS feed

  • Question

  • Hi,

    I am trying to run a powershell script which was written by Russ Slaten to create scom performance collection rule. But i am getting the below error at the final stage while saving the changes into the management pack ($MP.AcceptChanges()). Below is the script and also the error message. If any one know the cause or the solution pls reply...

    Param(            
        [parameter(Mandatory=$true)]            
        $ManagementServer,            
        [parameter(Mandatory=$true)]            
        $ManagementPackID,            
        [parameter(Mandatory=$true)]            
        $RuleID,
        [parameter(Mandatory=$true)]
        $RuleName,
        [parameter(Mandatory=$true)]
        $RuleDescription,
        [parameter(Mandatory=$true)]
        $RuleTarget,
        [parameter(Mandatory=$true)]
        $ComputerName,
        [parameter(Mandatory=$true)]
        $ObjectName,
        [parameter(Mandatory=$true)]
        $CounterName,
        [parameter(Mandatory=$true)]
        $AllInstances,
        [parameter(Mandatory=$true)]
        $InstanceName,
        [parameter(Mandatory=$true)]
        $IntervalSeconds,
        [parameter(Mandatory=$true)]
        $DWOnly,
        [parameter(Mandatory=$true)]
        $Enabled,
        [parameter(Mandatory=$true)]
        $IsOptimized,
        $Tolerance,
        $ToleranceType,
        $MaximumSampleSeparation
        )
    
    Write-Host "ManagementServer: "$ManagementServer
    Write-Host "ManagementPackID: "$ManagementPackID
    Write-Host "RuleID: "$RuleID
    Write-Host "RuleName: "$RuleName
    Write-Host "RuleDescription: "$RuleDescription
    Write-Host "RuleTarget: "$RuleTarget
    Write-Host "ComputerName: "$ComputerName
    Write-Host "ObjectName: "$ObjectName
    Write-Host "CounterName: "$CounterName
    Write-Host "AllInstances: "$AllInstances
    Write-Host "InstanceName: "$InstanceName
    Write-Host "IntervalSeconds: "$IntervalSeconds
    Write-Host "DWOnly: "$DWOnly
    Write-Host "Enabled: "$Enabled
    Write-Host "IsOptimized: "$IsOptimized
    Write-Host "Tolerance: "$Tolerance
    Write-Host "ToleranceType: "$ToleranceType
    Write-Host "MaximumSampleSeparation: "$MaximumSampleSeparation
    
    function CreateManagementPack
    {
      param([object]$ManagementServer, [string]$ManagementPackID)
      $MPStore = New-Object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore
      $MP = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPack($ManagementPackID, $ManagementPackID, (New-Object Version(1, 0, 0)), $MPStore)
      $ManagementServer.ImportManagementPack($MP)
    }
    
    function CreateSystemPerformanceDataProviderModule
    {
      param([object]$Rule, [object]$ManagementServer)
      $DSModuleType = $ManagementServer.GetMonitoringModuleTypes("System.Performance.DataProvider")[0]
      $DSModule = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModule($Rule, "DS")
      $DSModule.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModuleType]$DSModuleType
      $DSModule.Configuration = CreateSystemPerformanceDataProviderModuleConfig $CounterName $InstanceName $ObjectName $IntervalSeconds $AllInstances $ComputerName
      $Rule.DataSourceCollection.Add($DSModule)
    }
    
    function CreateSystemPerformanceOptimizedDataProviderModule
    {
      param([object]$Rule, [object]$ManagementServer)
      $DSModuleType = $ManagementServer.GetMonitoringModuleTypes("System.Performance.OptimizedDataProvider")[0]
      $DSModule = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModule($Rule, "DS")
      $DSModule.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModuleType]$DSModuleType
      $DSModule.Configuration = CreateSystemPerformanceOptimizedDataProviderModuleConfig $CounterName $InstanceName $ObjectName $IntervalSeconds $AllInstances $ComputerName $Tolerance $ToleranceType $MaximumSampleSeparation
      $Rule.DataSourceCollection.Add($DSModule)
    }
    
    function CreateSystemPerformanceDataProviderModuleConfig
    {
      param([string]$CounterName, [string]$InstanceName, [string]$ObjectName, [int]$IntervalSeconds, [string]$AllInstances, [string]$ComputerName)
      $oBuilder = New-Object System.Text.StringBuilder
      [void]$oBuilder.AppendFormat("<ComputerName>{0}</ComputerName>", $ComputerName)
      [void]$oBuilder.AppendFormat("<CounterName>{0}</CounterName>", (XMLEncode -s $CounterName))
      [void]$oBuilder.AppendFormat("<ObjectName>{0}</ObjectName>", (XMLEncode -s $ObjectName))
      if ($AllInstances -eq 'false') {[void]$oBuilder.AppendFormat("<InstanceName>{0}</InstanceName>", (XMLEncode -s $InstanceName))}
      [void]$oBuilder.AppendFormat("<AllInstances>{0}</AllInstances>", $AllInstances)
      [void]$oBuilder.AppendFormat("<Frequency>{0}</Frequency>", $IntervalSeconds)
      return $oBuilder.ToString()
    }
    
    function CreateSystemPerformanceOptimizedDataProviderModuleConfig
    {
      param([string]$CounterName, [string]$InstanceName, [string]$ObjectName, [int]$IntervalSeconds, [string]$AllInstances, [string]$ComputerName)
      $oBuilder = New-Object System.Text.StringBuilder
      [void]$oBuilder.AppendFormat("<ComputerName>{0}</ComputerName>", $ComputerName)
      [void]$oBuilder.AppendFormat("<CounterName>{0}</CounterName>", (XMLEncode -s $CounterName))
      [void]$oBuilder.AppendFormat("<ObjectName>{0}</ObjectName>", (XMLEncode -s $ObjectName))
      if ($AllInstances -eq 'false') {[void]$oBuilder.AppendFormat("<InstanceName>{0}</InstanceName>", (XMLEncode -s $InstanceName))}
      else {[void]$oBuilder.Append("<InstanceName />")}
      [void]$oBuilder.AppendFormat("<AllInstances>{0}</AllInstances>", $AllInstances)
      [void]$oBuilder.AppendFormat("<Frequency>{0}</Frequency>", $IntervalSeconds)
      [void]$oBuilder.AppendFormat("<Tolerance>{0}</Tolerance>", $Tolerance)
      [void]$oBuilder.AppendFormat("<ToleranceType>{0}</ToleranceType>", $ToleranceType)
      [void]$oBuilder.AppendFormat("<MaximumSampleSeparation>{0}</MaximumSampleSeparation>", $MaximumSampleSeparation)
      return $oBuilder.ToString()
    }
    
    function CreatePerformanceCollectionWriteToDB
    {
      param([object]$Rule, [object]$ManagementServer)
      $WAModuleTypeDB = $ManagementServer.GetMonitoringModuleTypes("Microsoft.SystemCenter.CollectPerformanceData")[0]
      $WAModuleDB = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModule($Rule, "WriteToDB")
      $WAModuleDB.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModuleType]$WAModuleTypeDB
      $Rule.WriteActionCollection.Add($WAModuleDB)
    }
    
    function CreatePerformanceCollectionWriteToDW
    {
      $WAModuleTypeDW = $ManagementServer.GetMonitoringModuleTypes("Microsoft.SystemCenter.DataWarehouse.PublishPerformanceData")[0]
      $WAModuleDW = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModule($Rule, "WriteToDW")
      $WAModuleDW.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackWriteActionModuleType]$WAModuleTypeDW
      $Rule.WriteActionCollection.Add($WAModuleDW)
    }
    
    function XMLEncode
    {
      param([string]$s)
      $s = $s.Replace("&", "&amp;")
      $s = $s.Replace("<", "&lt;")
      $s = $s.Replace(">", "&gt;")
      $s = $s.Replace('"', "&quot;")
      $s = $s.Replace("'", "&apos;")
      return $s.ToString()
    }
    
    Write-Host "Adding SCOM Snap-in"
    Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client
    
    Write-Host "Connecting to SCOM Management Group"
    $ManagementServer = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer)
    
    Write-Host "Getting MP Information and Incrementing Version"
    try
    {
      $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0]
      $VIncrement = $MP.Version.ToString().Split('.')
      $VIncrement[$VIncrement.Length - 1] = ([system.int32]::Parse($VIncrement[$VIncrement.Length - 1]) + 1).ToString()
      $MP.Version = ([string]::Join(".", $VIncrement))
    }
    catch
    {
      Write-Host "MP Not Found, Creating New MP"
      CreateManagementPack $ManagementServer $ManagementPackID
      $MP = $ManagementServer.GetManagementPacks($ManagementPackID)[0]
    }
    
    Write-Host "Creating New Rule"
    $Rule = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackRule($MP, $RuleID)
    
    Write-Host "Creating Data Source Section of Rule"
    [bool]$IsOptimized = [System.Convert]::ToBoolean($IsOptimized)
    if (!$IsOptimized) {CreateSystemPerformanceDataProviderModule $Rule $ManagementServer}
    else {CreateSystemPerformanceOptimizedDataProviderModule $Rule $ManagementServer}
    
    Write-Host "Creating Write Action Section of Rule"
    CreatePerformanceCollectionWriteToDW $Rule $ManagementServer
    [bool]$DWOnly = [System.Convert]::ToBoolean($DWOnly)
    if (!($DWOnly)){CreatePerformanceCollectionWriteToDB $Rule $ManagementServer}
    
    Write-Host "Adding Rule Target"
    $Rule.Target = $ManagementServer.GetMonitoringClasses($RuleTarget)[0]
    
    Write-Host "Adding Rule Category"
    $Rule.Category = [Microsoft.EnterpriseManagement.Configuration.ManagementPackCategoryType]::PerformanceCollection
    
    Write-Host "Adding Display Name"
    $Rule.DisplayName = (XMLEncode -s $RuleName)
    
    Write-Host "Adding Description"
    $Rule.Description = (XMLEncode -s $RuleDescription)
    
    Write-Host "Setting Enabled Property"
    [bool]$Enabled = [System.Convert]::ToBoolean($Enabled)
    If (!($Enabled)){$Rule.Enabled = [Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitoringLevel]::false}
    
    Write-Host "Writing Changes via SDK"
    $MP.AcceptChanges()
    
    Write-Host "Script Completed"

    Error is as below

    Exception calling "AcceptChanges" with "0" argument(s): "Database error. 
    MPInfra_p_ManagementPackInstall failed with exception: 
    Failed to validate item: testrule1"
    At line:193 char:1
    + $MP.AcceptChanges()
    + ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ManagementPackException

    Thanks and Regards,

    Shesh

    Thursday, January 21, 2016 5:40 PM

Answers

  • Hi Sir,

    I checked that blog you posted :

    http://blogs.msdn.com/b/rslaten/archive/2013/06/02/creating-performance-collection-rules-in-scom-2012-with-powershell.aspx

    And , I tested it in my lab (SCOM 2012R2 with UR7 installed  ) successfully :

    (I would suggest you use the sample input to test if the script works )

    Also the object properties of that rule :

     

    What is the scom version in your environment ?

    Have you installed any UR ?

    Best Regards,

    Elton


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com .


    • Edited by Elton_Ji Friday, January 22, 2016 11:45 AM
    • Proposed as answer by Elton_Ji Monday, February 15, 2016 10:07 AM
    • Marked as answer by Elton_Ji Tuesday, February 16, 2016 1:19 AM
    Friday, January 22, 2016 11:18 AM

All replies

  • Hi Sir,

    I checked that blog you posted :

    http://blogs.msdn.com/b/rslaten/archive/2013/06/02/creating-performance-collection-rules-in-scom-2012-with-powershell.aspx

    And , I tested it in my lab (SCOM 2012R2 with UR7 installed  ) successfully :

    (I would suggest you use the sample input to test if the script works )

    Also the object properties of that rule :

     

    What is the scom version in your environment ?

    Have you installed any UR ?

    Best Regards,

    Elton


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com .


    • Edited by Elton_Ji Friday, January 22, 2016 11:45 AM
    • Proposed as answer by Elton_Ji Monday, February 15, 2016 10:07 AM
    • Marked as answer by Elton_Ji Tuesday, February 16, 2016 1:19 AM
    Friday, January 22, 2016 11:18 AM
  • Hi Elton,

    Thank you very much for your reply. I'm using scom version 2012 R2. But i am not quite sure

    about the UR. I will update and let you know..

    Regard,s

    Shesha

    Wednesday, February 3, 2016 7:38 AM
  • Hi Shesh,

    do you have already a rule "testrule1" in the MG? Did you tried another Name?

    /patrick


    Please remember to click 'Mark as Answer' on the post that helped you.

    Patrick Seidl (System Center and Private Cloud)
    s2 - seidl solutions
    Blog: http://www.systemcenterrocks.com

    Wednesday, February 3, 2016 7:41 AM
  • Hi Shesha,

    Is there anyupdate ?

    Best Regards,

    Elton


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com .

    Sunday, February 14, 2016 4:50 PM