“PowerShell Script failed to run” alert in OpsMgr 2012

Don’t you hate it when you’ve just released a piece of work to public and you found an issue with it? Well, this is what happened to me today. Yesterday and released the OpsMgr Self Maintenance MP, and today, I found an issue with the 2012 version.

After the MP is imported and the “OpsMgr 2012 Self Maintenance Operational Database LocalizedText Table Health Monitor” has been enabled via an override, you’ll soon get this alert from one of the management servers:

The PowerShell script failed with below exception

System.Management.Automation.IncompleteParseException: White space is not allowed before the string terminator.
at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
at Microsoft.EnterpriseManagement.Common.PowerShell.RunspaceController.RunScript[T](String scriptName, String scriptBody, Dictionary`2 parameters, Object[] constructorArgs, IScriptDebug iScriptDebug, Boolean bSerializeOutput)

image

After spent sometime troubleshooting, the issue is a bit stupid, but I’ll remember this in the future:

I wrote the 2007 version using 2007 R2 authoring console, thus no problems found. The 2012 version was written using Visual Studio Authoring Extension. In the particular probe action module used by the monitor type, this is what the raw code looks like:

image

when I pasted the script in the xml, I tried to format it using the Tab key.

This is what it should look like:

image

After I removed all the Tab spaces, the monitor started working.

I’ve changed the version number of the 2012 version to 1.0.0.1. I’ll update the MP from the original article from yesterday. For those who has already downloaded the MP, apologies. please download it again and the mp can be in-place upgraded to the new version.

4 comments

  1. The tabs and white space aren’t necessarily a problem. As I suspected, you use a block string (a “here-string”) starting at line# 1364 and in a few other places . The closing characters of the block
    “@
    must be the first two characters on the final line of the quote. This is true regardless of how the rest of your code is formatted. This was most likely the cause of the error.

    Also, thanks for continuing to produce great stuff!.
    ~Tyson

  2. Thanks Tyson. I noticed this was the only module where I had tab space in front of each line of script. I did use the multi-line string variable (@” and “@) for the SQL query in this script. I’ll test it out next time when I use VSAE. Thanks for your advice.

  3. Hi again
    I just noticed (SCOM 2012 RTM running your MP 1.0.0.1) today that I am getting a lot of 22400 errors in the System log on the RMS-E, for various scripts. e.g.

    Failed to run the PowerShell script due to exception below, this workflow will be unloaded.

    System.TypeInitializationException: The type initializer for ‘System.Management.Automation.Runspaces.InitialSessionState’ threw an exception. —> System.Management.Automation.RuntimeException: Cannot find the type for custom attribute ‘CmdletBinding’. Make sure that the assembly that contains this type is loaded.
    at System.Management.Automation.AttributeNode.GetCustomAttributeObject()
    at System.Management.Automation.AttributeNode.GetCustomAttribute()
    at System.Management.Automation.ScriptBlock.InitializeAttributesAndParameters()
    at System.Management.Automation.ScriptBlock.get_CmdletBindingAttribute()
    at System.Management.Automation.CommandMetadata..ctor(ScriptBlock scriptblock, String commandName, ExecutionContext context)
    at System.Management.Automation.Runspaces.SessionStateFunctionEntry..ctor(String name, String definition)
    at System.Management.Automation.Runspaces.InitialSessionState..cctor()
    — End of inner exception stack trace —
    at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
    at Microsoft.EnterpriseManagement.Common.PowerShell.RunspaceConfigurationContainer.GetRunspaceConfiguration(InitialSessionStateEntryCollection1 variableEntries)
    at Microsoft.EnterpriseManagement.Common.PowerShell.RunspaceController.Initialize(RunspaceConfigurationContainer configuration, InitialSessionStateEntryCollection
    1 variableEntries)
    at Microsoft.EnterpriseManagement.Common.PowerShell.RunspaceManager.DeliverRunspaceThreadProc(Object appDomainObject)

    Script Name: SDKConnectionProbe.ps1

    One or more workflows were affected by this.

    Workflow name: OpsMgr.2012.Self.Maintenance.Collect.All.Management.Server.SDK.Connection.Count.Rule

    I can see similar errors for:
    LocalizedTextTable-HealthProbe.ps1
    MgmtServerInMaintModeProbe.ps1

    I am starting to worry a bit now, as Partition and Grooming has started failing now too (coincidentally?), so hope there is a simple fix for this?!

    Cheers

    Steve

    1. I wonder if you might get any more useful information if you pulled out the script at the bottom of the workflow (“SDKConnectionProbe.ps1″ in ProbeActionModuleType ID=”OpsMgr.2012.Self.Maintenance.All.Management.Server.SDK.Connection.Count.Probe”) and ran it manually on the RMS-E. Maybe add a few Write-Host commands throughout the script to show values/results as the script is running. You might get more helpful/specific errors if you run it manually. Or maybe just add this command at the top of the script:

      New-Variable -Name StartupVariables -Force -Value (Get-Variable -Scope Global | Select -ExpandProperty Name)

      Then add this command at the bottom of the script to puke out all of the custom variables in the session:

      Get-Variable | Where-Object { $startupVariables -notcontains $_.Name -and ($_.Name -ne “StartupVariables” ) } | % { $_ }

      To unseal a management pack, see here:
      http://blogs.msdn.com/b/boris_yanushpolsky/archive/2007/08/16/unsealing-a-management-pack.aspx

Leave a Reply

%d bloggers like this: