Missing Microsoft.Management.Infrastructure in packaged service

Service builds but fails to start with following error in Event Manager Win App log:

Exception Info: System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’. The system cannot find the file specified.
File name: ‘Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at System.Reflection.Assembly.get_ExportedTypes()
at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)
at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers) at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)
at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)

This is my test code:

function OnStart() {
‘OnStart’|Out-File ‘D:\TestDir\servicetest.txt’ -Append
$SvcName = (Get-CimInstance Win32_Service -Filter “ProcessId=$PID”).Name
$ServiceJob = Start-Job -Name ‘Background Task’ -ScriptBlock { while($true){“Job Script $(Get-Date)” | Out-File ‘D:\TestDir\servicetest.txt’ -Append; start-sleep -Seconds 60 }}

$null = Register-ObjectEvent -InputObject $ServiceJob -EventName "StateChanged" -MessageData $SvcName -Action {
	if ($Sender.State -ne 'Running') {
	
		Start-Job -Name 'Stop Service' -ArgumentList $Event.MessageData -ScriptBlock {
			param ([string]$SvcName)
			Stop-Service -Name $SVcName -Force
		}
	}
	$Sender | Remove-Job $foreach
	$EventSubscriber | Unregister-Event -Force
	$EventSubscriber.Action | Remove-Job -Force
}

}

function OnStop() {
$ServiceJob = Get-Job -Name ‘Background Task’
if ($ServiceJob.State -eq ‘Running’) {
$ServiceJob | Stop-Job -Force
$ServiceJob | Remove-Job
}
}

$CanStop = $true

Tool: Visual Studio, Visual Studio Code, PSScriptPad, PowerShell Module
Version: 

Can you share your packaging settings? Are you packaging for Windows PowerShell or PowerShell 7?

PowerShell 7.2.0

@{

Root = 'D:\DenovoTools\E1SMMonitor\Tests\E1TestSMMonitor.ps1'

OutputPath = 'D:\DenovoTools\E1SMMonitor\Tests\'

Package = @{

    Enabled = $true

    Obfuscate = $false

    HideConsoleWindow = $true

    DotNetVersion = 'net6.0'

    PowerShellVersion = '7.2.0'

    FileVersion = '0.3.0'

    FileDescription = 'Test'

    ProductName = 'E1TestSMMonitor'

    ServiceName = "E1TestSMMonitor"

    ServiceDisplayName = "E1TestSMMonitor"

    ProductVersion = '0.3.0'

    RequireElevation = $false

    ApplicationIconPath = ''

    PackageType = 'Service'

    RuntimeIdentifier = 'win7-x64'

   

}

Bundle = @{

    Enabled = $true

    Modules = $true

    # IgnoredModules = @()

}

}

1 Like

I have a work around for now…
I created a packaged service with Windows PowerShell that loops thru the PS7 scripts and starts them as jobs.

If anybody needs the code ping me here :slight_smile: