none
The Term 'Get-ADFineGrainedPasswordPolicySubject' not recognised as commandlet. While executing in C# from powershell RRS feed

  • Allgemeine Diskussion

  • I have strange problem. Literally no idea what to do in this case.

    While I can create fine-grained policies and apply those policies using 'Add-ADFineGrainedPasswordPolicySubject' commandlet without any issue. But while these commandlets are referred from C# program, there it started to complain about Get-ADFineGrainedPasswordPolicySubject and Add-ADFineGrainedPasswordPolicySubject as `'The Term Get-ADFineGrainedPasswordPolicySubject' as not recognised as commandlet or function.

    I literally have no idea. I already imported the module 'Active Directory' but stil throws exception all the time.

    Didn't find any solution somewhere over internet and kinda stuck.

    Please refer following source code for reference.

    using (var powerShell = PowerShell.Create())
    {
          powerShell.Commands.AddCommand("Import-Module").AddArgument("ActiveDirectory");
          powerShell.Invoke();
          powerShell.Commands.Clear();
    
          powerShell.Commands.AddCommand(new Command("Get-ADDefaultDomainPasswordPolicy", true));
          ICollection<PSObject> defaultDomainPasswordPolicyOutput = powerShell.Invoke();
    
          if (defaultDomainPasswordPolicyOutput != null)
          {
              defaultDomainPasswordPolicyOutput.ToList().ForEach(policy =>
              {                           
                    var newFineGrainedpolicyCmdLet = $"New-ADFineGrainedPasswordPolicy -Name preppedPolicy -Precedence 1";
    
                    powerShell.Commands.AddCommand(new Command(newFineGrainedpolicyCmdLet, true));
                    powerShell.Invoke();
    
                    Console.WriteLine("Error Count : {0}", powerShell.Streams.Error.Count);                                                               
    
                     var fineGrainedPolicySubjCmdLetASked = $"Get-ADFineGrainedPasswordPolicySubject -Identity preppedPolicy";
    
                     powerShell.Commands.AddCommand(fineGrainedPolicySubjCmdLetASked);
                     powerShell.Invoke();                               
    
                     var fineGrainedPolicySubjCmdLet = $"Add-ADFineGrainedPasswordPolicySubject -Identity preppedPolicy -Subjects S-AHPOneServer";
    
                     powerShell.Commands.AddCommand(fineGrainedPolicySubjCmdLet);
                     powerShell.Invoke();                                                            
               });
            }
            else
            {
                Console.WriteLine("Policies dont found.");
                Console.ReadLine();
             }
       }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Inside exception area : ");
        Console.WriteLine("Exceptions {0}", ex);
        Console.ReadLine();
    
    }

    This program can be run at Windows Server 2012 R2 wit .NET environment and there it throws above exceptions.

    Any idea how to come out from this?

    Freitag, 12. März 2021 07:01

Alle Antworten