Exemple #1
0
		public MsDNS2012()
		{
			// Create PowerShell helper
			ps = new PowerShellHelper();
			if( !this.IsInstalled() )
				return;
		}
Exemple #2
0
        /* public enum eReplicationScope: byte
         * {
         *      Custom, Domain, Forest, Legacy
         * } */

        /// <summary></summary>
        /// <param name="ps"></param>
        /// <param name="zoneName"></param>
        /// <param name="replicationScope">Specifies a partition on which to store an Active Directory-integrated zone.</param>
        /// <returns></returns>
        public static void Add_DnsServerPrimaryZone(this PowerShellHelper ps, string zoneName, string[] secondaryServers)
        {
            Log.WriteStart("Add_DnsServerPrimaryZone {0} {{{1}}}", zoneName, String.Join(", ", secondaryServers));

            // Add-DnsServerPrimaryZone -Name zzz.com -ZoneFile zzz.com.dns
            var cmd = new Command("Add-DnsServerPrimaryZone");

            cmd.addParam("Name", zoneName);
            cmd.addParam("ZoneFile", zoneName + ".dns");
            ps.RunPipeline(cmd);

            // Set-DnsServerPrimaryZone -Name zzz.com -SecureSecondaries ... -Notify ... Servers ..
            cmd = new Command("Set-DnsServerPrimaryZone");
            cmd.addParam("Name", zoneName);

            if (secondaryServers == null || secondaryServers.Length == 0)
            {
                // transfers are not allowed
                // inParams2[ "SecureSecondaries" ] = 3;
                // inParams2[ "Notify" ] = 0;
                cmd.addParam("SecureSecondaries", "NoTransfer");
                cmd.addParam("Notify", "NoNotify");
            }
            else if (secondaryServers.Length == 1 && secondaryServers[0] == "*")
            {
                // allowed transfer from all servers
                // inParams2[ "SecureSecondaries" ] = 0;
                // inParams2[ "Notify" ] = 1;
                cmd.addParam("SecureSecondaries", "TransferAnyServer");
                cmd.addParam("Notify", "Notify");
            }
            else
            {
                // allowed transfer from specified servers
                // inParams2[ "SecureSecondaries" ] = 2;
                // inParams2[ "SecondaryServers" ] = secondaryServers;
                // inParams2[ "NotifyServers" ] = secondaryServers;
                // inParams2[ "Notify" ] = 2;
                cmd.addParam("SecureSecondaries", "TransferToSecureServers");
                cmd.addParam("Notify", "NotifyServers");
                cmd.addParam("SecondaryServers", secondaryServers);
                cmd.addParam("NotifyServers", secondaryServers);
            }
            ps.RunPipeline(cmd);
            Log.WriteEnd("Add_DnsServerPrimaryZone");
        }
Exemple #3
0
        public static void Remove_DnsServerResourceRecords(this PowerShellHelper ps, string zoneName, string type)
        {
            var cmd = new Command("Get-DnsServerResourceRecord");

            cmd.addParam("ZoneName", zoneName);
            cmd.addParam("RRType", type);
            Collection <PSObject> resourceRecords = ps.RunPipeline(cmd);

            foreach (PSObject resourceRecord in resourceRecords)
            {
                cmd = new Command("Remove-DnsServerResourceRecord");
                cmd.addParam("ZoneName", zoneName);
                cmd.addParam("InputObject", resourceRecord);

                cmd.addParam("Force");
                ps.RunPipeline(cmd);
            }
        }
Exemple #4
0
        /// <summary>Get-DnsServerZone | Select-Object -Property ZoneName</summary>
        /// <remarks>Only primary DNS zones are returned</remarks>
        /// <returns>Array of zone names</returns>
        public static string[] Get_DnsServerZone_Names(this PowerShellHelper ps)
        {
            var allZones = ps.RunPipeline(new Command("Get-DnsServerZone"),
                                          where ("IsAutoCreated", false));

            string[] res = allZones
                           .Select(pso => new
            {
                name = (string)pso.Properties["ZoneName"].Value,
                type = (string)pso.Properties["ZoneType"].Value
            })
                           .Where(obj => obj.type == "Primary")
                           .Select(obj => obj.name)
                           .ToArray();

            Log.WriteInfo("Get_DnsServerZone_Names: {{{0}}}", String.Join(", ", res));
            return(res);
        }
Exemple #5
0
        /// <summary>Test-DnsServer -IPAddress 127.0.0.1</summary>
        /// <param name="ps">PowerShell host to use</param>
        /// <returns>true if localhost is an MS DNS server</returns>
        public static bool Test_DnsServer(this PowerShellHelper ps)
        {
            if (null == ps)
            {
                throw new ArgumentNullException("ps");
            }

            var cmd = new Command("Test-DnsServer")
                      .addParam("IPAddress", IPAddress.Loopback);

            PSObject res = ps.RunPipeline(cmd).FirstOrDefault();

            if (null == res || null == res.Properties)
            {
                return(false);
            }
            PSPropertyInfo p = res.Properties["Result"];

            if (null == p || null == p.Value)
            {
                return(false);
            }
            return(p.Value.ToString() == "Success");
        }
Exemple #6
0
        public static void Update_DnsServerResourceRecordSOA(this PowerShellHelper ps, string zoneName,
                                                             TimeSpan ExpireLimit, TimeSpan MinimumTimeToLive, string PrimaryServer,
                                                             TimeSpan RefreshInterval, string ResponsiblePerson, TimeSpan RetryDelay,
                                                             string PSComputerName)
        {
            var cmd = new Command("Get-DnsServerResourceRecord");

            cmd.addParam("ZoneName", zoneName);
            cmd.addParam("RRType", "SOA");
            Collection <PSObject> soaRecords = ps.RunPipeline(cmd);

            if (soaRecords.Count < 1)
            {
                return;
            }

            PSObject oldSOARecord = soaRecords[0];
            PSObject newSOARecord = oldSOARecord.Copy();

            CimInstance recordData = newSOARecord.Properties["RecordData"].Value as CimInstance;

            if (recordData == null)
            {
                return;
            }

            if (ExpireLimit != null)
            {
                recordData.CimInstanceProperties["ExpireLimit"].Value = ExpireLimit;
            }

            if (MinimumTimeToLive != null)
            {
                recordData.CimInstanceProperties["MinimumTimeToLive"].Value = MinimumTimeToLive;
            }

            if (PrimaryServer != null)
            {
                recordData.CimInstanceProperties["PrimaryServer"].Value = PrimaryServer;
            }

            if (RefreshInterval != null)
            {
                recordData.CimInstanceProperties["RefreshInterval"].Value = RefreshInterval;
            }

            if (ResponsiblePerson != null)
            {
                recordData.CimInstanceProperties["ResponsiblePerson"].Value = ResponsiblePerson;
            }

            if (RetryDelay != null)
            {
                recordData.CimInstanceProperties["RetryDelay"].Value = RetryDelay;
            }

            if (PSComputerName != null)
            {
                recordData.CimInstanceProperties["PSComputerName"].Value = PSComputerName;
            }

            UInt32 serialNumber = (UInt32)recordData.CimInstanceProperties["SerialNumber"].Value;

            // update record's serial number
            string sn        = serialNumber.ToString();
            string todayDate = DateTime.Now.ToString("yyyyMMdd");

            if (sn.Length < 10 || !sn.StartsWith(todayDate))
            {
                // build a new serial number
                sn           = todayDate + "01";
                serialNumber = UInt32.Parse(sn);
            }
            else
            {
                // just increment serial number
                serialNumber += 1;
            }

            recordData.CimInstanceProperties["SerialNumber"].Value = serialNumber;

            cmd = new Command("Set-DnsServerResourceRecord");
            cmd.addParam("NewInputObject", newSOARecord);
            cmd.addParam("OldInputObject", oldSOARecord);
            cmd.addParam("Zone", zoneName);
            ps.RunPipeline(cmd);
        }
Exemple #7
0
        public static void Remove_DnsServerResourceRecord(this PowerShellHelper ps, string zoneName, DnsRecord record)
        {
            string type;

            if (!RecordTypes.rrTypeFromRecord.TryGetValue(record.RecordType, out type))
            {
                throw new Exception("Unknown record type");
            }

            string Name = record.RecordName;

            if (String.IsNullOrEmpty(Name))
            {
                Name = "@";
            }

            var cmd = new Command("Get-DnsServerResourceRecord");

            cmd.addParam("ZoneName", zoneName);
            cmd.addParam("Name", Name);
            cmd.addParam("RRType", type);
            Collection <PSObject> resourceRecords = ps.RunPipeline(cmd);

            object inputObject = null;

            foreach (PSObject resourceRecord in resourceRecords)
            {
                DnsRecord dnsResourceRecord = resourceRecord.asDnsRecord(zoneName);

                bool found = false;

                switch (dnsResourceRecord.RecordType)
                {
                case DnsRecordType.A:
                case DnsRecordType.AAAA:
                case DnsRecordType.CNAME:
                case DnsRecordType.NS:
                case DnsRecordType.TXT:
                    found = dnsResourceRecord.RecordData == record.RecordData;
                    break;

                case DnsRecordType.SOA:
                    found = true;
                    break;

                case DnsRecordType.MX:
                    found = (dnsResourceRecord.RecordData == record.RecordData) && (dnsResourceRecord.MxPriority == record.MxPriority);
                    break;

                case DnsRecordType.SRV:
                    found = (dnsResourceRecord.RecordData == record.RecordData) &&
                            (dnsResourceRecord.SrvPriority == record.SrvPriority) &&
                            (dnsResourceRecord.SrvWeight == record.SrvWeight) &&
                            (dnsResourceRecord.SrvPort == record.SrvPort);
                    break;
                }

                if (found)
                {
                    inputObject = resourceRecord;
                    break;
                }
            }

            cmd = new Command("Remove-DnsServerResourceRecord");
            cmd.addParam("ZoneName", zoneName);
            cmd.addParam("InputObject", inputObject);

            cmd.addParam("Force");
            ps.RunPipeline(cmd);
        }