Link to home
Start Free TrialLog in
Avatar of samelamin
samelamin

asked on

using WMI commands to add a printer

Dear All

I would like to add a printer using a WMI command

any ideas

so far i can get a list of drivers using the code below but how do i add a printer?


thye string chosen is the chosen printer i want to install copies of. so forexample if the chosen is an HP printer i want to install copies of this HP
string path="not working";

            chosen = comboBox1.SelectedItem.ToString();

            ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PrinterDriver");
            ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(objectQuery);

            foreach (ManagementObject mo in searcher.Get())
            {
                if (chosen == mo["Name"].ToString())
                    path = mo["DriverPath"].ToString();

            }

Open in new window

Avatar of mattclarified
mattclarified
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

The following code should be what you are looking for to add a printer, replace the printer name in the strcomputer string and the \\PrintServer1\ArtDepartmentPrinter for your environment

M@
strComputer = "atl-ws-01"
Set objWMIService = GetObject( _
    "winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2")
Set objPrinter = objWMIService.Get("Win32_Printer")
errReturn = objPrinter.AddPrinterConnection _
    ("\\PrintServer1\ArtDepartmentPrinter")

Open in new window

Avatar of samelamin
samelamin

ASKER

Is this in c# though

because if i do that i get multiple errors
Are you trying to script it in C#? If not what are you using?

M@
yes
so far i am trying to make it work using this program but i keep getting error
string printerName, printerDriver, portName; bool sharedPrinter;
            string path="not working";
            string infname = "";
            portName = null;

            chosen = comboBox1.SelectedItem.ToString();

            ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PrinterDriver WHERE NAME='"+chosen+"'");
            ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(objectQuery);

            foreach (ManagementObject mo in searcher.Get())
           
                {
                    infname = mo["InfName"].ToString();
                }

            MessageBox.Show(infname);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of samelamin
samelamin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial