我在Windows服务项目中使用的是System.Management.Automation.dll的PowerShell类。这是PowerShell代码:
PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-AppxPackage -Name xxx");
Collection<PSObject> results = ps.Invoke();
foreach (PSObject item in results)
{
if (item != null)
{
//print the result into a log file
}
}
...some exception catching code
我将PowerShell代码放在我的Windows Service OnStart方法中,然后启动该服务,但日志文件中没有打印任何内容,也没有异常。然后我把代码放在服务的main()方法中,它运行得很好。然后我将代码放在一个新的Windows窗体项目中,它也很完美。服务中发生了一些事情,我不知道它是什么。
“Get-AppxPackage”命令在服务中不可用吗?或者我在创建服务时遗漏了什么?请帮忙。感谢。