8

我正在使用 WMI Code Creator 创建代码以添加网络打印机。

http://img13.imageshack.us/img13/9847/wmicodecreatorwin32prin.png

生成的代码效果很好(无论如何在我的域帐户下):

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class CallWMIMethod
    {
        public static void Main()
        {
            try
            {
                ManagementClass classInstance =
                    new ManagementClass("root\\CIMV2",
                    "Win32_Printer", null);

                // Obtain in-parameters for the method
                ManagementBaseObject inParams =
                    classInstance.GetMethodParameters("AddPrinterConnection");

                // Add the input parameters.
                inParams["Name"] =  "\\\\PrintServer\\PrinterName";

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams =
                    classInstance.InvokeMethod("AddPrinterConnection", inParams, null);

                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
            }
        }
    }
}

但是,我需要将联网打印机添加到本地 PC 帐户,即无权访问 \PrintServer 的非域帐户。

我在哪里可以将域用户(服务帐户)的用户名和密码放入上述代码中?

我已经在谷歌上搜索了几个小时,但我只能找到一篇愚蠢的帖子,上面写着如何在远程机器上添加打印机,这不是我想要做的。

(我需要将远程打印机添加到当前PC,而不是远程 PC。)(需要注意的是登录用户是本地 PC 帐户。)

有谁知道如何做到这一点?

4

2 回答 2

1

you could create the same local account on the print server, enabling peer-to-peer authentication by doing so...

i.e. pc1 has user bob1 locally. make bob1 a user on the print server.

run your map program as bob1 on pc1 and you should be able to get to the printer.

does that help?

otherwise, network printers are per user...running your program as the domain user that has access (i.e. runas) wouldn't work since it would simply map the printer to the users session and not the one you actually want.

...what about this? http://www.codescript.co.uk/wmi_connect_as_another_user.htm

...or scriptomatic? http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028 (even tho it is not for c#, it can still give wmi synatx stuff)

于 2011-08-06T04:22:28.043 回答
0

因此,由于删除了其他答案。我刚刚测试了这个并且它有效。这是获取我正在使用的课程的链接https://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User 下面是对我有用的代码。如果你看有一些更好的类实现来模拟用户。

    static void Main(string[] args)
    {

        using (new Impersonator("username", "domainName", "myPassword"))
        {
            // The following code is executed under the impersonated user.
            AddPrinterUnc(@"\\PrintServer\printershare");
        }


    }

    public static void AddPrinterUnc(string printUncPath)
    {

            using (ManagementClass oPrinterClass = new ManagementClass(new ManagementPath("Win32_printer"), null))
            {
                using (ManagementBaseObject oInputParameters = oPrinterClass.GetMethodParameters("AddPrinterConnection"))
                {
                    oInputParameters.SetPropertyValue("Name", printUncPath);

                    oPrinterClass.InvokeMethod("AddPrinterConnection", oInputParameters, null);

                }
            }

    }
于 2017-09-21T17:57:27.183 回答