快速得知目前到底是"誰"使用了 RMS 服務的 vbs 範例


在 RMS 目前的管理報表中(如下圖),只有多少個使用者使的總數量,並沒有明確列出是那些使用者使用過 RMS 的服務。

image

要如何得知這些使用者的清單呢?這個問題有很多的朋友問過我,

這兩天看到同事 Jason Tyler 寫了一段 VBS 的範例,讓我們可以非常的簡單就找出有使用過 RMS 的服務。

 

VBS 範例:

‘+++++++++++++++++++++++++++++++++++++++++++++++++++

‘ rmsusers.vbs – A simple script to match up the SDDL sids in the RMS

‘ dbase to user accounts

‘+++++++++++++++++++++++++++++++++++++++++++++++++++

Option Explicit

Dim strComputer, strRoot, strDbase, strSQL, strConn
Dim objRoot
Dim conn,ldpconn
Dim rs,rsUser
Dim objArgs

Set objArgs = Wscript.Arguments

If objArgs.Count = 0 Then
ShowUsage
Wscript.Quit
End if

strComputer = objArgs(0)

Set objRoot = GetObject(“LDAP://RootDSE“)
strRoot = objRoot.Get(“defaultnamingcontext”)
Set objRoot = Nothing

Set conn = CreateObject(“ADODB.Connection”)
Set rs = CreateObject(“ADODB.RecordSet”)

With rs
.CursorLocation=3
.CursorType=0
End With

With conn
strDbase = “master”
GetConn()
.open strConn    
strSQL = “Select Name From SysDatabases Where” & _
                       ” Name like ‘DRMS_Config%'”
rs.Open strSQL,conn
End With

If rs.RecordCount > 0 Then
strDbase = rs.Fields(“name”).Value
Wscript.Echo “++++Found Database:” & strDbase & “.++++” & vbcrlf & vbcrlf
rs.close
conn.close
strSQL = “Select s_Sid from UD_WindowsAuthIdentities”
GetConn()
conn.open strConn
rs.open strSQL,conn
If rs.RecordCount > 0 Then
  Wscript.Echo “There were ” & rs.RecordCount & ” users found.”
  Do until rs.EOF
   ResolveName(rs.Fields(“s_Sid”).Value)
   rs.MoveNext
  Loop
Else
  Wscript.Echo “No users found in UD_WindowsAuthIdentities Table”
  Cleanup
  Wscript.Quit
End If
Else
Wcript.Echo “Could not find RMS Configuration Dbase”
CleanUp
Wscript.Quit
End if

‘**********************************************************

‘GetConn – Probably not needed. I thought I’d be making more connections

‘**********************************************************

Sub GetConn()
strConn = “Provider=SQLOLEDB;Data Source=” & strComputer & “;” & _
“Trusted_Connection=Yes;Initial Catalog=” & strDbase
End Sub

‘**********************************************************

‘Resolve Name – Will spit out the user names based on SDDL sid

‘**********************************************************

Sub ResolveName (strSID)
Set ldpconn = CreateObject(“ADODB.Connection”)
With ldpconn
  .Provider = “ADSDSOObject”
  .Open “ADSProvider”
  strSQL = “<GC://” & strRoot & “>;(&(objectclass=user)(objectSID=” & strSID &”));name,mail;subtree”
  Set rsUser = .Execute(strSQL)
  If rsUser.RecordCount > 0 Then
Wscript.Echo “**-” & rsUser.Fields(“name”).Value & _
        “-” & rsUser.Fields(“mail”).Value
  Else
Wscript.Echo “Unresolved: ” & strSID
  End If
  rsUser.Close
  ldpconn.Close
  Set rsuser = nothing
  Set ldpconn = nothing
End with
End Sub

‘*************************************

‘CleanUp – If we bail – try to clean up

‘*************************************

Sub CleanUp
rs.close
set rs = Nothing
conn.close
set conn=nothing
End Sub

‘*************************************

‘ShowUsage – How to use the script

‘*************************************

Sub ShowUsage
Wscript.Echo “Usage:>cscript rmsusers.vbs rms_sql_server”
Wscript.Echo “Usage:>cscript rmsuser.vbs rms_sql_server\instance”
End Sub

 

使用方式:

1. 將上面的 VBS 複製並貼到 Notepad,儲檔為 rmsusers.vbs

2. 使用命令提示字元,輸入 cscript.exe rmsusers.vbs RMS_SQL_Server

 

執行結果:

image

 

原文:http://blogs.technet.com/rmssupp/archive/2007/12/31/how-to-tell-who-is-using-rms-in-your-environment-quickly.aspx

Skip to main content