简体   繁体   English

c#如何获取AD中Computer的objectguid和objectid

[英]How to get the objectguid and objectsid of Computer in AD using c#

based on List all computers in active directory基于列出活动目录中的所有计算机

im Trying to get the objectGUID and objectSID of a computer in my ad domain.我正在尝试获取我的广告域中计算机的 objectGUID 和 objectSID。

So I added所以我加了

                mySearcher.PropertiesToLoad.Add("objectGUID");
                mySearcher.PropertiesToLoad.Add("objectSID");

and try to receive the informations with:并尝试通过以下方式接收信息:

                            string computerGUID = (string)resEnt.Properties["objectGUID"][0].ToString();
                            string computerSID = (string)resEnt.Properties["objectSID"][0].ToString();

but just getting 'System.Byte[]' back.但只是让 'System.Byte[]' 回来。 How can I get the "real" data?我怎样才能得到“真实”的数据?

thank you谢谢你

You can do this by using running the following PowerShell commands from C#:您可以通过从 C# 运行以下 PowerShell 命令来执行此操作:

Get-ADUser USERNAME -Properties * | Get-ADUser USERNAME -Properties * | Select SamaccountName,ObjectSid,ObjectGUID选择 SamaccountName、ObjectSid、ObjectGUID

After you apply the filter, you use FindOne() that retrieves the first entry based on the filter应用过滤器后,您使用FindOne()根据过滤器检索第一个条目

mySearcher.PropertiesToLoad.Add("objectGUID");
mySearcher.PropertiesToLoad.Add("objectSID");
SearchResult searchResult = mySearcher.FindOne(); //FindAll() can return a collection

var objectGUID = searchResult.GetDirectoryEntry().Properties["objectGUID"].Value;

var computerGUID = searchResult.GetDirectoryEntry().Properties["objectSID"].Value;

OR或者


 mySearcher.PropertiesToLoad.Add("objectGUID");
 mySearcher.PropertiesToLoad.Add("objectSID");

 SearchResultCollection results;

 results = mySearcher.FindAll();

string sidStringValue="";
string guidStringValue="";

    foreach (var result in results)
    {

                  //reset SID

                  byte[] SID = null;

                  if (result.Properties[“objectSid”].Count > 0)

                  {

                      SID = (byte[]) result.Properties[“objectSid”][0];

                  }

                  sidStringValue = GetSidString(SID); 

             }//

GetSidString Is a part of Unmanaged code so you'll have to P/Invoke GetSidString 是非托管代码的一部分,因此您必须 P/Invoke


[DllImport(“advapi32”, CharSet = CharSet.Auto, SetLastError = true)]

static extern bool ConvertSidToStringSid([MarshalAs(UnmanagedType.LPArray)] byte[] pSID, out IntPtr ptrSid);


public static string GetSidString(byte[] sid)
{

            IntPtr ptrSid;

            string sidString;

            if (!ConvertSidToStringSid(sid, out ptrSid))

                throw new System.ComponentModel.Win32Exception();

            try

            {

                sidString = Marshal.PtrToStringAuto(ptrSid);

            }

            finally

            {

                Marshal.FreeHGlobal(ptrSid);

            }

            return sidString;

}

Note :注意

Second approach emphasizes on objectSid you'll have to modify it accordingly for objectGUID第二种方法强调objectSid您必须为objectGUID相应地修改它

References:参考:

https://philippsen.wordpress.com/2010/12/07/retrieving-user-sid-using-directoryentry-and-directorysearcher/ https://philippsen.wordpress.com/2010/12/07/retrieving-user-sid-using-directoryentry-and-directorysearcher/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM