How to get WMI object from a WMI object reference

ぐ巨炮叔叔 提交于 2019-12-24 00:03:57

问题


I'm using WMI to do some Citrix work, specifically to search for a particular user to log off. First I have to enumerate an object called MetaFrame_Session, which lists current sessions, then from that I have to retrieve an object called Citrix_User, which has the user name.

The Session object contains a reference to the User object, but I am not very familiar with WMI and I'm stumped as to how to get the actual object from the reference. Examples of how to do this in VBScript would be very helpful


回答1:


Seems you couldn't find an answer on this other forum, either, but the code to log off a Citrix session using WMI was kindly posted here as follows by Haydn Davies for one Citrix server:

' Logoff Disconnected Sessions
' If you want to logoff active sessions as well, change the query to include
' cActive
On Error Resume Next

Const cActive = 0
Const cDisconnected = 4
Const strComputer = "."

Set objWMICitrix = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\citrix")
Set colItems = objWMICitrix.ExecQuery ("Select * from Metaframe_Session Where sessionstate = " & cDisconnected)

For Each objItem in colItems
if (objItem.SessionID > 0) and (objItem.SessionID < 65530) then
objItem.Logoff
end if
Next

Set objWMICitrix = Nothing

See here for code on how to get a Citrix VirtualIP, since it might help as reference for techniques. Also, if you find the WMI is broken on the server, see here for repairing.




回答2:


you can do that with string manipulation, because "SessionUser" is a string

dim name
For Each objItem in colItems
name=left(Mid(objItem.SessionUser,InStr(objItem.SessionUser,"=")+2,20),InStr(Mid(objItem.SessionUser,InStr(objItem.SessionUser,"=")+2,20),",")-2)
 if (name="YOUR_SEARCH_NAME") and (objItem.SessionID < 65530) then
objItem.Logoff
end if


来源:https://stackoverflow.com/questions/9229997/how-to-get-wmi-object-from-a-wmi-object-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!