header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

AddPrinterConnection
Last Post 01 Feb 2008 06:41 PM by marco.shaw. 9 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
KlaasUser is Offline
New Member
New Member
Posts:11
Avatar

--
26 Jan 2008 09:25 AM  

Hi,

I read that I could also write to WMI, so I wanted to try that. In the documentation  and in WBEMTEST I can see a method AddPrinterConnection in the Win32_Printer class, but when I use   "gwmi -class Win32_Printer | gm"    I can't see this method anywhere. Isn't this possible with Powershell?

marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:189
Avatar

--
26 Jan 2008 11:10 AM  
I don't know if there's an easier way to do this... I can't test it right now, so I'm not 100% sure it will work.

PSH>$unc_path_printer="\\myServer\printer1"
PSH>$obj=new-object system.management.managementclass("win32_printer")
PSH>$obj.psbase.invokemethod("addprinterconnection",$unc_path_printer)
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:189
Avatar

--
26 Jan 2008 03:28 PM  
I just realized that this won't help you if you're trying to do this remotely. I'll look into it...
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:189
Avatar

--
26 Jan 2008 03:43 PM  
I don't have the environment currently to test any of this, but maybe you do. I'm not sure how to invoke this remotely... easily anyways. Are you wanting to do this remotely? What do you have on the remote end? You have PowerShell v1? winrm?

PowerShell v2 has a new cmdlet named invoke-wmimethod which may make something like this work:
invoke-wmimethod -computer server -path win32_printer -name create -argumentlist "addprinterconnection","\\myServer\printer1"

Of course, v2 is just currently a preview release, and not intended for production environments.
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
RichSUser is Offline
New Member
New Member
Posts:41
Avatar

--
27 Jan 2008 09:41 AM  

Have you tried using the [WMIClass] accelerator

$p = [WMIClass]"\\.\root\cimv2:Win32_Printer"

$p | get-member

You will see the AddprinerConnection method then

If you want to create something via WMI you have to create an empty instance of the object & then populate it.  Get-WMIObject only really works with existing WMI objects

 

Richard Siddaway
Microsoft MVP - PowerShell
UK PowerShell User group Leader
www.get-psuguk.org.uk
marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:189
Avatar

--
27 Jan 2008 11:49 AM  
Thanks Richard!

Funny, this doesn't work also:
PSH>$gwmi=gwmi win32_printer
PSH>$gwmi|gm -force add*
PSH>

(-force because I have v2)

(I tried psbase, and the others also.)
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
KlaasUser is Offline
New Member
New Member
Posts:11
Avatar

--
31 Jan 2008 11:45 AM  

The trick with the accelerator works. Thank you Richard and Marco!

Locally, there 's no problem: I just did $p.addprinterconnection(\\servername\printername) and the printer was added.

Before adding the printer to a remote computer, or a series of them, I want to know if the printer is already known. Unfortunately, I only get a "Microsoft XPS document writer", allthough on my local comp, the same line returns all printers, local and network:  gwmi -cl win32_printer. On the remote PC's, There are usually 3 to 5 printers installed and yet I can never get them using this gwmi cmd-let.

 

I use Powershell V1.0 and all PC's run Windows XP and the servers WIN2003.

marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:189
Avatar

--
31 Jan 2008 05:24 PM  
I've been playing with this off and on. You can't use an accelerator remotely if you need to have different credentials. Is that a problem?

Strange about getting the limited WMI output... I'll try to think up a test...
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
KlaasUser is Offline
New Member
New Member
Posts:11
Avatar

--
01 Feb 2008 11:22 AM  

I think the credentials are the real problem: I loop through all computers in AD and to do that I pass administrator credentials. On each PC is a different user logged in (or not), so when I ask for the known printers, I get the ones that are installed for administrator, which is nothing since administrator never works at those PC's.

If I would install printers this way, they will be installed for administrator and will be no use for the other users.

Am I right? And if so; is there another way?

marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:189
Avatar

--
01 Feb 2008 06:41 PM  
There must be a way to list all the installed printers from the registry. I looked briefly, but could not find the proper location. I'll keep searching.
Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer