是否可以在 Windows 服务中使用 WMI?

发布于 2024-10-09 15:40:22 字数 555 浏览 4 评论 0原文

我正在编写一个简单的服务来根据我的连接位置设置默认打印机,但我在服务中使用 WMI 时遇到问题。我想得越多,我就越觉得不可能做我想做的事情;因为服务与特定用户无关,但我想无论如何我都会询问它,以防我的怀疑是错误的。

这就是我正在做的事情。

  1. 服务订阅 NetworkChange.NetworkAvailabilityChanged 事件。
  2. 收到事件后,查询 WMI SELECT * FROM Win32_Printer WHERE Network=true
  3. 循环访问 ManagementObjectCollection 并检查每台打印机的状态。
  4. 如果打印机已连接,请调用 ManagementObject.InvokeMethod("SetDefaultPrinter",null)

当我检查安全事件日志时,我注意到失败审核事件“未知的用户名或错误密码”,我假设是因为我没有明确提供该信息。

有没有办法在不指定用户名和密码的情况下执行此操作,或者出于安全目的需要凭据?

I'm writing a simple service to set the default printer based on where I'm connected, but I'm having trouble using WMI in my service. The more I think about it the less I think it's possible to do what I'm trying to do; as a service is not associated with a particular user, but I thought I would ask about it anyway in case my suspicions are wrong.

Here is what I'm doing.

  1. Service subscribes to NetworkChange.NetworkAvailabilityChanged event.
  2. When event is received query WMI SELECT * FROM Win32_Printer WHERE Network=true.
  3. Loop through the ManagementObjectCollection and check the status of each printer.
  4. If the printer is connected call ManagementObject.InvokeMethod("SetDefaultPrinter",null)

When I check the Security event log I noticed Failure Audit events "Unknown username or bad password", which I'm assuming is because I did not explicitly supply that information.

Is there a way to do this without specifying a username and password, or are credentials required for security purposes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

电影里的梦 2024-10-16 15:40:22

该服务必须以某个用户身份运行,例如 NETWORK SERVICE、LOCAL SERVICE 或 SYSTEM。您需要确保给定帐户有权执行您想要的操作。
要更改 Windows 上已部署服务的权限,请转到“开始”>“管理工具>然后您可以更改给定服务的服务帐户。

The service must run as some user, for example NETWORK SERVICE, LOCAL SERVICE or SYSTEM. You need to make sure the given account has permission to do what you want.
To change the permission of a deployed service on Windows go to Start > Administrative Tools > Services then you can change the service account for a given service.

流绪微梦 2024-10-16 15:40:22

我知道这是一个老问题,但我觉得现有的答案并不能完全提供可管理的解决方案。

正如所指出的,要影响用户的默认打印机选择,您需要作为该用户进行身份验证,虽然可以将 Windows 服务设置为以特定用户身份登录,但更好的解决方案是...

使用隐藏的创建 winforms 程序表单并在用户使用任务计划程序登录时启动它。

这样,PC 就可以被许多用户使用并从应用程序中受益,而无需更改服务登录设置。

我建议使用 winforms 应用程序,因为它的入口栏较低,您可以使用从 UI 角度来看不可见的任何其他类型的项目类型。

I know this is an old question but i feel the existing answer doesn't quite provide a manageable solution.

As pointed out to effect a users default printer selection you need to be authenticated as that user and whilst it is possible to set a windows service to logon as a specific user a better solution would be to ...

Create a winforms program using a hidden form and launch it when the user logs on using Task Scheduler.

This way the PC can be used by many users and benefit from the application without having to change the service logon settings.

I suggested a winforms application as it has a low entry bar, you can use any other kind of invisible from a UI perspective project type.

念三年u 2024-10-16 15:40:22

你应该使用 linqtowmi ,它效果很好!

you should use linqtowmi for this it works great!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文