{"id":53703,"date":"2009-05-21T15:25:00","date_gmt":"2009-05-21T15:25:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/05\/21\/hey-scripting-guy-how-can-i-perform-more-than-one-action-with-the-win32_printer-wmi-class\/"},"modified":"2009-05-21T15:25:00","modified_gmt":"2009-05-21T15:25:00","slug":"hey-scripting-guy-how-can-i-perform-more-than-one-action-with-the-win32_printer-wmi-class","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-perform-more-than-one-action-with-the-win32_printer-wmi-class\/","title":{"rendered":"Hey, Scripting Guy! How Can I Perform More Than One Action with the Win32_Printer WMI Class?"},"content":{"rendered":"<h2><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" width=\"34\" height=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\"> <\/h2>\n<p>Hey, Scripting Guy! The <b>Win32_Printer<\/b> WMI class has several methods that are listed in it. But I am not sure how to best use these methods from inside a Windows PowerShell script. I do not want to create four different scripts to send a test page, clean out print jobs, and to cycle the print queue. Do you have an example of how I might do more than one action in a script?<\/p>\n<p>&#8211; AA<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" width=\"5\" height=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" width=\"34\" height=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\"> <\/p>\n<p>Hi AA,<\/p>\n<p>Ed is relaxing after a long hard week of working at Tech\u2219Ed. His talk was a success, and he is resting on his laurels. He feels that he might be able to milk it for at least another week. In fact, he is sitting with his feet propped up listening to <a target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Mac_Davis\">Mac Davis<\/a> on his Zune. Mac Davis, he feels, goes well with green tea with a hint of jasmine. If we can get his attention, perhaps he might return to work.<\/p>\n<table id=\"E6C\" class=\"dataTable\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\">This week we will examine printing. There are lots of resources related to printing on the TechNet Script Center. There is a collection of <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/printing.mspx\">Hey, Scripting Guy! articles<\/a> that lists 24 articles relates to both server-side and client-side printing. We have almost 50 scripts in the <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/printing\/default.mspx\">printer section<\/a> of the <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/default.mspx\">Script Center Script Repository<\/a>. There are also almost 50 scripts in the <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/csc\/scripts\/print\/default.mspx\">printer section<\/a> of the Community-Submitted Scripts Center. We have some good technical information about printing in the <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_prn_overview.mspx\">Scripting Guide<\/a>. All those resources are in VBScript. To use them in Windows PowerShell, you would have to translate the scripts into PowerShell. The Microsoft Press book, <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth9541.aspx\">Windows PowerShell Scripting Guide<\/a>, has a whole chapter devoted to printing.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Well, AA, you are in luck, we have Ed\u2019s attention. Ed loves writing scripts that perform multiple actions. To do this, he uses command-line parameters. The <b>ManagePrinter.ps1<\/b> script shows how to call multiple printer functions. You can call it by using command lines similar to this. Each command line allows you to access a different functionality provided by the <b>ManagePrinter.ps1<\/b> script. The five different combinations allow you to perform routine tasks such as sending a test page, stopping print jobs, pausing, and resuming the printer.<\/p>\n<pre class=\"codeSample\"># List local printers on the localhost.\\ManagePrinter.ps1 -list -computer localhost# Send a test page to a printer named testprinter on the localhost .\\ManagePrinter.ps1 -computer localhost -sendTestPage -printerPath testprinter# Cancel all print jobs on the testprinter on the localhost.\\ManagePrinter.ps1 -computer localhost -stopPrintJobs -printerPath testprinter# Resume printing on the testprinter on the localhost.\\ManagePrinter.ps1 -computer localhost -printerPath testprinter \u2013resume# suspend printing on the testprinter on the localhost.\\ManagePrinter.ps1 -computer localhost -printerPath testprinter \u2013suspend<\/pre>\n<p>The complete <b>ManagePrinter.ps1<\/b> script is seen here:<\/p>\n<p><b>ManagePrinter.ps1<\/b><\/p>\n<pre class=\"codeSample\">Param(            $computer,             $printerPath,             [switch]$list,             [switch]$sendTestPage,            [switch]$stopPrintJobs,            [switch]$suspend,            [switch]$resume)Function Get-Printer($computer){ Get-WmiObject -class Win32_Printer -computer $computer} #end Get-PrinterFunction Format-Printer($printObject){ Write-Host -foregroundcolor cyan \"Shared printers on $computer\" $printObject |  Where-Object { $_.sharename } | Format-Table -property sharename, location, comment -autosize -wrap} #end Format-PrinterFunction Get-SuccessCode($code){ if($code.ReturnValue -eq 0)  { Write-Host -foregroundcolor green \"Operation suceeded!\" } Else  { Write-Host -foregroundcolor red \"Operation failed with $($code.returnvalue)\" }} #end get-successcodeFunction Send-TestPage($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Sending test page to $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath }  $dp.PrintTestPage()} #end Send-TestPageFunction Stop-AllPrintJobs($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Cancelling all print jobs on $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath }  $dp.CancelAllJobs()} #end Stop-AllPrintJobsFunction Suspend-Printer($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Suspending printing on $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath }  $dp.Pause()} #end Suspend-PrinterFunction Resume-Printer($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Resuming printing on $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath }  $dp.Resume()} #end Resume-Printer# *** Entry Point to Script ***if($list) { Format-Printer(Get-Printer($computer)) ; exit }if($sendTestPage) {   Get-SuccessCode -code `   (Send-TestPage -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit }if($stopPrintJobs) {   Get-SuccessCode -code `   (Stop-AllPrintJobs -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit }if($suspend) {   Get-SuccessCode -code `   (Suspend-Printer -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit }if($resume) {   Get-SuccessCode -code `   (Resume-Printer -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit }<\/pre>\n<p>The first thing the <b>ManagePrinter.ps1<\/b> script does is create a number of parameters. The <b>\u2013computer<\/b> parameter holds the name of the destination computer, and the <b>\u2013printerpath<\/b> parameter holds the name of the printer. The other parameters are switched parameters that are used to obtain access to different functions within the script. The <b>param<\/b> statement is seen here:<\/p>\n<pre class=\"codeSample\">Param(            $computer,             $printerPath,             [switch]$list,             [switch]$sendTestPage,            [switch]$stopPrintJobs,            [switch]$suspend,            [switch]$resume)<\/pre>\n<p>The first function, the <b>Get-Printer<\/b> function, is the same as the <b>Get-Printer<\/b> function from <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may09\/hey0519.mspx\">Tuesday&#8217;s article<\/a>:<\/p>\n<pre class=\"codeSample\">Function Get-Printer($computer){Get-WmiObject -class Win32_Printer -computer $computer} #end Get-Printer<\/pre>\n<p>The second function, the <b>Format-Printer<\/b> function, is also the same as the <b>Format-Printer<\/b> function from Tuesday&#8217;s article:<\/p>\n<pre class=\"codeSample\">Function Format-Printer($printObject){Write-Host -foregroundcolor cyan \"Shared printers on $computer\"$printObject | Where-Object { $_.sharename } |Format-Table -property sharename, location, comment -autosize -wrap} #end Format-Printer<\/pre>\n<p>We now come to the <b>Get-SuccessCode<\/b> function. Guess what? It is also the same as the <b>Get-SuccessCode<\/b> function from Tuesday (please refer to <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may09\/hey0519.mspx\">Tuesday\u2019s \u201cHey, Scripting Guy!\u201d article<\/a> for a discussion of that function):<\/p>\n<pre class=\"codeSample\">Function Get-SuccessCode($code){if($code.ReturnValue -eq 0)  { Write-Host -foregroundcolor green \"Operation suceeded!\" }Else  { Write-Host -foregroundcolor red \"Operation failed with $($code.returnvalue)\" }} #end get-successcode<\/pre>\n<p>Now we arrive at something new. The <b>Send-TestPage<\/b> function is used to send a test page to a printer. When this function is called, it is passed a WMI <b>Win32_Printer<\/b> object (which is returned by the <b>Get-Printer<\/b> function) in addition to the name of the printer that will receive the test page. The first thing the <b>Send-TestPage<\/b> function does is display a message that states it is sending a test page to the printer designated by the string contained in the <b>$printerPath<\/b> variable. The WMI <b>Win32_Printer<\/b> object is pipelined to the <b>Where-Object<\/b> cmdlet. Inside the script block for the <b>Where-Object<\/b>, the <b>deviceID<\/b> property of each printer in the collection of printers is searched to see whether it matches the string contained in the <b>$printerPath<\/b> variable. If a match is found, the printer object is held in the <b>$dp<\/b> variable. The <b>PrintTestPage<\/b> method is called from the <b>Win32_Printer<\/b> WMI object that is stored in the <b>$dp<\/b> variable. The complete <b>Send-TestPage<\/b> function is shown here:<\/p>\n<pre class=\"codeSample\">Function Send-TestPage($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Sending test page to $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath }  $dp.PrintTestPage()} #end Send-TestPage<\/pre>\n<p>The command line to send a test page to a printer specifies the name of the computer, the <b>sendtestpage<\/b> switched parameter, and the name of the printer. This is seen here:<\/p>\n<pre class=\"codeSample\">.\\ManagePrinter.ps1 -computer localhost -sendTestPage -printerPath testprinter<\/pre>\n<p>When the <b>ManagePrinter.ps1<\/b> script starts with the previous command line, this is displayed:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of a successful operation\" width=\"500\" height=\"158\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0521\/hsg-05-21-09-01.jpg\"> <\/p>\n<p>&nbsp;<\/p>\n<p>If the <b>ManagePrinter.ps1<\/b> script is run with the <b>StopPrintJobs<\/b> parameter, it calls the <b>Stop-AllPrintJobs<\/b> function. The command line to call the <b>Stop-AllPrintJobs<\/b> function is seen here:<\/p>\n<pre class=\"codeSample\">.\\ManagePrinter.ps1 -computer localhost -stopprintjobs -printerPath testprinter<\/pre>\n<p>When the <b>Stop-AllPrintJobs<\/b> function is called, it receives the <b>Win32_Printer<\/b> object that is contained in the <b>$printObject<\/b> variable and the path of the printer whose print jobs are to be canceled. The syntax of the <b>Stop-AllPrintJobs<\/b> function resembles the <b>SendTestPage<\/b> function except that the <b>Stop-AllPrintJobs<\/b> function calls the <b>CancelAllJobs<\/b> method from the <b>Win32_Printer<\/b> object that is contained in the <b>$dp<\/b> variable. The <b>Stop-AllPrintJobs<\/b> function is seen here:<\/p>\n<pre class=\"codeSample\">Function Stop-AllPrintJobs($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Canceling all print jobs on $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath }  $dp.CancelAllJobs()} #end Stop-AllPrintJobs<\/pre>\n<p>If you have to suspend printing, you call the <b>Suspend-Printer<\/b> function and pass the <b>Win32_Printer<\/b> object and the path to the printer that will be paused. When you start the <b>ManagePrinter.ps1<\/b> script to pause printing, you would use this command-line syntax:<\/p>\n<pre class=\"codeSample\">.\\ManagePrinter.ps1 -computer localhost -printerPath testprinter \u2013suspend<\/pre>\n<p>In the <b>Suspend-Printer<\/b> function, the first thing that happens is a message is displayed that states printing is being suspended on the target printer. The <b>Win32_Printer<\/b> WMI object is pipelined to the <b>Where-Object<\/b> where it is filtered based on the <b>deviceID<\/b> property. The resulting object is used to call the <b>pause<\/b> method. This is seen here:<\/p>\n<pre class=\"codeSample\">Function Suspend-Printer($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Suspending printing on $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath }  $dp.Pause()} #end Suspend-Printer<\/pre>\n<p>When the <b>ManagePrinter.ps1<\/b> script is used to suspend printing, the target printer\u2019s status changes to paused:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of the target printer's status that has changed to Paused\" width=\"500\" height=\"178\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0521\/hsg-05-21-09-02.jpg\"> <\/p>\n<p>&nbsp;<\/p>\n<p>To resume printing, you call the <b>Resume-Printer<\/b> function. This function works the same as the <b>Suspend-Printer<\/b> function because it accepts two parameters. The first parameter is the <b>Win32_Printer<\/b> object that is contained in the <b>$printObject<\/b> variable, and the other is the name of the printer stored in the <b>$printerPath<\/b> variable. The <b>resume<\/b> method is called from the printer object. To resume printing by using the <b>ManagePrinter.ps1<\/b> script, you would use this command line:<\/p>\n<pre class=\"codeSample\">.\\ManagePrinter.ps1 -computer localhost -printerPath testprinter \u2013resume<\/pre>\n<p>The complete <b>Resume-Printer<\/b> function is seen here:<\/p>\n<pre class=\"codeSample\">Function Resume-Printer($printObject, [string]$printerPath){  Write-Host -foregroundcolor cyan \"Resuming printing on $printerPath\"  $dp = $printObject |  Where-Object { $_.deviceID -match $printerPath } $dp.Resume()} #end Resume-Printer<\/pre>\n<p>The entry point to the script is used to examine the command line parameters. If the <b>\u2013list<\/b> parameter is specified, the <b>Get-Printer<\/b> function is called by using the computer name that is contained in the <b>$computer<\/b> variable. The resulting management object is passed to the <b>Format-Printer<\/b> function and the script exits. This is seen here:<\/p>\n<pre class=\"codeSample\">if($list) { Format-Printer(Get-Printer($computer)) ; exit }<\/pre>\n<p>If the <b>\u2013sendTestPage<\/b> switched parameter is used on the command line, the <b>Get-Printer<\/b> function is called and the resulting management object is passed to the <b>Send-Test<\/b><b>P<\/b><b>age<\/b> function by using the <b>\u2013printObject<\/b> parameter:<\/p>\n<pre class=\"codeSample\">if($sendTestPage){   Get-SuccessCode -code `   (Send-TestPage -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit }<\/pre>\n<p>If the <b>\u2013stopPrintJobs<\/b> switched parameter is used, the <b>Get-Printer<\/b> function is used to retrieve a <b>Win32_Printer<\/b> management object, which is passed to the <b>Stop-AllPrintJobs<\/b> function through the <b>\u2013printObject<\/b> parameter. The target printer is passed via the <b>\u2013printerPath<\/b> parameter. The return value from calling the <b>Stop-AllPrintJobs<\/b> function is parsed by the <b>Get-SuccessCode<\/b> function. This is seen here:<\/p>\n<pre class=\"codeSample\">if($stopPrintJobs){   Get-SuccessCode -code `   (Stop-AllPrintJobs -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit}<\/pre>\n<p>If the script is run with the <b>\u2013suspend<\/b> switched parameter, the <b>Get-Printer<\/b> function is used to return a <b>Win32_Printer<\/b> object, which is passed to the <b>Suspend-Printer<\/b> function through the <b>\u2013printObject<\/b> parameter. The path of the printer is passed to the <b>Suspend-Printer<\/b> function through the <b>\u2013printerpath<\/b> parameter. The return value is evaluated by the <b>Get-SuccessCode<\/b> function through the <b>\u2013code<\/b> parameter. This is shown here:<\/p>\n<pre class=\"codeSample\">if($suspend) {   Get-SuccessCode -code `   (Suspend-Printer -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit}<\/pre>\n<p>If the script is started with the <b>\u2013resume<\/b> parameter, the first thing that happens is the <b>Get-Printer<\/b> function is used to create a <b>Win32_Printer<\/b> WMI management object, which is passed to the <b>Resume-Printer<\/b> function through the <b>\u2013printobject<\/b> parameter. The name of the printer is passed to the <b>Resume-Printer<\/b> function through the <b>\u2013printerpath<\/b> parameter. The <b>Get-SuccessCode<\/b> function is used to evaluate the return value. This is seen here:<\/p>\n<pre class=\"codeSample\">if($resume){   Get-SuccessCode -code `   (Resume-Printer -printObject(Get-Printer($computer)) -printerPath $printerPath)   exit}<\/pre>\n<p>Well, AA, we come to the end of the <b>ManagePrinter.ps1 <\/b>script. It is also the end of Printing Week on the Script Center. Join us tomorrow for Quick-Hits Friday as we answer several short e-mail messages that cover a variety of topics. Until then, take care and we\u2019ll see you over the weekend and next week.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! The Win32_Printer WMI class has several methods that are listed in it. But I am not sure how to best use these methods from inside a Windows PowerShell script. I do not want to create four different scripts to send a test page, clean out print jobs, and to cycle the print [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[445,404,3,5,45],"class_list":["post-53703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-vbscript","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! The Win32_Printer WMI class has several methods that are listed in it. But I am not sure how to best use these methods from inside a Windows PowerShell script. I do not want to create four different scripts to send a test page, clean out print jobs, and to cycle the print [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=53703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53703\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=53703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}