# -*- coding: utf-8 -*-

# setup-printer --client=10.200.7.60 --domain four.three --user Administrator --password univention --printername printer2  --server 10.200.7.51 --credssp

name = 'setup-printer'
description = 'setup network printer'
args = dict(
	printername=dict(help='name of the printer', required=True),
	server=dict(help='host server', required=True),
)


def pre(winrm):
	# logon as user, seems to be the only way to apply user gpo's
	winrm.args.username = winrm.args.run_as_user
	winrm.args.userpwd = winrm.args.run_as_password
	winrm.args.command = [
		"rundll32 printui.dll,PrintUIEntry /ia /c\\\\" + winrm.args.server + ' /m`"MS Publisher Color Printer`"',
		"rundll32 printui.dll,PrintUIEntry /Xs /n\\\\" + winrm.args.server + '\\' + winrm.args.printername + ' DriverName `"MS Publisher Color Printer`"',
		"rundll32 printui.dll,PrintUIEntry /ga /n\\\\" + winrm.args.server + '\\' + winrm.args.printername,
	]
	winrm.plugins.logon_as()
	return

# cmd
#
# rundll32 printui.dll,PrintUIEntry /in /n\\server\printer

# powershell
# more powershell stuff that currently did not work
#
# Add-PrinterDriver -Name "MS Publisher Color Printer"
# Add-Printerport -PrinterHostAddress "%(server)s" -Name "%(server)s"
# Add-Printer -PortName "\\\\%(server)s\\%(printername)s" -Name "%(printername)s" -DriverName "MS Publisher Color Printer"
# $PrinterPath = "\\\\%(server)s\\%(printername)s"
# Set-printer -ComputerName ucs-samba -Name printer1 -DriverName "MS Publisher Color Printer"
# Add-Printer -ConnectionName "printer1"
# get-printer
# get-printerport

# wmi
#
# $PrinterPath = "\\slave\printer2"
# $net = new-Object -Com WScript.Network
# $net.AddWindowsPrinterConnection($PrinterPath)
# Get-WMIObject Win32_Printer'


# via gpo
#
# dn: CN=PushedPrinterConnections,CN=User,CN={31B2F340-016D-11D2-945F-00C04FB984F9},CN=Policies,CN=System,DC=four,DC=three
# objectClass: top
# objectClass: container
# cn: PushedPrinterConnections
# name: PushedPrinterConnections
# objectCategory: CN=Container,CN=Schema,CN=Configuration,DC=four,DC=three
#
# dn: CN={BB193150-5F40-460D-A2E1-165775DE3A28},CN=PushedPrinterConnections,CN=User,CN={31B2F340-016D-11D2-945F-00C04FB984F9},CN=Policies,CN=System,DC=four,DC=three
# objectClass: top
# objectClass: msPrint-ConnectionPolicy
# cn: {BB193150-5F40-460D-A2E1-165775DE3A28}
# name: {BB193150-5F40-460D-A2E1-165775DE3A28}
# uNCName: \\SLAVE\printer2
# serverName: \\SLAVE
# printAttributes: 0
# printerName: printer2
# objectCategory: CN=ms-Print-ConnectionPolicy,CN=Schema,CN=Configuration,DC=four,DC=three
