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, Dictionary
2& aliases, Dictionary2& providers) at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary
2& cmdlets, Dictionary2& aliases, Dictionary
2& 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: