Bagikan melalui


Menggunakan VBScript untuk Mengelola Perangkat (POS untuk Dokumentasi .NET v1.14 SDK)

Menggunakan API WMI yang didokumenkan di bagian ini, dimungkinkan untuk mengelola perangkat menggunakan kode terkelola atau pembuatan skrip. POSDM.EXE adalah antarmuka baris perintah untuk API ini. Sampel VBScript ini melakukan hal berikut:

  • Ini menggunakan metode WMI ExecQuery untuk mengambil daftar objek PosDevice yang diinstal. Dengan daftar Objek Layanan ini, skrip menampilkan jenis, nama, jalur terkait, dan statusnya yang diaktifkan atau dinonaktifkan. Ini dianalogikan untuk menjalankan perintah berikut:

    PosDM.exe LISTDEVICES

  • Kemudian mencoba menetapkan jalur COM1 ke Objek Layanan yang diinstal, Microsoft Msr Simulator menggunakan metode AddDevice . Ini setara dengan menjalankan:

    PosDM.exe ADDDEVICE COM1 /SONAME:Microsoft Msr Simulator

  • Jika metode AddDevice gagal, skrip menangkap kesalahan dan mengasumsikan bahwa COM1 mungkin telah ditambahkan ke perangkat dan oleh karena itu mencoba menghapusnya dengan memanggil DeleteDevice. Ini setara dengan menjalankan:

    PosDM.exe DELETEDEVICE COM1

  • Jika metode AddDevice sebelumnya gagal, skrip kemudian mencoba memanggil AddDevice lagi. Program keluar jika metode gagal.

  • Terakhir, sampel mencoba menambahkan nama logis MSRSim ke Objek Layanan ini dengan memanggil AddName. Ini setara dengan menjalankan:

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

Dimungkinkan untuk melihat hasil sampel ini dengan menjalankan:

PosDM.exe LISTDEVICES

Dan

PosDM.exe LISTNAMES

Untuk menjalankan sampel

  1. Microsoft Msr Simulator Objek Layanan diinstal dengan SDK. Pastikan bahwa itu diinstal pada komputer yang akan Anda gunakan untuk menjalankan sampel.

  2. Salin skrip ini ke file PosDMSample.vbs

  3. Jalankan skrip dengan baris perintah berikut:

    CScript //U PosDMSample.vbs

Contoh

'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

Jika jalur COM1 belum ditetapkan ke perangkat, sampel menghasilkan output yang mirip dengan kode ini.

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

Jika jalur COM1 sudah digunakan dan tidak ada kesalahan lain yang terjadi, skrip menghasilkan output yang terlihat seperti kode ini.

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

Lihat Juga

Sumber Daya Lain