Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Si se usa la API de WMI documentada en esta sección, se pueden administrar dispositivos mediante código administrado o scripting. POSDM.EXE es una interfaz de línea de comandos a esta API. En este ejemplo de VBScript se realizan lo siguientes pasos:
Se usa el método de WMI ExecQuery para recuperar una lista de los objetos PosDevice instalados. Con esta lista de objetos de servicio, el script muestra su tipo, nombre y ruta de acceso correspondientes, así como un estado habilitado o deshabilitado. Esto es similar a ejecutar el siguiente comando:
PosDM.exe LISTDEVICESA continuación, se intenta asignar la ruta de acceso COM1 al objeto de servicio instalado, Microsoft Msr Simulator, mediante el método AddDevice. Esto equivale a ejecutar lo siguiente:
PosDM.exe ADDDEVICE COM1 /SONAME:Microsoft Msr SimulatorSi se produce un error en el método AddDevice, el script detecta el error y asume que es posible que COM1 ya se haya agregado al dispositivo y, por lo tanto, intenta eliminarlo llamando a DeleteDevice. Esto equivale a ejecutar lo siguiente:
PosDM.exe DELETEDEVICE COM1Si el método AddDevice ha producido un error anteriormente, el script intenta volver a llamar a AddDevice. El programa se cierra si se produce un error en el método.
Por último, en el ejemplo se intenta agregar el nombre lógico MSRSim a este objeto de servicio, llamando para ello a AddName. Esto equivale a ejecutar lo siguiente:
PosDM.exe ADDNAME MSRSim /SONAME:"Microsoft Msr Simulator"
Los resultados de este ejemplo se pueden ver ejecutando lo siguiente:
PosDM.exe LISTDEVICES
And
PosDM.exe LISTNAMES
Para ejecutar el ejemplo
El objeto de servicio Microsoft Msr Simulator se instaló con el SDK. Asegúrese de que está instalado en el equipo que va a usar para ejecutar el ejemplo.
Copie este script en un archivo PosDMSample.vbs.
Ejecute el script con la siguiente línea de comandos:
CScript //U PosDMSample.vbs
Ejemplo
'Get a handle to the POS namespace service into 'objServices'.
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objServices = objLocator.ConnectServer(, "/root/MicrosoftPointOfService")
'List the POS devices.
EnumeratePosDevice
'Add a name: MSRSim for Msr Simulator by retrieving the SO and invoking AddDevice() then AddName()
WScript.Echo "Add Device on COM1 and add name 'MSRSim' for MsrSimulator ..."
Set objSO = objServices.Get("ServiceObject.Type='Msr',Name='Microsoft Msr Simulator'")
On Error Resume Next
objSO.AddDevice "COM1"
if Err.number <> 0 Then
WScript.Echo "AddDevice failed - it already is in use."
WScript.Echo "Try to delete the device..."
On Error Resume Next
objSO.DeleteDevice "COM1"
if Err.number <> 0 Then
WScript.Echo "DeleteDevice failed"
WScript.Quit 1
end if
WScript.Echo "DeleteDevice succeeded! Attempting AddDevice again..."
On Error Resume Next
objSO.AddDevice "COM1"
if Err.number <> 0 Then
WScript.Echo "AddDevice failed a second time - exiting"
WScript.Quit 2
end if
end if
Set objDevice = objServices.Get("PosDevice.SoName='Microsoft Msr Simulator',Type='Msr',Path='COM1'")
objDevice.AddName "MSRSim"
Set objDevice = GetDevice("Msr", "MSRSim")
WScript.Echo "Added 'MSRSim' to: " & objDevice.Type & vbTab & objDevice.SoName & vbTab & objDevice.Path
'Enumerate the sClass by name
Sub EnumeratePosDevice( )
sClass = "PosDevice"
WScript.Echo "Enumerating " & sClass & "..." & vbCrLf
Set collection = objServices.ExecQuery("SELECT * From " & sClass)
For Each obj In collection
Enabled = "DISABLED"
if obj.Enabled = true Then
Enabled = "ENABLED"
end If
WScript.Echo obj.Type & Space(15-len(obj.type)) & obj.SoName & Space(35-len(obj.SoName)) & Enabled & vbTab & obj.Path
Next
WScript.Echo vbCrLf
End Sub
'Return a PosDevice matching DeviceType and Name.
Function GetDevice( DeviceType, Name )
Set Logical = GetLogicalDevice( DeviceType, Name )
objectPath = "PosDevice.SoName='" & Logical.SoName & "',Type='" & DeviceType & "',Path='" & Logical.Path & "'"
Set GetDevice = objServices.Get(objectPath)
End Function
'Return a LogicalDevice matching DeviceType and Name.
Function GetLogicalDevice( DeviceType, Name )
Query = "SELECT * From LogicalDevice WHERE Type = '" & DeviceType & "' AND Name='" & Name & "'"
Set collection = objServices.ExecQuery( Query )
For Each obj In collection
Set GetLogicalDevice = obj
exit For
Next
End Function
Si la ruta de acceso COM1 no se ha asignado a un dispositivo, el ejemplo genera una salida similar a este código.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Enumerating PosDevice...
Msr Microsoft Msr Simulator ENABLED
Msr Microsoft Msr Simulator ENABLED COM1
Keylock Microsoft Keylock Simulator ENABLED
Scanner Microsoft Scanner Simulator ENABLED
CashDrawer Microsoft CashDrawer Simulator ENABLED
CheckScanner Microsoft CheckScanner Simulator ENABLED
LineDisplay Microsoft LineDisplay Simulator ENABLED
PinPad Microsoft PinPad Simulator ENABLED
PosPrinter Microsoft PosPrinter Simulator ENABLED
PosKeyboard Microsoft PosKeyboard Simulator ENABLED
Add Device on COM1 and add name 'MSRSim' for MsrSimulator ...
AddDevice failed - it already be in use.
Try to delete the device...
DeleteDevice succeeded! Attempting AddDevice again...
Added 'MSRSim' to: Msr Microsoft Msr Simulator
Si la ruta de acceso COM1 ya está en uso y no se produce ningún otro error, el script genera una salida similar a este código.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Enumerating PosDevice...
Msr Microsoft Msr Simulator ENABLED
Msr Microsoft Msr Simulator ENABLED COM1
Keylock Microsoft Keylock Simulator ENABLED
Scanner Microsoft Scanner Simulator ENABLED
CashDrawer Microsoft CashDrawer Simulator ENABLED
CheckScanner Microsoft CheckScanner Simulator ENABLED
LineDisplay Microsoft LineDisplay Simulator ENABLED
PinPad Microsoft PinPad Simulator ENABLED
PosPrinter Microsoft PosPrinter Simulator ENABLED
PosKeyboard Microsoft PosKeyboard Simulator ENABLED
Add Device on COM1 and add name 'MSRSim' for MsrSimulator ...
AddDevice failed - it already be in use.
Try to delete the device...
DeleteDevice succeeded! Attempting AddDevice again...
Added 'MSRSim' to: Msr Microsoft Msr Simulator