Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何从SQL Server存储的SID中获取Active Directory组名?_Sql Server_Security_Powershell_Active Directory_Sql Server 2000 - Fatal编程技术网

Sql server 如何从SQL Server存储的SID中获取Active Directory组名?

Sql server 如何从SQL Server存储的SID中获取Active Directory组名?,sql-server,security,powershell,active-directory,sql-server-2000,Sql Server,Security,Powershell,Active Directory,Sql Server 2000,这是我今天早上早些时候提出的一个问题的后续问题(.)按照提供的说明,我已设法在SQL Server 2000数据库中查询与广告组关联的SID。但是,SID如下所示: 0x0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567 S-1-5-21-1454471165-1004335555-1606985555-5555 如何获取SID引用的AD组的名称?我尝试过谷歌PowerShell脚本,但是,它们的大多数SID示例如下所示: 0x0

这是我今天早上早些时候提出的一个问题的后续问题(.)按照提供的说明,我已设法在SQL Server 2000数据库中查询与广告组关联的SID。但是,SID如下所示:

0x0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567
S-1-5-21-1454471165-1004335555-1606985555-5555
如何获取SID引用的AD组的名称?我尝试过谷歌PowerShell脚本,但是,它们的大多数SID示例如下所示:

0x0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567
S-1-5-21-1454471165-1004335555-1606985555-5555
显然,这看起来不像我从SQL Server得到的值。我如何做到这一点?

如果您使用的是针对SQL 2000的sqlps(SQL Powershell主机)(我已经在我的2000实例上进行了测试),那么您可以使用:

$query = @"
select sid from syslogins where isntgroup = 1
AND name = 'CONTOSO\mylogin'
"@

invoke-sqlcmd -ServerInstance "myserver" -Database master -Query $query | 
foreach {$SID = new-object security.principal.securityidentifier($_.SID,0); $SID.translate([system.security.principal.NTAccount]) }
对于没有sqlps的用户: 使用此联机C#shell do将单个sid格式化为文本

代码备份:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;

namespace Rextester
{
    public class Program
    {

        public static string ConvertByteToStringSid(Byte[] sidBytes)
        {

            StringBuilder strSid = new StringBuilder();
            strSid.Append("S-");

            // Add SID revision.
            strSid.Append(sidBytes[0].ToString());
            // Next six bytes are SID authority value.
            if (sidBytes[6] != 0 || sidBytes[5] != 0)
            {
                string strAuth = String.Format
                ("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}",
                (Int16)sidBytes[1],
                (Int16)sidBytes[2],
                (Int16)sidBytes[3],
                (Int16)sidBytes[4],
                (Int16)sidBytes[5],
                (Int16)sidBytes[6]);
                strSid.Append("-");
                strSid.Append(strAuth);
            }
            else
            {
                Int64 iVal = (Int32)(sidBytes[1]) +
                (Int32)(sidBytes[2] << 8) +
                (Int32)(sidBytes[3] << 16) +
                (Int32)(sidBytes[4] << 24);
                strSid.Append("-");
                strSid.Append(iVal.ToString());
            }

            // Get sub authority count...
            int iSubCount = Convert.ToInt32(sidBytes[7]);
            int idxAuth = 0;
            for (int i = 0; i < iSubCount; i++)
            {
                idxAuth = 8 + i * 4;

                if (idxAuth >= sidBytes.Length)
                {
                    Console.WriteLine("OK :old NT account");
                    return strSid.ToString();
                }

                UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
                strSid.Append("-");
                strSid.Append(iSubAuth.ToString());
            }
            return strSid.ToString();
        } 

        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine(
                ConvertByteToStringSid(
                    SoapHexBinary.Parse(
                        "0x01050000000000051500000079542007311FAE6D096510145E540300".Substring(2)
                    ).Value
                )
            );
        }
    }
}
//Rextester.Program.Main是代码的入口点。不要改变它。
//适用于Microsoft(R).NET Framework 4.5的编译器版本4.0.30319.17929
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text.RegularExpressions;
使用系统文本;
使用System.Runtime.Remoting.Metadata.W3cXsd2001;
名称空间测试仪
{
公共课程
{
公共静态字符串ConvertByteToStringSid(字节[]sidBytes)
{
StringBuilder strId=新的StringBuilder();
标准格式。附加(“S-”);
//添加SID修订。
追加(sidBytes[0].ToString());
//接下来的六个字节是SID权限值。
if(sidBytes[6]!=0 | | sidBytes[5]!=0)
{
string strAuth=string.Format
(“0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}”,
(Int16)sidBytes[1],
(Int16)sidBytes[2],
(Int16)sidBytes[3],
(Int16)sidBytes[4],
(Int16)sidBytes[5],
(Int16)sidBytes[6]);
标准符号。附加(“-”);
附加(strAuth);
}
其他的
{
Int64 iVal=(Int32)(sidBytes[1])+
(Int32)(sidBytes[2]