none
Trying to get forest information from my domain but getting an element not found RRS feed

  • Question

  • I'm trying to get trust information for my network.  I am trying to return information from ForestTrustRelatioinshipInformation, but I think I may be missing something.  If I run the below code I get an Element not found.  If I remove the TopLevelNames, ExcludedTopLevelNames, and TrustedDomainInformation it works.  I want to get that info even if it's empty.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.DirectoryServices.ActiveDirectory;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                string result = "\n";
                Forest forest = Forest.GetCurrentForest();
    
                TrustRelationshipInformationCollection forestTrust = forest.GetAllTrustRelationships();
    
                foreach (ForestTrustRelationshipInformation trust in forestTrust)
                {
                    result += $"TopLevelNames           : {trust.TopLevelNames} \n";
                    result += $"ExcludedTopLevelNames   : {trust.ExcludedTopLevelNames} \n";
                    result += $"SourceName             : {trust.SourceName} \n";
                    result += $"TargetName             : {trust.TargetName} \n";
                    result += $"Direction               : {trust.TrustDirection} \n";
                    result += $"Type                    : {trust.TrustDirection} \n";
    
                }
    
                Console.WriteLine(result);
            }
        }
    }
    

    Saturday, October 6, 2018 9:47 PM

All replies

  • Hi,

    I haven't tested your code but can you just check for null before appending to result. Something like below - 

     if(trust.TopLevelNames != null)
          result += $"TopLevelNames           : {trust.TopLevelNames.ToString()} \n";
    if(trust.ExcludedTopLevelNames != null)      
         result += $"ExcludedTopLevelNames   : {trust.ExcludedTopLevelNames.ToString()} \n";


    Thanks,
    Vivek Bansod
    Blog | MSDN | LinkedIn
     

    Monday, October 8, 2018 7:37 AM