Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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();
            PSPropertyInfo p   = res.Properties["Result"];

            if (null == res || null == res.Properties)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 4
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("ZoneName", zoneName);
            ps.RunPipeline(cmd);
        }
Ejemplo n.º 5
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);
        }