通过计划任务运行C#脚本

通过计划任务运行C#脚本,c#,C#,我用c#开发了一个小脚本,用于查询SQL Server,并根据某些条件将计算机对象添加到某些Active Directory组中。当我使用具有从Active Directory组添加/删除对象所需权限的帐户运行脚本时,脚本工作正常 try { DirectoryEntry ent = new DirectoryEntry(bindString); ent.AuthenticationType = AuthenticationTypes.Secure

我用c#开发了一个小脚本,用于查询SQL Server,并根据某些条件将计算机对象添加到某些Active Directory组中。当我使用具有从Active Directory组添加/删除对象所需权限的帐户运行脚本时,脚本工作正常

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
当我尝试调度作业时,它会使用“系统”帐户从服务器自动运行,但不起作用,我会收到“拒绝访问”消息。我已更新绑定帐户以使用有效帐户的凭据,但我仍然存在相同的问题

> Error Message:
> *2020-01-13 18:32:30,984 [1] ERROR TestAD.clsActiveDirectory - Error occured when trying to add computerobject abcdefg-a10 to group. Error
> message: Access is denied.*
    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
唯一可行的方法是使用实际帐户作为帐户来运行计划任务,但是,问题是我们公司的策略不允许我们存储密码,所以我需要登录帐户才能运行此脚本

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
代码片段

                    de.Username = "testing@test.com";
                    de.Password = "xxxxxxxxx";
                    de.AuthenticationType = AuthenticationTypes.Secure;
                    de.AuthenticationType = AuthenticationTypes.Sealing;
                    de.AuthenticationType = AuthenticationTypes.Delegation;
                    de.Properties.Count.ToString();
                    ds.SearchRoot = de;
                    ds.Filter = "(&(objectClass=computer)(name=" + _myComputerName.ToString() + `"))";`
                    ds.PropertiesToLoad.Add("memberof");
                    ds.PropertiesToLoad.Add("distinguishedname");
                    ds.SizeLimit = 10;
                    ds.PageSize = 0;
                    ds.SearchScope = System.DirectoryServices.SearchScope.Subtree;
    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
我尝试添加一些“AuthenticationTypes”,看看这是否有所不同,但仍然相同

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
任何帮助都将不胜感激

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 

Thx.

您是否尝试过使用SQL Server代理?我的公司使用它们,而不是计划任务。他们可能不那么优雅,但这可能是一个很好的替代你的情况

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
  • 创建一个SQL Server代理,该代理使用或不使用参数调用可执行文件。
    • 如果无法从宿主操作系统调用可执行文件,则可以调用网络上的SSIS包来为您调用可执行文件

如果您需要更多详细信息,请告诉我。

我发现了这个问题,最后非常直截了当。
    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
Active Directory流如下所示 -使用我的特殊帐户绑定到active directory并搜索计算机对象和 验证是否需要将其添加到Active Directory组 -如果需要添加,请第二次绑定到Active Directory组并添加计算机 对象==>使用计划任务或在“系统”上下文下运行时,此项失败

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
失败原因:当我第二次绑定时,我没有指定任何凭据,所以 使用默认凭据(系统)如果我运行脚本我的帐户有足够的 将计算机对象添加到组的权限

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
我更新了第二个绑定的代码以包含绑定凭据,现在它的工作方式是 预料之中

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
我希望这将有助于解决类似问题的其他人

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
旧代码

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    }
    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 
新代码

    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.AuthenticationType = AuthenticationTypes.Secure;
        ent.AuthenticationType = AuthenticationTypes.Sealing;
        ent.AuthenticationType = AuthenticationTypes.Delegation;
        ent.Username = "test123@test.com";
        ent.Password = "test123";
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    } 

我建议查看托管服务帐户,以便您可以在域上创建帐户。嗨,Joe,我使用的帐户是域帐户,正如我已经解释过的,使用该帐户确实有效,只有在我尝试在计划任务中使用此帐户时,才会出现问题。我想必须以某种方式向绑定和/或上下文中添加一些内容,但不确定要添加什么,因此出现了一个问题。@DenisGendera您可以指定哪个用户将在计划程序中运行您的任务。托管服务帐户使管理帐户更容易,并且不需要在任何地方硬编码密码。当你说计划任务时,我不确定你指的是SQL代理还是Windows任务。我知道,对于SQL代理作业,您可以使用该帐户设置代理,使其不会使用SYSTEM或NT\Service帐户执行,这是默认帐户,具体取决于SQL代理服务使用的登录名。我指的是Windows计划任务。我目前已经配置了任务,但是使用域帐户,它在测试PC上运行,但是它不能长期持续。我应该可以使用不同的帐户绑定到active directory,对于需要检查active directory或其他需要查询active directory的桌面应用程序(SCCM)的IIS网站也是这样做的。我仍然想了解为什么绑定不能按预期工作。简单测试如果我运行psexec.exe-I-s cmd.exe(系统上下文),并且我运行脚本,它也会失败。我能够进行身份验证,我已经记录了,但当我尝试将计算机添加到组时,我会收到相同的错误消息。我正在运行WireShark跟踪,看到绑定发生了它是针对域控制器的简单绑定,当我在我的帐户下运行脚本时也发生了相同的绑定。