使用 VBScript 管理裝置 (POS for .NET v1.14 SDK 文件)
使用本節中所述的 WMI API,可以使用受控程式碼或指令碼,來管理裝置。 POSDM.EXE 是這個 API 的命令列介面。 此 VBScript 範例會執行下列動作:
使用 WMI 方法 ExecQuery,擷取所安裝的 PosDevice 物件清單。 有了此服務物件的清單後,指令碼會顯示其類型、名稱、對應路徑,以及是啟用或停用狀態。 這類似於執行下列命令:
PosDM.exe LISTDEVICES
然後,會使用 AddDevice 方法,嘗試將路徑 COM1 指派給安裝的服務物件 Microsoft Msr 模擬器。 這相當於執行:
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
若要執行範例
服務物件 Microsoft Msr 模擬器已隨 SDK 一起安裝。 請確定它已安裝在您將用來執行範例的電腦上。
將此指令碼複製到 PosDMSample.vbs 檔案
使用下列命令列,執行指令碼:
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