-2

I have a PowerShell script:

    $objPrintQueues = Get-WmiObject Win32_PerfFormattedData_Spooler_PrintQueue | Select Jobs
    $queuesCounter=0
    $queuesDetector=0
    foreach($queue in $objPrintQueues) { $queuesCounter=$queuesCounter+$queue.Jobs }
    if ($queuesCounter > 0) { $queuesDetector=1 }
    echo $queuesDetector

It detects, is there some jobs in printing queue (in spooler) or printers are free. I need the same functionality from the .bat (batch) file. Is it possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
Natalia
  • 1
  • 1
  • I don't think this is possible with pure batch scripting; the only idea I have is `wmic PrintJob`, but I don't know how the output may be of service for your task... What did you find out so far? – aschipfl Oct 27 '16 at 14:59
  • I know that may be there are some SNMP ways, but is it applicable for local printers? And I know that Print Spooler API has an EnumJobs function, but I don't know, can bash work with Print Spooler or not. And I can't find clear examples: all examples are about stop, clear, start and restart spooler, not about how to check for emptyness. – Natalia Oct 27 '16 at 15:19
  • You mean [tag:batch-file], right? note that [tag:bash] is something completely different... – aschipfl Oct 27 '16 at 15:32
  • Yes, I'm sorry. In parallel to PowerShell I wrote script with the same logic for Posix systems, so now in my head 'bash' and 'batch' are closely near. – Natalia Oct 27 '16 at 15:49

1 Answers1

0

It kind of depends on why you need it in a .bat file, if it's because the program you are using to call the software only supports running .bat files and not .ps1 files then you can just write a .bat that runs powershell.exe and calls your script. If it's because you are prevented from running PowerShell on the systems you are working with by security policies than it's going to be significantly harder, assuming you can't convince your security people to allow you to run signed scripts. It's likely possible via WMI scripting but on that count I can only point you to the MicroSoft resources on how to create that type of code - https://technet.microsoft.com/en-us/library/ee156560.aspx. Hope that helps a bit.

Mike Garuccio
  • 2,588
  • 1
  • 11
  • 20
  • Thank you for reply, Mike. Yes, it's about security problems. Ifwe want to run scripts, we first must execute command "Set-ExecutionPolicy Unrestricted" And it will very uncomfortably for our project clients. Bash script can solve this problem, but it more difficult to write such bash. – Natalia Oct 27 '16 at 15:21
  • so is there a reason you cant use code signing? unrestricted worries clients but often times they are comfortable with setting the execution policy to remotesigned. Also if this is something that is run regularly in the background you can always create a service account and use the -scope property of set-executionpolicy to limit the unrestricted script access to that single account, or if this is only used in one-off situations you can either use the -process scope to only disable the executionpolicy for the running process or just paste the code into ISE and run from there without saving. – Mike Garuccio Oct 27 '16 at 15:28
  • "bash" is "batch" in my previous comment, sorry. – Natalia Oct 27 '16 at 15:38
  • yea that's what I figured, and doesn't really impact any of the other things, @aschipfl is correct that wmic is probably the way to go if running a powershell script is not possible, but not many PS devs are going to be able to assist with that, you may want to post with a WMI tag here - http://stackoverflow.com/questions/tagged/wmi for assistance with that portion – Mike Garuccio Oct 27 '16 at 15:43