KirkMunro / ScsmPx

System Center Service Manager PowerShell Extensions
http://kirkmunro.github.io/ScsmPx/
Apache License 2.0
30 stars 5 forks source link

Issue loading ScsmPx with Service Management Automation Runbook (SMA) #2

Open flopzoid opened 9 years ago

flopzoid commented 9 years ago

Very simple runbook that attempts to import the ScsmPx module like this: Import-Module -Name ScsmPx -Force -ErrorAction SilentlyContinue

Results in the error below. Have you seen anything like this? Running the same command in powershell workflow works just fine.

3/11/2015 9:52:37 AM, Error: Update-FormatData : Updating format data is not allowed in this runspace. The 'DisableFormatUpdates' property is set to True when creating the runspace. At AAASCSMTest:1 char:1

3/11/2015 9:52:38 AM, Error: Write-Host : A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows. At AAASCSMTest:1 char:1

3/11/2015 9:52:38 AM, Error: Get-ScsmPxObject : The term 'Get-ScsmPxObject' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At AAASCSMTest:1 char:1

KirkMunro commented 9 years ago

Thanks for reporting this error. I'm investigating what needs to be done to resolve it, and will follow-up shortly.

KirkMunro commented 9 years ago

Hi again,

So, I've dug into this issue to see what is the cause. There are several things in play with this error.

From what I can tell, the script that is used to initialize the native SCSM environment is failing. When that script fails, the script module has a block of code that tries to output the error message. That shouldn't be necessary, but it's an attempt to work around a bug in PowerShell. I use Write-Host for the error message because I'm trying to identify clearly where the error comes from, and Write-Error wasn't doing that for me. But there may be another way. Anyhow, this error handling works fine when you import that module in PowerShell, but the runbook doesn't like it because Write-Host is not supported there, so I'll have to change it. That's on me.

What I can't tell yet from your error message though is why the module failed to load (i.e. why the Initialize-NativeScsmEnvironment.ps1 script is getting an error message). Can you try your runbook again, this time only calling Import-Module to import the ScsmPx module, but with the -Verbose switch added so that I can perhaps identify what the root cause is?

Also, on the system where your runbook ran (the Runbook agent), can you try importing the ScsmPx module there and make sure that it imports successfully? It is dependent on the SCSM Management Console being installed (only because that's the only supported way to get the native SCSM modules installed on the system).

Lastly, can you share what you would like to do with ScsmPx from inside an SMA runbook? Knowing what sort of tasks you would like to accomplish would be very helpful to me.

Thanks!

flopzoid commented 9 years ago

Thanks for the quick response! The SCSM admin console is loaded on the SMA machine. Put together a quick script to repro like you asked.

Here is the script: workflow AASCSMPX { inlinescript { Import-module scsmpx -verbose

}

}

Here is the output: 3/13/2015 11:38:29 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\scsmpx\scsmpx.psd1'. 3/13/2015 11:38:29 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\scsmpx\scripts\Initialize-NativeScsmEnvironment.ps1'. 3/13/2015 11:38:29 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Dot-sourcing the script file 'C:\Program Files\WindowsPowerShell\Modules\scsmpx\scripts\Initialize-NativeScsmEnvironment.ps1'. 3/13/2015 11:38:30 AM, Error: Update-FormatData : Updating format data is not allowed in this runspace. The 'DisableFormatUpdates' property is set to True when creating the runspace. At AASCSMPX:1 char:1

3/13/2015 11:38:30 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\SnippetPx\SnippetPx.psd1'. 3/13/2015 11:38:30 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\SnippetPx\SnippetPx.dll'. 3/13/2015 11:38:30 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Exporting cmdlet 'Get-Snippet'. 3/13/2015 11:38:30 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Exporting cmdlet 'Invoke-Snippet'. 3/13/2015 11:38:30 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Importing cmdlet 'Get-Snippet'. 3/13/2015 11:38:30 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Importing cmdlet 'Invoke-Snippet'. 3/13/2015 11:38:30 AM, Verbose: b6bb43df-1e8d-4e00-9ee0-643692b3dfed:[localhost]:Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\scsmpx\ScsmPx.psm1'. 3/13/2015 11:38:30 AM, Error: Write-Host : A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows. At AASCSMPX:1 char:1

3/13/2015 11:38:30 AM, Error: Initialize-NativeScsmEnvironment : Could not load file or assembly '.\System.Center.Service.Manager.psm1' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) At AASCSMPX:1 char:1

3/13/2015 11:38:30 AM, Error: Import-module : The module to process 'ScsmPx.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest 'C:\Program Files\WindowsPowerShell\Modules\scsmpx\scsmpx.psd1' was not processed because no valid module was found in any module directory. At AASCSMPX:1 char:1

flopzoid commented 9 years ago

additional data. Here are the results when I import the Scsmpx module outside of SMA:

Import-Module scsmpx -Verbose VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\ScsmPx\ScsmPx.psd1'. VERBOSE: Importing function 'Add-ScsmPxFileAttachment'. VERBOSE: Importing function 'Add-ScsmPxTroubleTicketComment'. VERBOSE: Importing function 'Get-ScsmPxAdGroup'. VERBOSE: Importing function 'Get-ScsmPxAdPrinter'. VERBOSE: Importing function 'Get-ScsmPxAdUser'. VERBOSE: Importing function 'Get-ScsmPxBuild'. VERBOSE: Importing function 'Get-ScsmPxBusinessService'. VERBOSE: Importing function 'Get-ScsmPxChangeRequest'. VERBOSE: Importing function 'Get-ScsmPxCommand'. VERBOSE: Importing function 'Get-ScsmPxConfigItem'. VERBOSE: Importing function 'Get-ScsmPxConnectedUser'. VERBOSE: Importing function 'Get-ScsmPxDependentActivity'. VERBOSE: Importing function 'Get-ScsmPxDwCube'. VERBOSE: Importing function 'Get-ScsmPxDwDataSource'. VERBOSE: Importing function 'Get-ScsmPxDwName'. VERBOSE: Importing function 'Get-ScsmPxEnterpriseManagementGroup'. VERBOSE: Importing function 'Get-ScsmPxEnvironment'. VERBOSE: Importing function 'Get-ScsmPxFileAttachment'. VERBOSE: Importing function 'Get-ScsmPxIncident'. VERBOSE: Importing function 'Get-ScsmPxInstallDirectory'. VERBOSE: Importing function 'Get-ScsmPxKnowledgeArticle'. VERBOSE: Importing function 'Get-ScsmPxList'. VERBOSE: Importing function 'Get-ScsmPxListItem'. VERBOSE: Importing function 'Get-ScsmPxManagementServer'. VERBOSE: Importing function 'Get-ScsmPxManualActivity'. VERBOSE: Importing function 'Get-ScsmPxObject'. VERBOSE: Importing function 'Get-ScsmPxObjectHistory'. VERBOSE: Importing function 'Get-ScsmPxParallelActivity'. VERBOSE: Importing function 'Get-ScsmPxPrimaryManagementServer'. VERBOSE: Importing function 'Get-ScsmPxProblem'. VERBOSE: Importing function 'Get-ScsmPxRelatedObject'. VERBOSE: Importing function 'Get-ScsmPxReleaseRecord'. VERBOSE: Importing function 'Get-ScsmPxRequestOffering'. VERBOSE: Importing function 'Get-ScsmPxReviewActivity'. VERBOSE: Importing function 'Get-ScsmPxRunbook'. VERBOSE: Importing function 'Get-ScsmPxRunbookActivity'. VERBOSE: Importing function 'Get-ScsmPxSequentialActivity'. VERBOSE: Importing function 'Get-ScsmPxServiceOffering'. VERBOSE: Importing function 'Get-ScsmPxServiceRequest'. VERBOSE: Importing function 'Get-ScsmPxSoftwareItem'. VERBOSE: Importing function 'Get-ScsmPxSoftwareUpdate'. VERBOSE: Importing function 'Get-ScsmPxUserOrGroup'. VERBOSE: Importing function 'Get-ScsmPxViewData'. VERBOSE: Importing function 'Get-ScsmPxWindowsComputer'. VERBOSE: Importing function 'New-ScsmPxManagementPackBundle'. VERBOSE: Importing function 'New-ScsmPxObject'. VERBOSE: Importing function 'New-ScsmPxObjectSearchCriteria'. VERBOSE: Importing function 'New-ScsmPxProxyFunctionDefinition'. VERBOSE: Importing function 'Remove-ScsmPxAdGroup'. VERBOSE: Importing function 'Remove-ScsmPxAdPrinter'. VERBOSE: Importing function 'Remove-ScsmPxAdUser'. VERBOSE: Importing function 'Remove-ScsmPxBuild'. VERBOSE: Importing function 'Remove-ScsmPxBusinessService'. VERBOSE: Importing function 'Remove-ScsmPxChangeRequest'. VERBOSE: Importing function 'Remove-ScsmPxConfigItem'. VERBOSE: Importing function 'Remove-ScsmPxDependentActivity'. VERBOSE: Importing function 'Remove-ScsmPxDwCube'. VERBOSE: Importing function 'Remove-ScsmPxDwDataSource'. VERBOSE: Importing function 'Remove-ScsmPxEnvironment'. VERBOSE: Importing function 'Remove-ScsmPxFileAttachment'. VERBOSE: Importing function 'Remove-ScsmPxIncident'. VERBOSE: Importing function 'Remove-ScsmPxKnowledgeArticle'. VERBOSE: Importing function 'Remove-ScsmPxManagementServer'. VERBOSE: Importing function 'Remove-ScsmPxManualActivity'. VERBOSE: Importing function 'Remove-ScsmPxObject'. VERBOSE: Importing function 'Remove-ScsmPxParallelActivity'. VERBOSE: Importing function 'Remove-ScsmPxProblem'. VERBOSE: Importing function 'Remove-ScsmPxReleaseRecord'. VERBOSE: Importing function 'Remove-ScsmPxRequestOffering'. VERBOSE: Importing function 'Remove-ScsmPxReviewActivity'. VERBOSE: Importing function 'Remove-ScsmPxRunbook'. VERBOSE: Importing function 'Remove-ScsmPxRunbookActivity'. VERBOSE: Importing function 'Remove-ScsmPxSequentialActivity'. VERBOSE: Importing function 'Remove-ScsmPxServiceOffering'. VERBOSE: Importing function 'Remove-ScsmPxServiceRequest'. VERBOSE: Importing function 'Remove-ScsmPxSoftwareItem'. VERBOSE: Importing function 'Remove-ScsmPxSoftwareUpdate'. VERBOSE: Importing function 'Remove-ScsmPxUserOrGroup'. VERBOSE: Importing function 'Remove-ScsmPxWindowsComputer'. VERBOSE: Importing function 'Rename-ScsmPxAdGroup'. VERBOSE: Importing function 'Rename-ScsmPxAdPrinter'. VERBOSE: Importing function 'Rename-ScsmPxAdUser'. VERBOSE: Importing function 'Rename-ScsmPxBuild'. VERBOSE: Importing function 'Rename-ScsmPxBusinessService'. VERBOSE: Importing function 'Rename-ScsmPxChangeRequest'. VERBOSE: Importing function 'Rename-ScsmPxConfigItem'. VERBOSE: Importing function 'Rename-ScsmPxDependentActivity'. VERBOSE: Importing function 'Rename-ScsmPxDwCube'. VERBOSE: Importing function 'Rename-ScsmPxDwDataSource'. VERBOSE: Importing function 'Rename-ScsmPxEnvironment'. VERBOSE: Importing function 'Rename-ScsmPxFileAttachment'. VERBOSE: Importing function 'Rename-ScsmPxIncident'. VERBOSE: Importing function 'Rename-ScsmPxKnowledgeArticle'. VERBOSE: Importing function 'Rename-ScsmPxManagementServer'. VERBOSE: Importing function 'Rename-ScsmPxManualActivity'. VERBOSE: Importing function 'Rename-ScsmPxObject'. VERBOSE: Importing function 'Rename-ScsmPxParallelActivity'. VERBOSE: Importing function 'Rename-ScsmPxProblem'. VERBOSE: Importing function 'Rename-ScsmPxReleaseRecord'. VERBOSE: Importing function 'Rename-ScsmPxRequestOffering'. VERBOSE: Importing function 'Rename-ScsmPxReviewActivity'. VERBOSE: Importing function 'Rename-ScsmPxRunbook'. VERBOSE: Importing function 'Rename-ScsmPxRunbookActivity'. VERBOSE: Importing function 'Rename-ScsmPxSequentialActivity'. VERBOSE: Importing function 'Rename-ScsmPxServiceOffering'. VERBOSE: Importing function 'Rename-ScsmPxServiceRequest'. VERBOSE: Importing function 'Rename-ScsmPxSoftwareItem'. VERBOSE: Importing function 'Rename-ScsmPxSoftwareUpdate'. VERBOSE: Importing function 'Rename-ScsmPxUserOrGroup'. VERBOSE: Importing function 'Rename-ScsmPxWindowsComputer'. VERBOSE: Importing function 'Reset-ScsmPxCommandCache'. VERBOSE: Importing function 'Restore-ScsmPxAdGroup'. VERBOSE: Importing function 'Restore-ScsmPxAdPrinter'. VERBOSE: Importing function 'Restore-ScsmPxAdUser'. VERBOSE: Importing function 'Restore-ScsmPxBuild'. VERBOSE: Importing function 'Restore-ScsmPxBusinessService'. VERBOSE: Importing function 'Restore-ScsmPxConfigItem'. VERBOSE: Importing function 'Restore-ScsmPxEnvironment'. VERBOSE: Importing function 'Restore-ScsmPxKnowledgeArticle'. VERBOSE: Importing function 'Restore-ScsmPxManagementServer'. VERBOSE: Importing function 'Restore-ScsmPxObject'. VERBOSE: Importing function 'Restore-ScsmPxServiceRequest'. VERBOSE: Importing function 'Restore-ScsmPxSoftwareItem'. VERBOSE: Importing function 'Restore-ScsmPxSoftwareUpdate'. VERBOSE: Importing function 'Restore-ScsmPxUserOrGroup'. VERBOSE: Importing function 'Restore-ScsmPxWindowsComputer'. VERBOSE: Importing function 'Set-ScsmPxAdGroup'. VERBOSE: Importing function 'Set-ScsmPxAdPrinter'. VERBOSE: Importing function 'Set-ScsmPxAdUser'. VERBOSE: Importing function 'Set-ScsmPxBuild'. VERBOSE: Importing function 'Set-ScsmPxBusinessService'. VERBOSE: Importing function 'Set-ScsmPxChangeRequest'. VERBOSE: Importing function 'Set-ScsmPxConfigItem'. VERBOSE: Importing function 'Set-ScsmPxDependentActivity'. VERBOSE: Importing function 'Set-ScsmPxDwCube'. VERBOSE: Importing function 'Set-ScsmPxDwDataSource'. VERBOSE: Importing function 'Set-ScsmPxEnvironment'. VERBOSE: Importing function 'Set-ScsmPxFileAttachment'. VERBOSE: Importing function 'Set-ScsmPxIncident'. VERBOSE: Importing function 'Set-ScsmPxKnowledgeArticle'. VERBOSE: Importing function 'Set-ScsmPxManagementServer'. VERBOSE: Importing function 'Set-ScsmPxManualActivity'. VERBOSE: Importing function 'Set-ScsmPxObject'. VERBOSE: Importing function 'Set-ScsmPxParallelActivity'. VERBOSE: Importing function 'Set-ScsmPxProblem'. VERBOSE: Importing function 'Set-ScsmPxReleaseRecord'. VERBOSE: Importing function 'Set-ScsmPxRequestOffering'. VERBOSE: Importing function 'Set-ScsmPxReviewActivity'. VERBOSE: Importing function 'Set-ScsmPxRunbook'. VERBOSE: Importing function 'Set-ScsmPxRunbookActivity'. VERBOSE: Importing function 'Set-ScsmPxSequentialActivity'. VERBOSE: Importing function 'Set-ScsmPxServiceOffering'. VERBOSE: Importing function 'Set-ScsmPxServiceRequest'. VERBOSE: Importing function 'Set-ScsmPxSoftwareItem'. VERBOSE: Importing function 'Set-ScsmPxSoftwareUpdate'. VERBOSE: Importing function 'Set-ScsmPxUserOrGroup'. VERBOSE: Importing function 'Set-ScsmPxWindowsComputer'.

KirkMunro commented 9 years ago

I'm trying to figure out where that call to Update-FormatData is coming from. I don't call it myself. It might be in the SCSM native modules. Can you confirm what version of ScsmPx you have installed, and if you don't have the latest, can you upgrade and then try again and see if you get the same results? I'll do more testing today with my own SMA environment as well to get to the bottom of this (couldn't yesterday, not enough resources to open that VM).

KirkMunro commented 8 years ago

Just circling back on this old thread in case others come across this problem. It turns out that the native SCSM modules are much more funky that I originally realized, and that they way that they are currently designed is incompatible with SMA. That doesn't mean that can't be fixed though, and I was able to make a series of benign changes on my local system to address the design issues that prevent the native SCSM modules from loading in SMA. I have these documented locally, and I will figure out next steps (likely pushing the issues onto UserVoice for the SCSM team to address the problems in the product, plus also possibly adding a command to ScsmPx that fixes the design problems in recent releases by automatically making the appropriate changes).

qb411 commented 7 years ago

Hi there Kirk,

Could you comment on how you got around the SMA issue? I'm experiencing the same problem with a NTFSSecurity module from Technet. https://github.com/raandree/NTFSSecurity is the project. I'm reaching out to them as well to let them know of the issue. However it also encounters the same problem with DisableFormatUpdates being erroring out.

KirkMunro commented 7 years ago

That's a tough one @qb411. The challenge with the NTFSSecurity module is their NTFSSecurity.init.ps1 script file that is invoked as a startup script for the module when the module is imported. That file invokes Update-FormatData to load the format.ps1xml file so that they can get their formatting defaults to apply before the native formatting for the same types (the FormatsToProcess attribute in a module manifest does not support prepending the format file in the format list -- it should, but they just never implemented that support in a manifest).

That said, from a quick analysis of the module I cannot identify why they need to prepend their types. I don't see a conflict between the type names used in their format.ps1xml file and the type names used in native format.ps1xml files. I also don't see why they are using a startup script at all (and to be honest, I would recommend they don't -- startup scripts are not very reliable in my experience).

I don't have time to try this, but if I were inclined to try to change the way this is currently implemented I would start by moving the logic they have in the startup script into the psm1 file for the module, removing the Update-FormatData invocation, and adding the format.ps1xml file to the FormatsToProcess entry in the manifest. I would also probably remove the Add-Type calls and replace them with entries in the NestedModules entry in the manifest. That's just some off the cuff ideas that would be worth looking into.

Let me know if you work it out with them, and if you have other questions please don't hesitate to ask.

qb411 commented 7 years ago

Thanks very much for the ideas Kirk. I figured you might answer that we look into refactoring the module. I'm going to spend some personal time on that as well because I'd like to try and contribute to the fix if possible. However for the current situation I need to get it functional within the runspace on our SMA server. I'm looking at trying to set the runspace to change the "defaultformatupdates" in SMA's powershell session to "False".

I'm not very skilled with c# and i'd say I'm still at beginner level powershell scripting with modules. So I'd rather get the environment functional while I continue to work on the module afterwards.

I'm employing the following call to try and set the session variable:

I'm able to retrieve the session settings, but not set the boolean that its looking for yet.


Update:

The following placed in the inlinescript portion of the workflow in the SMA Runbook will allow you to disable the session boolean, and then reenable it. This allows you to load the module. Its a complete hack/workaround for the functionality, but this gives us the breathing room to look into the refactor question further.

[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.InitialSessionState.DisableFormatUpdates = $false
            Import-Module NTFSSecurity -ErrorAction SilentlyContinue
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.InitialSessionState.DisableFormatUpdates = $true 

Want to say thank you so much to Kirk. You got me looking into the possibilities on how to force the load of the format data.

@AspenForester - Its important to note that this issue does not occur except within the runspace of SMA. If you are just loading modules in standard powershell you won't have this issue. Its a specific problem to SMA, as they restrict the format data differently in their runspaces than does Powershell. https://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.initialsessionstate(v=vs.85).aspx This has the details on the flag.

AspenForester commented 7 years ago

I'm really interested in this, not because of SCSMPx or SMA, but because I use NTFSSecurity almost daily. I'll see what I can do to take a look at the init script, the PSM1, and the call to Update-FormatData tomorrow.

KirkMunro commented 7 years ago

@qb411 Glad you worked out a workaround. That will be handy for anyone facing this problem with any module. Even SMA aside though, the module could use some cleanup. @AspenForester I saw you forked it already. If you need some assistance let me know. I think the changes required would be pretty straightforward. Also I re-read what I said yesterday and for the dlls that just define .NET classes and not cmdlets, it should be possible to move them into the RequiredAssemblies attribute. The manifest should have its AliasesToExport value replaced with the actual list of aliases that are exported (best practice for performance reasons).