使用 VBScript 管理设备(POS for .NET v1.14 SDK 文档)

使用本部分中所述的 WMI API,可以通过托管代码或脚本管理设备。 POSDM.EXE 是此 API 的命令行接口。 此 VBScript 示例将执行以下操作:

  • 它使用 WMI 方法 ExecQuery 检索已安装的 PosDevice 对象的列表。 在此服务对象列表中,脚本会显示其类型、名称、相应路径及其启用或禁用状态。 这类似于运行以下命令:

    PosDM.exe LISTDEVICES

  • 然后,它尝试使用 AddDevice 方法将路径 COM1 分配给已安装的服务对象 Microsoft Msr Simulator。 这相当于运行以下代码:

    PosDM.exe ADDDEVICE COM1 /SONAME:Microsoft Msr Simulator

  • 如果 AddDevice 方法失败,脚本将捕获错误,并假定 COM1 可能已添加到设备,因此尝试通过调用 DeleteDevice 将其删除。 这相当于运行以下代码:

    PosDM.exe DELETEDEVICE COM1

  • 如果之前 AddDevice 方法曾失败,脚本会尝试再次调用 AddDevice。 如果方法失败,程序将退出。

  • 最后,该示例尝试通过调用 AddName 将逻辑名称 MSRSim 添加到此服务对象。 这相当于运行以下代码:

    PosDM.exe ADDNAME MSRSim /SONAME:"Microsoft Msr Simulator"

可以通过运行以下命令来查看此示例的结果:

PosDM.exe LISTDEVICES

PosDM.exe LISTNAMES

运行示例

  1. 服务对象 Microsoft Msr Simulator 已随 SDK 一起安装。 请确保它安装在将用于运行示例的计算机上。

  2. 将此脚本复制到文件 PosDMSample.vbs

  3. 使用以下命令行执行该脚本:

    CScript //U PosDMSample.vbs

示例

'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

如果路径 COM1 尚未分配给设备,示例将生成类似于以下代码的输出。

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

如果路径 COM1 已在使用中,并且没有发生其他错误,脚本将生成类似于以下代码的输出。

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

另请参阅

其他资源