KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Problem with Benny's "Purge Print Que..."-script?

 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Wed Dec 20, 2006 3:26 pm    Post subject: Problem with Benny's "Purge Print Que..."-script? Reply with quote

Hi!

Decided to test Benny's script (found here).

But it bugs out on line 274, when I click on an item in the left listview.

/Viggen
Back to top
View user's profile Send private message MSN Messenger
Les
KiXforms Aficionado
KiXforms Aficionado


Joined: 24 Dec 2003
Posts: 317

PostPosted: Wed Dec 20, 2006 5:01 pm    Post subject: Reply with quote

Care to share with us what line 234 is and what error you get.
_________________
The Repro Man
Stealing for a living!
Back to top
View user's profile Send private message
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Wed Dec 20, 2006 5:44 pm    Post subject: Reply with quote

Well... Line 274 is:

Code:
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2")


I'm @ home right now, so the I haven't got the exact error, but something along this:
"Missing ')' "

/Viggen
Back to top
View user's profile Send private message MSN Messenger
Les
KiXforms Aficionado
KiXforms Aficionado


Joined: 24 Dec 2003
Posts: 317

PostPosted: Wed Dec 20, 2006 6:22 pm    Post subject: Reply with quote

So, what OS is this running on?
Does it have WMI installed?
What version of KiX?

_________________
The Repro Man
Stealing for a living!
Back to top
View user's profile Send private message
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Wed Dec 20, 2006 7:01 pm    Post subject: Reply with quote

OS: Windows XP SP2
KiX: 4.53
KF.Net: 3.1.45.0

I run many other WMI-scripts that works with no problem Smile

/Viggen
Back to top
View user's profile Send private message MSN Messenger
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Wed Dec 20, 2006 8:48 pm    Post subject: Reply with quote

Hi Viggen,
This script will only work on a network, if that is where you are getting the error, does the ListView on the left populate with printers? Are they shared printers you know "\\servername\printersharename"? this script is for network shared printers only.

_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Wed Dec 20, 2006 11:10 pm    Post subject: Reply with quote

The printers (and the servers) list correctly.
(It finds all our AD-servers with printers installed.)

When I click on one of the listed items, the script errors out.

So it's the "GetPrintQue()"-function that 'dies'

/Viggen
Back to top
View user's profile Send private message MSN Messenger
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Thu Dec 21, 2006 12:49 pm    Post subject: Reply with quote

when the script fails, tell me what the list item is that you are clicking on?
_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Thu Dec 21, 2006 12:54 pm    Post subject: Reply with quote

Index number or item text?
(have tested different printers, and all fail at the same line)


Edit:



prntque.jpg
 Description:
 Filesize:  46.21 KB
 Viewed:  967 Time(s)

prntque.jpg


Back to top
View user's profile Send private message MSN Messenger
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Thu Dec 21, 2006 1:18 pm    Post subject: Reply with quote

do any of them not give an error?
_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Thu Dec 21, 2006 1:30 pm    Post subject: Reply with quote

try this code, let me know what you get:
Code:

;region Setup Variables
$Title = "Purge Print Que..."
$BuildVer = '1.0.2.0'
$Version = " Build($BuildVer)"

$objRootDSE = GetObject("LDAP://rootDSE")
$defaultNamingContext = $objRootDSE.Get("defaultNamingContext")

Break On
$System = CreateObject("Kixforms.System")
If Not $System
   $nul= MessageBox("KiXforms.Net Not Initiated. This Script Will Now Close.","Error",16)
   Quit()
EndIf
$nul = $System.Application.EnableVisualStyles

$SysVer = $System.ProductVersion
$MinSysVer = "3.1.29.0"
If $SysVer < $MinSysVer
   $nul= MessageBox("KiXforms.Net Version Installed ("+$SysVer+")."+@CRLF+"The Minimum Version that must be Installed is ("+$MinSysVer+"). "+@CRLF+"This Script Will Now Close.","Error",16)
   Quit()
EndIf
;endregion

;region Main Form
$Form = $System.Form()
$Form.StartPosition = 1  ;FormStartPosition_CenterScreen
$Form.Size = $System.Size(775,353) ;(Width,Height)
$Form.Text = $Title + $Version

$MainPanel = $System.Panel()
$MainPanel.Dock = 5  ;Fill
$nul = $Form.Controls.Add($MainPanel)

;region MainMenu
$MainMenu = $System.MainMenu()

$File = $MainMenu.MenuItems.Add($System.MenuItem("File"))

$Exit = $File.MenuItems.Add($System.MenuItem("Exit"))
$Exit.Click = "ExitForm()"

$Tools = $MainMenu.MenuItems.Add($System.MenuItem("Tools"))

$Refresh = $Tools.MenuItems.Add($System.MenuItem("Refresh"))
$Refresh.Click = "SearchForPrinters()"

$Form.Menu = $MainMenu
;endregion

$ListView = $System.ListView()
$ListView.CheckBoxes = -1  ;True
$ListView.Dock = 5  ;Fill
$ListView.FullRowSelect = -1  ;True
$ListView.View = $System.View_Details
$nul = $MainPanel.Controls.Add($ListView)

$Column0 = $ListView.Columns.Add($System.ColumnHeader("Document Name",100,$System.HorizontalAlignment_Left))
$Column1 = $ListView.Columns.Add($System.ColumnHeader("Status",60,$System.HorizontalAlignment_Left))
$Column2 = $ListView.Columns.Add($System.ColumnHeader("Owner",60,$System.HorizontalAlignment_Left))
$Column3 = $ListView.Columns.Add($System.ColumnHeader("Pages",50,$System.HorizontalAlignment_Left))
$Column4 = $ListView.Columns.Add($System.ColumnHeader("Size",60,$System.HorizontalAlignment_Left))
$Column5 = $ListView.Columns.Add($System.ColumnHeader("Submitted",120,$System.HorizontalAlignment_Left))

$RightPanel = $System.Panel()
$RightPanel.Dock = 4  ;Right
$nul = $MainPanel.Controls.Add($RightPanel)

$Splitter = $System.Splitter()
$nul = $MainPanel.Controls.Add($Splitter)

$ListBox = $System.ListBox()
$ListBox.Click = "EnablePurge()"
$ListBox.Dock = 3  ;Left
$ListBox.Width = 213
$nul = $MainPanel.Controls.Add($ListBox)

;region StatusBar
$StatusBar = $System.StatusBar()
$StatusBar.Text = ""
$nul = $MainPanel.Controls.Add($StatusBar)

$ProgressBar = $System.ProgressBar()
$ProgressBar.Left = $StatusBar.Width-$ProgressBar.Width-20
$ProgressBar.Top = 3
$ProgressBar.Height = $StatusBar.Height-4
$ProgressBar.Visible = 0
$nul = $StatusBar.Controls.Add($ProgressBar)
;endregion

$Cancel = $System.Button()
$Cancel.Click = "CancelPurge()"
$Cancel.DialogResult = 2  ;Cancel
$Cancel.FlatStyle = 3  ;System
$Cancel.Left = 10
$Cancel.Text = "Cancel"
$Cancel.Top = 10
$nul = $RightPanel.Controls.Add($Cancel)

$SelectAll = $System.Button()
$SelectAll.Click = "SelectAll()"
$SelectAll.Enabled = 0
$SelectAll.FlatStyle = 3  ;System
$SelectAll.Left = 10
$SelectAll.Text = "Select All"
$SelectAll.Top = $Cancel.Bottom+10
$nul = $RightPanel.Controls.Add($SelectAll)

$Purge = $System.Button()
$Purge.Click = "PurgePrintQue()"
$Purge.Enabled = 0
$Purge.FlatStyle = 3  ;System
$Purge.Left = 10
$Purge.Text = "Purge"
$Purge.Top = $SelectAll.Bottom+10
$nul = $RightPanel.Controls.Add($Purge)

$RightPanel.Width = $Purge.Width+20

$Form.AcceptButton = $Purge
$Form.CancelButton = $Cancel
$Form.Show  ;Displays the Form

SearchForPrinters()

While $Form.Visible
   $Nul = Execute($Form.DoEvents())
Loop
Exit 0
;endregion

Function ExitForm()
   Quit()
EndFunction

Function AdvanceProgressBar($Step,Optional $Text)
   
   $Count = $Count + $Step
   If $Count > 100
      $Count = 0
   EndIf
   $StatusBar.Text = $Text
   $ProgressBar.Value = $Count
   Sleep 0.05
   
EndFunction

Function SearchForPrinters()
   
   $ProgressBar.Visible = -1
   $ListBox.Items.Clear
   If $Purge.Enabled = -1
      $Purge.Enabled = 0
      $SelectAll.Enabled = 0
   EndIf
   
   $ADS_SCOPE_SUBTREE = 2
   $objConnection = CreateObject("ADODB.Connection")
   $objCommand = CreateObject("ADODB.Command")
   $objConnection.Provider = "ADsDSOObject"
   $objConnection.Open("Active Directory Provider")
   $objCommand.ActiveConnection = $objConnection
   $objCommand.CommandText = "Select url from 'LDAP://$defaultNamingContext' where objectClass='printQueue' "
   $objCommand.Properties("Page Size",1000)
   $objCommand.Properties("Searchscope",$ADS_SCOPE_SUBTREE)
   $objRecordSet = $objCommand.Execute
   $objRecordSet.MoveFirst
   Do
      $url = $objRecordSet.Fields("url").Value
      For Each $Item in $url
         $Period = InStr($Item,".")
         If $Period > 0
               $Array1 = Split($Item,"//")
               $Array2 = Split($Array1[1],".")
               $ServerName = $Array2[0]
               $Array3 = Split($Array2[2],"/")
               $ShareName = $Array3[1]
               $PrinterShareName = "\\" + $ServerName + "\" + $ShareName
               AdvanceProgressBar(10,$PrinterShareName)
               $ListItem = $ListBox.Items.Add($PrinterShareName)
         EndIf
      Next
      $objRecordSet.MoveNext
   Until $objRecordSet.EOF
   
   ;Object cleanup
   $objConnection = 0
   $objCommand = 0
   $objRecordSet = 0
   
   $Count = 0
   AdvanceProgressBar(0,"")
   $ProgressBar.Visible = 0
   
EndFunction

Function EnablePurge()
   
   $Item = $ListBox.SelectedItem
   If $Item
      GetPrintQue()
   EndIf
   
EndFunction

Function CancelPurge()
   
   $ListBox.SetSelected(0,0)
   $ListView.Items.Clear
   If $Purge.Enabled = -1
      $Purge.Enabled = 0
      $SelectAll.Enabled = 0
   EndIf
   
EndFunction

Function PurgePrintQue()
   
   $Index = 0
   $Item = $ListBox.SelectedItem
   $Array = Split($Item,"\")
   
   $strComputer = $Array[2]
   $PrinterName = $Array[3]
   
   $objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2")
   $objPrinter = $objWMIService.ExecQuery("Select * from Win32_Printer")
   For Each $objPrinterName in $objPrinter
      If $objPrinterName.ShareName = $ShareName
         $PrinterName = $objPrinterName.Name
      EndIf
   Next
   
   $objPrintJobs = $objWMIService.ExecQuery("Select * from Win32_PrintJob",,48)
   For Each $objPrintJob in $objPrintJobs
      $objName = $objPrintJob.Name
      $Array = Split($objName,",")
      $PName = $Array[0]
      If $PName = $PrinterName
         $Item = $ListView.Items.Item($Index)
         If $Item.Checked; = -1  ;True
               $objPrintJob.Delete_
         EndIf
         $Index = $Index + 1
      EndIf
   Next
   
   $ListView.Items.Clear
   $ListBox.SetSelected(0,0)
   If $Purge.Enabled = -1
      $Purge.Enabled = 0
      $SelectAll.Enabled = 0
   EndIf
   
EndFunction

Function GetPrintQue()

   $ListView.Items.Clear
   $Item = $ListBox.SelectedItem
   $Array = Split($Item,"\")
   
   $strComputer = $Array[2]
   $ShareName = $Array[3]
   
   $objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2")
   If Not(@ERROR = 0)
      $nul = MessageBox("Error connecting to the remote computer '"+$strComputer+"' SError# "+@SERROR+" Error#"+@ERROR,"Error",16)
      Quit @SERROR
   EndIf
   $objPrinter = $objWMIService.ExecQuery("Select * from Win32_Printer")
   For Each $objPrinterName in $objPrinter
      If $objPrinterName.ShareName = $ShareName
         $PrinterName = $objPrinterName.Name
      EndIf
   Next
   
   $objPrintJobs = $objWMIService.ExecQuery("Select * from Win32_PrintJob",,48)
   For Each $objPrintJob in $objPrintJobs
      $objName = $objPrintJob.Name
      $Array = Split($objName,",")
      $PName = $Array[0]
      If $PName = $PrinterName
         $Item = $ListView.Items.Add($System.ListViewItem($objPrintJob.Document))
         $ItemIndex = $Item.Index
         If $objPrintJob.Status = "UNKNOWN"
               $Item = $ListView.Items.Item($ItemIndex).SubItems.Add($System.ListViewSubItem(""))
         Else
               $Item = $ListView.Items.Item($ItemIndex).SubItems.Add($System.ListViewSubItem($objPrintJob.Status))
         EndIf
         $Item = $ListView.Items.Item($ItemIndex).SubItems.Add($System.ListViewSubItem($objPrintJob.Owner))
         $Item = $ListView.Items.Item($ItemIndex).SubItems.Add($System.ListViewSubItem($objPrintJob.TotalPages))
         $Item = $ListView.Items.Item($ItemIndex).SubItems.Add($System.ListViewSubItem($objPrintJob.Size))
         $TimeSubmitted = $objPrintJob.TimeSubmitted
         $year = SubStr($TimeSubmitted,1,4)
         $month = SubStr($TimeSubmitted,5,2)
         $day = SubStr($TimeSubmitted,7,2)
         $hour = SubStr($TimeSubmitted,9,2)
         $min = SubStr($TimeSubmitted,11,2)
         $sec = SubStr($TimeSubmitted,13,2)
         $Submitted = $hour+":"+$min+":"+$sec+" "+$month+"/"+$day+"/"+$year
         $Item = $ListView.Items.Item($ItemIndex).SubItems.Add($System.ListViewSubItem($Submitted))
      EndIf
   Next
   If $ListView.Items.Count > 0
      $Purge.Enabled = -1
      $SelectAll.Enabled = -1
   Else
      $Purge.Enabled = 0
      $SelectAll.Enabled = 0
   EndIf
   
EndFunction

Function SelectAll()
   
   For Each $Item in $ListView.Items
      $Item.Checked = -1  ;True
   Next
   
EndFunction

_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)


Last edited by benny69 on Thu Dec 21, 2006 5:28 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Thu Dec 21, 2006 2:39 pm    Post subject: Reply with quote

Hmmm... this is very odd...
The error indicates a rights problem (error: -2147024891)

But if I use DameWare NT Utilities, I have no problem manipulating the printers
(member of Domain Admins)

/Viggen
Back to top
View user's profile Send private message MSN Messenger
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Thu Dec 21, 2006 5:29 pm    Post subject: Reply with quote

I have modified my previous code post to include @SERROR, run it and lets see what happens.
_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Fri Dec 22, 2006 7:58 am    Post subject: Reply with quote

Yepp... "Access denied"...
Very strange...
But I don't get the "expected ')'! " anymore Smile

Must look into the server rights, and I also must try an figure out why DW works...

If I can get this to run, it sure looks like a very handy tool!
(alot less for the helpdesk to click on, compared to DW)

/Viggen
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group