Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Usando a API WMI documentada nesta seção, é possível gerenciar dispositivos usando código gerenciado ou scripts. POSDM.EXE é uma interface de linha de comando para essa API. Este exemplo de VBScript faz o seguinte:
Ele usa o método WMI ExecQuery para recuperar uma lista de objetos PosDevice instalados. Com essa lista de Objetos de Serviço, o script exibe seu tipo, nome, caminho correspondente e seu status habilitado ou desabilitado. Isso é análogo à execução do seguinte comando:
PosDM.exe LISTDEVICESEm seguida, ele tenta atribuir o caminho COM1 para o objeto de serviço instalado, Microsoft Msr Simulator usando o método AddDevice . Isso equivale a executar:
PosDM.exe ADDDEVICE COM1 /SONAME:Microsoft Msr SimulatorSe o método AddDevice falhar, o script deteta o erro e assume que COM1 pode já ter sido adicionado ao dispositivo e, portanto, tenta excluí-lo chamando DeleteDevice. Isso equivale a executar:
PosDM.exe DELETEDEVICE COM1Se o método AddDevice tiver falhado anteriormente, o script tentará chamar AddDevice novamente. O programa é encerrado se o método falhar.
Finalmente, o exemplo tenta adicionar o nome lógico MSRSim a este objeto de serviço chamando AddName. Isso equivale a executar:
PosDM.exe ADDNAME MSRSim /SONAME:"Microsoft Msr Simulator"
É possível ver os resultados deste exemplo executando:
PosDM.exe LISTDEVICES
And
PosDM.exe LISTNAMES
Para executar o exemplo
O objeto de serviço Microsoft Msr Simulator foi instalado com o SDK. Certifique-se de que ele está instalado no computador que você usará para executar o exemplo.
Copie este script para um arquivo PosDMSample.vbs
Execute o script com a seguinte linha de comando:
CScript //U PosDMSample.vbs
Exemplo
'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
Se o caminho COM1 não tiver sido atribuído a um dispositivo, o exemplo produzirá uma saída semelhante 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
Se o caminho COM1 já estiver em uso e nenhum outro erro ocorrer, o script produzirá uma saída semelhante a esse 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