Page 1 of 1

PrinterInfo with Powershell

Posted: Sun Dec 29, 2024 1:13 am
by Otto
Best regards,
Otto

Code: Select all | Expand


 #include "fivewin.ch"

function main
     CreateAndRunPrinterScript() 
     CreateAndRunPrinterInfoScript()
return nil

//----------------------------------------------------------------------------//
FUNCTION CreateAndRunPrinterScript()
   LOCAL cScript, cOutputFile := ".\psfiles\get_printers.ps1"
   LOCAL cResultFile := ".\psfiles\printers_output.txt"
    LOCAL aPrinters := ""
    
   // PowerShell-Befehle als Text zusammenstellen
   cScript := '$printers = Get-ChildItem -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Print\Printers"' + CRLF
   cScript += '$output = @()' + CRLF
   cScript += 'foreach ($printer in $printers) {' + CRLF
   cScript += '    $printerName = $printer.PSChildName' + CRLF
   cScript += '    $output += $printerName' + CRLF
   cScript += '}' + CRLF
   cScript += '$output | Out-File -FilePath "' + cResultFile + '" -Encoding utf8' + CRLF

   // PowerShell-Skript in eine Datei schreiben
   MEMOWRIT(cOutputFile, cScript)

   // PowerShell-Skript ausführen
   WAITRUN("powershell.exe -ExecutionPolicy Bypass -File " + cOutputFile)

   // Ergebnis anzeigen
   IF FILE(cResultFile)
      aPrinters := MEMOREAD(cResultFile)
    msgInfo( aPrinters )
    
   ELSE
      ? "Keine Ausgabe gefunden."
   ENDIF

   RETURN NIL
   
   
   
   
   FUNCTION CreateAndRunPrinterInfoScript()
   LOCAL cScript, cOutputFile := ".\psfiles\get_printer_info.ps1"
   LOCAL cResultFile := ".\psfiles\printer_info_output.csv"

   // PowerShell-Skript zusammenstellen
   cScript := 'Get-Printer | Select-Object Name, DriverName, PortName, ShareName, PrinterStatus, Default | ' + ;
              'Export-Csv -Path "' + cResultFile + '" -NoTypeInformation -Encoding UTF8' + CRLF

   // PowerShell-Skript in eine Datei schreiben
   MEMOWRIT(cOutputFile, cScript)

   // PowerShell-Skript ausführen
   WAITRUN("powershell.exe -ExecutionPolicy Bypass -File " + cOutputFile)

   // Ergebnis anzeigen
   IF FILE(cResultFile)
     msginfo( MEMOREAD(cResultFile) )
     
   ELSE
      ? "Keine Druckerinformationen gefunden."
   ENDIF

   RETURN NIL

   

 

Re: PrinterInfo with Powershell

Posted: Sun Dec 29, 2024 7:26 am
by Antonio Linares
Dear Otto,

Many thanks for sharing it :-)

Re: PrinterInfo with Powershell

Posted: Mon Dec 30, 2024 3:23 pm
by karinha
Mister Otto,explain, pls:

Code: Select all | Expand

   LOCAL cScript, cOutputFile := ".\psfiles\get_printers.ps1"
   LOCAL cResultFile := ".\psfiles\printers_output.txt"

   LOCAL cScript, cOutputFile := ".\psfiles\get_printer_info.ps1"
   LOCAL cResultFile := ".\psfiles\printer_info_output.csv"
 
???????????

Regards, saludos.

Re: PrinterInfo with Powershell

Posted: Mon Dec 30, 2024 4:13 pm
by Otto
Hello João,
I use ps1 files for many tasks, especially for installing servers, and have automated many tasks with them.
You need to create the directory. You could use c:\temp, for example.
Best regards,
Otto

c:\fwh\samples\psfiles\