Stand vor der Herausforderung im Loginscript die Etikettengrößen für alle verbundenen Drucker neu zu setzen damit spätere Überraschungen weil Printservice das gerne verstellt/vergisst hintangehalten werden.
$c=@'
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Microsoft.Win32;
public class PrinterSettings
{
[DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = false, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern Int32 GetLastError();
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="DocumentPropertiesA", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
private static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPStr)] string pDeviceName, IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode);
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr PrinterDefaults);
[DllImport("winspool.drv", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int Command);
[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_INFO_9
{
public IntPtr pDevMode;
}
private const short CCDEVICENAME = 32;
private const short CCFORMNAME = 32;
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCFORMNAME)]
public string dmFormName;
public short dmUnusedPadding;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
private const int PRINTER_ACCESS_USE = 0x8;
private const int DM_OUT_BUFFER = 0x2;
private const int DM_MODIFY = 0x8;
private const int DM_PAPERSIZE = 0x2;
public static bool ChangePaperSize(string sPrinterName, short nSize)
{
DEVMODE oDM;
IntPtr pNativeDM = IntPtr.Zero;
int nBytesNeeded = 0;
IntPtr hPrinter = new System.IntPtr();
PRINTER_INFO_9 oPrinterInfo;
IntPtr pNativePrinterInfo = IntPtr.Zero;
IntPtr pDummy=IntPtr.Zero;
if (!OpenPrinter(sPrinterName, out hPrinter, IntPtr.Zero))
{
Console.WriteLine("OpenPrinter() failed with error "+Marshal.GetLastWin32Error()+", aborting.");
return false;
}
nBytesNeeded=DocumentProperties(IntPtr.Zero,hPrinter,sPrinterName,IntPtr.Zero,ref pDummy,0);
pNativeDM=Marshal.AllocHGlobal(nBytesNeeded);
DocumentProperties(IntPtr.Zero,hPrinter,sPrinterName,pNativeDM,ref pDummy,DM_OUT_BUFFER);
oDM = (DEVMODE)Marshal.PtrToStructure(pNativeDM, typeof(DEVMODE));
Marshal.FreeHGlobal(pNativeDM);
oDM.dmPaperSize = nSize;
oDM.dmFields |= DM_PAPERSIZE;
pNativeDM = Marshal.AllocHGlobal(Marshal.SizeOf(oDM));
Marshal.StructureToPtr(oDM, pNativeDM, true);
DocumentProperties(IntPtr.Zero,hPrinter,sPrinterName,pNativeDM,ref pDummy,DM_MODIFY);
oPrinterInfo.pDevMode = pNativeDM;
pNativePrinterInfo = Marshal.AllocHGlobal(Marshal.SizeOf(oPrinterInfo));
Marshal.StructureToPtr(oPrinterInfo, pNativePrinterInfo, true);
if (!SetPrinter(hPrinter, 9, pNativePrinterInfo, 0))
{
Console.WriteLine("SetPrinter() failed with error "+Marshal.GetLastWin32Error()+", aborting.");
Cleanup(pNativeDM, pNativePrinterInfo, hPrinter);
return false;
}
Cleanup(pNativeDM, pNativePrinterInfo, hPrinter);
return true;
}
public static void Cleanup(IntPtr pNativeDM,IntPtr pNativePrinterInfo,IntPtr hPrinter)
{
try
{
Console.WriteLine("Memory and handle cleanup");
if (hPrinter != IntPtr.Zero) ClosePrinter(hPrinter);
if (pNativeDM != IntPtr.Zero) Marshal.FreeHGlobal(pNativeDM);
if (pNativePrinterInfo != IntPtr.Zero) Marshal.FreeHGlobal(pNativePrinterInfo);
}
catch (Exception)
{
}
}
}
'@
Add-Type -TypeDefinition $c -ReferencedAssemblies System.Runtime.InteropServices,System.ComponentModel
Add-Type -AssemblyName System.Drawing
$printer="\\meinprintserver\meinverbundenerdrucker"
$ps=New-Object System.Drawing.Printing.PrinterSettings
$ps.PrinterName=$printer
$size=$ps.PaperSizes|? PaperName -like "*meine Etiketten*"
[PrinterSettings]::ChangePaperSize($printer,$size.RawKind)
Größtenteils von hier geklaut und vereinfacht/bereinigt – mit minimalem Fehlerhandling: https://www.codeproject.com/Articles/6899/Changing-printer-settings-using-C