How to resolve latest DNS CName results via powershell - powershell

I am trying to resolve the latest DNS CName using the following powershell script. However due to the DNS server caching, I am getting the cached host name which is not the latest CName.
Is there anyway to avoid this and get the latest results? (as in digwebinterface.com)
Afterwards I am invoking a third party DNS management API, which will modify or create DNS CName mapping. For this I need the latest dns data.
#resolve the dns host name
$resolvedCName = Resolve-DnsName -Name $vanityHostName -DnsOnly -Type CNAME |
Select-Object -First 1 -Property Name,NameHost
write-host $resolvedCName.NameHost

This is not a PowerShell issue or error. Its an environment condition.
Why are you not just clearing the cache as part of what you are doing?
Are you saying, that the DnsClientCache cmdlets or Ipconfig -FlushDNS are not giving you what you are after?
The Clear-DnsClientCache cmdlet deletes all the contents of the DNS client cache. Running this cmdlet is equivalent to running ipconfig /flushdns.
Get-Command -Name Clear-DnsClientCache | Format-Table -AutoSize
<#
CommandType Name Version Source
----------- ---- ------- ------
Function Clear-DnsClientCache 1.0.0.0 DnsClient
#>
# get function / cmdlet details
Get-Command -Name Clear-DnsClientCache -Syntax
(Get-Command -Name Clear-DnsClientCache).Parameters.Keys
Get-help -Name Clear-DnsClientCache -Full
Get-help -Name Clear-DnsClientCache -Online
Get-help -Name Clear-DnsClientCache -Examples
ipconfig /?
<#
USAGE:
ipconfig [/allcompartments] [/? | /all |
/renew [adapter] | /release [adapter] |
/renew6 [adapter] | /release6 [adapter] |
/flushdns | /displaydns | /registerdns |
/showclassid adapter |
/setclassid adapter [classid] |
/showclassid6 adapter |
/setclassid6 adapter [classid] ]
where
adapter Connection name
(wildcard characters * and ? allowed, see examples)
Options:
...
/flushdns Purges the DNS Resolver cache.
/registerdns Refreshes all DHCP leases and re-registers DNS names
…
#>

Apologies for the late response, finally I decided to go with the DNS solution (Ansible Tower) API to get the CName for the hostname.
Powershell Resolve-DnsName -DnsOnly take some time to propagate the DNS. However my release pipeline expect the propagation to be done within few minutes. It's actually doing couple of changes to the host name.
The use of -DnsOnly switch will resolves the query using only the DNS protocol. Even this takes some time to resolve latest DNS results. Therefore I had to go with the API and it worked without any latency issues.

For recurring DNS testing that tou need to have change when the responsible nMe server changez the address, query the responsible namd server directly.
Clearing the client side cache will not effect Resolve-DNSName ] as it id an NSLookup equivalent and so queries the remote server directly instead of using the host's normal resolver mechanism.
So, you should locate the name servers for $VanityHostName uding a half dozen methods, such as arin or dnstoolbox, then specify the name servers for the site as the server in your query as below (4.2.2.2 is a placeholder for the address of the sites' s name server.)
#resolve the dns host name
$resolvedCName = Resolve-DnsName -Name $vanityHostName -DnsOnly -Type CNAME -Server '4.2.2.2' | Select-Object -First 1 -Property Name,NameHost
write-host $resolvedCName.NameHost

Related

How do you create a new Microsoft DNS zone with powershell that loads from a DNS file?

I'm trying to use Powershell to create some DNS zones on a Microsoft DNS server. We have the DNS files already created and fully populated. The idea is that we place those DNS files in the \dns directory, and then run the script to import them. The problem I am having is that when I run the command below, it overwrites the populated DNS file with a new (empty) zone/file.
Add-DnsServerPrimaryZone -Name abc.com -ZoneFile "abc.com.dns"
How do I do this, please?
If you move the file and create the DNS Zone using
Add-DnsServerPrimaryZone -Name abc.com -ZoneFile "abc.com.dns"
can you then add the A Records using
$entries = Import-Csv <filename with DNS Records>
foreach($entry in $entries){
Add-DnsServerResourceRecordA -Name $entry.name -ZoneName abc.com -IPv4Address $entry.ip -ComputerName $entry.HostName
}
There are also a bunch of other ADD commands for other record types, so you maybe able to build a script to identify the type from the input depending on the contents
https://learn.microsoft.com/en-us/powershell/module/dnsserver/add-dnsserverresourcerecorda?view=win10-ps
You can use dnscmd to easily accomplish that.
dnscmd /zoneadd test.com /primary /file test.com.dns /load
The zone file has to be in C:\Windows\System32\dns.
As far as I know there is no specific Powershell module for importing DNS zones. You can verify that with Get-Command -Module DnsServer -Noun *Zone*.

List all services in PowerShell

There are some services in Windows (such as http and USBStor) which are not listed when you view Services, or when running the Get-Service cmdlet. What is the simplest way to list all services, even the hidden or unlisted ones?
For example, the http and USBStor services are not enumerated when listing services, but they can be accessed directly by name:
PS C:\Windows\System32> Get-Service | Where-Object {"http","usbstor","spooler" -contains $_.Name}
Status Name DisplayName
------ ---- -----------
Running Spooler Print Spooler
PS C:\Windows\System32> Get-Service "http","usbstor","spooler"
Status Name DisplayName
------ ---- -----------
Running http HTTP Service
Running spooler Print Spooler
Stopped usbstor USB Mass Storage Driver
This might not be the most elegant way of getting all the services (hidden per say), but this will give you all the services along with ones these are dependent on.
Get-Service -RequiredServices | select -Unique DisplayName | ? {$_.DisplayName -like "Http*" }
Try 'Get-CimInstance'.
Such functions (Get-Service) delivered by Microsoft rely on and use CIM/Win32 classes.
(Get-Service only shows Windows services. 'HTTP' is a system driver.)
Get-CimInstance 'CIM_Service'

What service restart first when we put 2 or more arguments (or in this same time)?

I need restart 2 services, but first vmicvss and next vss.
Get-Service -Name vmicvss, vss | Restart-Service
General services vss need vmicvss to run properly and I want know how exactly this code is execution. Is like:
Restart-Service vmicvss
Restart-Service vss
Or
in this same time or random moment.
Get-Service and Restart-Service apparently output / restart services in alphabetical order when given an array of names via parameter -Name.[1]
By contrast, providing the names / service objects via the pipeline does honor the input order:
# CAVEAT: Names passed to -Name are *sorted alphabetically*,
# so 'vmicvss' is processed before 'vss', due to coming first
# alphabetically.
Get-Service -Name vss, vmicvss | Restart-Service
# OK - with pipeline input, order is honored.
'vmicvss', 'vss' | Get-Service | Restart-Service
# OK (you don't need Get-Service in your scenario)
'vmicvss', 'vss' | Restart-Service
[1] As of Windows PowerShell v5.1 / PowerShell Core 6.2.0-preview.3; this problematic behavior is discussed in this GitHub issue.

Is there a way to get a hostname from an IP address without depending on a DNS inquiry?

I'm trying to write a script that depends on knowing the names of the computers on a network segment, but all the scripts I've found depend on a DNS inquiry which only replys with the names of a few of the machines. For example:
[System.Net.Dns]::GetHostbyAddress($IPAddress)
I've also tried using
Ping -a $ipaddress
but this often fails to return the machine name as well. Is there a way to ask the host what it's name is directly and what level of permissions might be required in AD to get a response?
Thanks in advance.
[System.Net.DNS]::GetHostByAddress() (now [System.Net.DNS]::GetHostEntry()) doesn't only rely on DNS, despite it's name. It will also check the local C:\Windows\System32\Drivers\etc\hosts file for locally configured entries.
straight dns via nslookup can't find the name:
PS C:\Users\Tim> nslookup 192.168.1.50
Server: dns03
Address: 192.168.2.103
*** rpi03 can't find 192.168.1.50: Non-existent domain
yet, gethostentry() still finds the name:
PS C:\Users\Tim> [system.net.dns]::gethostentry('192.168.1.50')
HostName Aliases AddressList
-------- ------- -----------
localentry {} {192.168.1.50}
COMMAND:
wmic.exe /node:10.20.30.40 OS get CSName /format:list
BATCH FILE FOR WHOLE SUBNET:
for /L %%z in (1,1,254) do wmic.exe /node:10.20.30.%%z OS get CSName /format:list 2>NUL
You can try by using something like:
Invoke-Command -computername $computer {Get-Item HKLM:\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName}
The active computername is equal to your DNS name (without suffix ofcourse)
I may misunderstand the problem but you can query the Win32_ComputerSystem instance using a CIM session to the remote computer and use one of those properties (Name, DNSName, etc.) Running locally it would be like
Get-CimInstance -namespace root/cimv2 -classname Win32_ComputerSystem | fl *
I'm aware that WMI might take fairly hefty permissions (e.g., domain admin) but (a) that might not be out of the question for your use case and (b) you might be able to do some limited querying with fewer permissions.
Another idea might be to query your SCCM server if you have one:
(Get-WmiObject -Query "SELECT * from SMS_R_SYSTEM WHERE IPAddresses LIKE '%$ipaddress%'" -Namespace "root\sms\site_$SiteCode" -computerName $SCCMServer).Name
Another idea using powershell:
Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer -Property Name | ForEach-Object {$_.Name}
Where $Computer is an IP address

Windows Equivalent on dnsmasq in Appveyor

What do you all recommend using as a replacement for dnsmasq on Windows on AppVeyor? Do you have any installation instructions for a replacement or an example job I can look at?
I believe it is pretty easy to script installation (using Install-WindowsFeature) and configuration of standard MS DNS and DHCP on Appveyor VM and configure them with PowerShell.
Here are useful PowerShell commands:
https://technet.microsoft.com/en-us/library/jj590751(v=wps.630).aspx
https://technet.microsoft.com/en-us/library/jj649850.aspx
Here is sample install section for Appveyor.yml:
install:
- ps: |
Install-WindowsFeature -Name DNS -IncludeManagementTools -WarningAction SilentlyContinue
Install-WindowsFeature -Name DHCP -IncludeManagementTools -WarningAction SilentlyContinue
Add-DnsServerPrimaryZone -Name foo.bar -ZoneFile foo.bar.dns
Add-DhcpServerv4Scope -Name TestScope -StartRange 192.168.1.100 -EndRange 192.168.1.110 -SubnetMask 255.255.255.0
$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -like '*ethernet*'}).IPAddress
Set-DHCPServerv4OptionValue -DnsDomain foo.bar -DnsServer $ip
However, I am not sure I fully understand the scenario. What machines are going to be clients of those services? Appveyor build is being executed on the single VM, which is behind the NAT and it is not accessible from public Internet. Or it will be some pieces of your software who will connect to local machine’s DNS/DHCP server, acquire private IP and register some name?
Also please note that you can configure hosts file, which may be simpler solution for your problem.
Thank you,
Ilya.