2013-02-26 101 views
1

我正在编写一个应用程序,它聚合了几个不同服务器上的所有事件日志条目。我可以通过传递MachineNameEventLog.GetEventLogs来获取事件日志。这通常会在某个阶段失败,因为用户不是该机器上的本地管理员,所以我想提前检查并跳到下一组服务器(如果情况如此)检查用户是否是外部计算机上的本地管理员

For Each svr As String In Servers 

    'TODO: check to see if they are a local administrator, else continue for 

    Dim logs As List(Of EventLog) = EventLog.GetEventLogs(svr).ToList 
    For Each log As EventLog In logs 
     LoadEachOSLogEntry(log) 
    Next 
Next 

大多数解决方案(如here)只会检查用户是否是当前正在执行的计算机上的管理员。

Dim user As WindowsIdentity = WindowsIdentity.GetCurrent() 
Dim principal As New WindowsPrincipal(user) 
Dim isAdmin As Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator) 
+1

旁白:处理偶尔的失败不是更容易吗?而不是对目标机器执行一些WMI调用并解析该用户的权限。 这只是我作为“我怎么能告诉我是否可以写入文件?”的同一类型的问题罢了。答案是“做到这一点,并处理错误” – hometoast 2013-02-26 18:44:33

+0

我认为在发生错误之前最好先处理容易避免的错误。如果我可以提前检查priveleges,我宁愿不承担处理一长串服务器中几乎所有服务器的异常的成本。另外,我现在拥有结构化的方式,可以将权限信息临时保存在服务器列表中,因此不需要进行两次调用即可检查。在catch块中很难做到这一点,除了权限之外的任何数量的问题都可能在手边。 – KyleMit 2013-02-26 19:01:00

+0

@Kyle:异常的成本比单个网络呼叫便宜几个数量级。 – 2013-02-27 18:40:05

回答

0

我将分享部分解决,但我不与它完全满意,所以如果任何人有什么好,我会高兴地接受他们的答案。

在任何机器上,以下函数将返回是否属于特定用户组的用户(在本例中为"Administrators")。

Imports System.DirectoryServices.AccountManagement 

Public Shared Function IsMemberOfGroup(userName As String, machineName As String, memberGroup as String) As Boolean 
    Dim isMember As Boolean = False 
    Using rootContext As New PrincipalContext(ContextType.Machine, machineName), _ 
      grp As GroupPrincipal = GroupPrincipal.FindByIdentity(rootContext, memberGroup), _ 
      usr As UserPrincipal = UserPrincipal.FindByIdentity(rootContext, IdentityType.SamAccountName, userName) 
     If grp IsNot Nothing AndAlso usr IsNot Nothing Then 
      ' Check if the user is a member of the group. 
      isMember = grp.GetMembers(True).Contains(usr) 
     Else 
      isMember = False 
     End If 
    End Using 
    Return isMember 
End Function 

的caviat是运行方法的用户必须以有权此信息PrincipalContext设置管理员。我希望应用程序能够确定运行该应用程序的用户是否是管理员。

,使这个超级有帮助的唯一方法是调用它,看看它是否想出了“拒绝访问”,类似于hometoast已经建议,但是这仍然没有手感超“干净”

相关问题