WMI Win32_TCPIPPrinterPort InstallDate
- From: Al Haraka <alharaka@xxxxxxxxx>
- Date: Fri, 22 May 2009 07:44:20 -0700 (PDT)
I am trying to use WMI in VBScript to install remote printers. Now
that it is correctly installing a port, driver, and printer, I want to
add some more details to certain fields so the data is more useful
later on. The trouble I am having here is setting the InstallDate
correctly. A quick inspection using PowerShell reveals the following
for the printer port in question:
PS C:\Windows\System32> gwmi -namespace "root\cimv2" -class
"win32_tcpipprinterport"
__GENUS : 2
__CLASS : Win32_TCPIPPrinterPort
__SUPERCLASS : CIM_ServiceAccessPoint
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_TCPIPPrinterPort.Name="TEST Port"
__PROPERTY_COUNT : 17
__DERIVATION : {CIM_ServiceAccessPoint, CIM_LogicalElement,
CIM_ManagedSystemElement}
__SERVER : HOSTNAME
__NAMESPACE : root\cimv2
__PATH : \\HOSTNAME\root
\cimv2:Win32_TCPIPPrinterPort.Name="TEST Port"
ByteCount : True
Caption :
CreationClassName : Win32_TCPIPPrinterPort
Description :
HostAddress : lprserver.company.com
InstallDate :
Name : TEST Port
PortNumber : 515
Protocol : 2
Queue : TEST_Q
SNMPCommunity :
SNMPDevIndex :
SNMPEnabled : False
Status :
SystemCreationClassName : Win32_ComputerSystem
SystemName :
Type :
Now, I originally tried setting the printer port's install date using
Now(), the Date() in the script. This lead to a type mismatch. Once
I read more here, my impression was I needed to create a proper
SWbemDateTime object to pass the installation date in VT_DATE format
(so it is in UTC). I tried what I think is correct. I tried
debugging it with output, and it appears the function returns the
object. Nonetheless, I get the same result in PowerShell after
deleting the port, restarting the spooler service, and running the
script twice. Below is the code. Can anyone give me a clue here? I
am lost as hell. I am testing it in the sub InstallPrinterPort(),
passing along a SWbemDateTime object from GetInstallDate(). If anyone
could guide me along, I would really appreciate it.
Option Explicit
'On Error Resume Next
Dim objWMIService
'Check registry For key to see if we need to run
If RegistryKeyExists("SOFTWARE\Our Company", "200905HP5415") = False
Then
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
objWMIService.Security_.Privileges.AddAsString
"SeLoadDriverPrivilege", True
' Delete the old printer, if necessary
Delete "TEST Printer", _
"HP LaserJet P4010_P4510 Series PCL 6", _
"lprserver.company.com", _
"TEST_Q", _
"TEST Port"
' Install new printer
Install "TEST Printer", _
"HP LaserJet P4010_P4510 Series PCL 6", _
"\\server\share\drivers\hpc4010c\vista\pcl6", _
"\\server\share\drivers\hpc4010c\vista\pcl6\hpc4015c.inf", _
"lprserver.company.com" , _
"TEST_Q" , _
"TEST Port"
' Add new registry key
Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
'SetRegistryKey "SOFTWARE\Our Company", "200905HP5415", "Yes"
End If
' End of modifiable portion of script, begin Subs
Sub Install(strPrinterName, strDriverName, strDriverSource,
strDriverInf, strHostAddress, strQueue, strPortName)
' Install the specified printer, including driver and port
' strHostname can be a hostname OR IP address, see MSDN documentation
for more info:
' http://msdn.microsoft.com/en-us/library/aa394492(VS.85).aspx
InstallPrinterPort strHostAddress, strQueue, strPortName
'InstallPrinterDriver strDriverName, strDriverSource, strDriverInf
'InstallPrinter strPrinterName, strDriverName, strPortName
End Sub
Sub Delete(strPrinterName, strDriverName, strHostAddress, strQueue,
strPortName)
DeletePrinter(strPrinterName) ' Delete Old Printer
DeletePrinterDriver(strDriverName) ' Delete Old Printer Driver
DeletePrinterPort strHostAddress, strQueue, strPortName
End Sub
Sub InstallPrinter(strPrinterName, strDriverName, strPortName)
' Install printer as detailed
Dim objPrinter : Set objPrinter = objWMIService.Get
("Win32_Printer").SpawnInstance_
objPrinter.DriverName = strDriverName
objPrinter.PortName = strPortName
objPrinter.DeviceID = strPrinterName
objPrinter.Location = ""
objPrinter.Network = True
objPrinter.Shared = False
'objPrinter.InstallDate = Date
objPrinter.Put_
End Sub
Sub InstallPrinterDriver(strDriverName, strDriverSource, strDriverInf)
' Installs the printer driver
Dim objDriver, strInstalledDriverName
Dim strWMIQuery : strWMIQuery = "SELECT Name FROM
Win32_PrinterDriver"
' First check whether the driver is installed already
Dim colInstalledDrivers : Set colInstalledDrivers =
objWMIService.ExecQuery(strWMIQuery)
For Each objDriver in colInstalledDrivers
strInstalledDriverName = Left(objDriver.Name, Instr
(1,objDriver.Name,",")-1)
If strInstalledDriverName = strDriverName Then Exit Sub ' We have a
match, so no need to install driver
Next
' Driver not present, so install it
Set objDriver = objWMIService.Get("Win32_PrinterDriver")
objDriver.Name = strDriverName
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
objDriver.DriverPath = strDriverSource
objDriver.Infname = strDriverInf
objDriver.AddPrinterDriver(objDriver)
'objDriver.InstallDate = GetInstallDate(Date)
End Sub
Sub InstallPrinterPort(strHostAddress, strQueue, strPortName)
' First check whether the port exists already
Dim objPort, strWMIQuery : strWMIQuery = "SELECT * FROM
Win32_TCPIPPrinterPort"
Dim colInstalledPorts : Set colInstalledPorts = _
objWMIService.ExecQuery(strWMIQuery)
For Each objPort in colInstalledPorts
If objPort.HostAddress = strHostAddress And _
objPort.Queue = strQueue Then Exit Sub ' We have a result, so no
need to add port
Next
' Add new printer port
Set objPort = objWMIService.Get _
("Win32_TCPIPPrinterPort").SpawnInstance_
objPort.Name = strPortName
' With Pharos, you must enable LPR byte counting,
' or the jobs will not be correctly queued.
objPort.ByteCount = TRUE
objPort.Protocol = 2
objPort.HostAddress = strHostAddress
objPort.Queue = strQueue
objPort.SNMPEnabled = False
objPort.InstallDate = GetInstallDate(Now())
WScript.Echo GetInstallDate(Now())
objPort.Put_
End Sub
Sub SetRegistryKey(strKeyPath, strValueName, strValue)
Const HKEY_LOCAL_MACHINE = &H80000002
Dim strComputer : strComputer = "."
Dim objReg : Set objReg = GetObject("winmgmts:
{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
' Add string value now
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"
& _
strComputer & "\root\default:StdRegProv")
objReg.SetStringValue
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
End Sub
Function RegistryKeyExists(strKeyPath, strValueName)
' Read String and DWORD Registry Values
Dim strValue
Const HKEY_LOCAL_MACHINE = &H80000002
Dim strComputer : strComputer = "."
Dim objReg : Set objReg = GetObject("winmgmts:
{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE, _
strKeyPath, _
strValueName, _
strValue
If LCase(strValue) = "yes" Then
RegistryKeyExists = True
Exit Function
End If
RegistryKeyExists = False
Exit Function
End Function
Sub DeletePrinter(strPrinterName)
Dim objPrinter, _
colInstalledPrinters
Dim strComputer : strComputer = "."
Dim strWMIQuery : strWMIQuery = "Select * from Win32_Printer WHERE
DeviceID = '" & strPrinterName & "'"
'Dim objWMIService
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery (strWMIQuery)
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
End Sub
Sub DeletePrinterDriver(strDriverName)
'Dim objWMIService
Dim objDriver, strComputer : strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
objWMIService.Security_.Privileges.AddAsString
"SeLoadDriverPrivilege", True
Dim colInstalledDrivers : Set colInstalledDrivers = _
objWMIService.ExecQuery("SELECT Name FROM Win32_PrinterDriver")
For Each objDriver in colInstalledDrivers
'WScript.Echo objDriver.Name
If objDriver.Name=strDriverName Then objDriver.Delete_
Next
End Sub
Sub DeletePrinterPort(strHostAddress, strQueue, strPortName)
'Dim objWMIService
Dim objPort, strComputer : strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
objWMIService.Security_.Privileges.AddAsString
"SeLoadDriverPrivilege", True
Dim colInstalledPorts : Set colInstalledPorts = _
objWMIService.ExecQuery("SELECT * FROM Win32_TCPIPPrinterPort")
For Each objPort in colInstalledPorts
'WScript.Echo objPort.Queue
WScript.Echo objPort.InstallDate
If objPort.Name = strPortName And _
objPort.Queue = strQueue And _
objPort.HostAddress = strHostAddress Then
' Sometimes deleting a port errors out. Restarting the spooler
' and deleting the port through the GUI works, so I thought this
' might help if you are in a rut.
' Start print spooler, wait 10000 ms, the start it again
' RestartService "Spooler", 10000
objPort.Delete_
End If
Next
End Sub
Sub RestartService(strServiceName, intSleep)
'Dim objWMIService
Dim objService, strComputer : strComputer = "."
Dim strWMIQuery : strWMIQuery = "SELECT * from Win32_Printer WHERE
DeviceID = '" & strServiceName & "'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
Dim colServices : Set colServices = objWMIService.ExecQuery
(strWMIQuery)
For Each objService in colServices
objService.StopService()
WScript.Sleep intSleep 'intSleep is a measurement in milliseconds
objService.StartService()
Next
End Sub
Function GetInstallDate(dtDateTimeStamp)
' This is not a usual DateTime datatype in VBScript, this it a
SWbemDateTime
' object, only to use for the InstallDate value of certain WMI class
members.
If IsDate(dtDateTimeStamp) Then
Dim objWMIDateTime : Set objWMIDateTime = CreateObject
("WbemScripting.SWbemDateTime")
objWMIDateTime.SetVarDate(CDate(dtDateTimeStamp))
GetInstallDate = objWMIDateTime
Else
Exit Function
End If
End Function
.
- Prev by Date: How to edit a text file (remove carriage returns)
- Next by Date: Re: Vista/7 permissions for script?
- Previous by thread: How to edit a text file (remove carriage returns)
- Next by thread: Startup Script Via GPO... Where Are Parameters Stored?
- Index(es):
Relevant Pages
|