<# @rem DnsRdp.bat - this will run without showing the powershell window @start "" /MIN pwsh -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -File "%~dp0\\DnsRdp.ps1" #> # auto sign in (script must be run as admin at least once for this to take effect) function AllowAutoLogins() { $null = reg.exe add "HKLM\SOFTWARE\Policies\Microsoft\Windows\CredentialsDelegation\AllowDefaultCredentials" /v "1" /t REG_SZ /d "TERMSRV/*" /f $null = reg.exe add "HKLM\SOFTWARE\Policies\Microsoft\Windows\CredentialsDelegation\AllowDefCredentialsWhenNTLMOnly" /v "1" /t REG_SZ /d "TERMSRV/*" /f } function EnsureDnsServerModule() { # Check if the DnsServer module is available if (-not (Get-Module -ListAvailable -Name DnsServer)) { Write-Error "The DnsServer module is not available. Attempting to install it..." Install-WindowsFeature -Name RSAT-DNS-Server if (-not (Get-Module -ListAvailable -Name DnsServer)) { Write-Error "Failed to install the DnsServer module. Please ensure you have the necessary permissions and try again." exit 1 } else { Write-Host "DnsServer module installed successfully." } } } function LaunchDnsRdp() { # Load the required assembly for Windows Forms Add-Type -AssemblyName System.Windows.Forms # Specify the DNS server and zone $dnsServer = $Env:USERDNSDOMAIN.ToLower() $zoneName = $Env:USERDNSDOMAIN.ToLower() try { # Create the loading form $loadingForm = New-Object System.Windows.Forms.Form $loadingForm.Text = "Loading DNS Records... ($zoneName)" $loadingForm.Size = New-Object System.Drawing.Size(600, 150) $loadingForm.BackColor = [System.Drawing.Color]::FromArgb(64, 64, 64) # Dark grey background $loadingForm.FormBorderStyle = 'FixedDialog' # Prevent resizing $loadingForm.ForeColor = [System.Drawing.Color]::FromArgb(255, 255, 255) $loadingForm.Font = New-Object System.Drawing.Font("Arial", 14) $loadingForm.StartPosition = 'CenterScreen' # This centers the form on the screen # Create the loading label $loadingLabel = New-Object System.Windows.Forms.Label $loadingLabel.Text = "Loading DNS records, please wait..." $loadingLabel.AutoSize = $false $loadingLabel.Size = New-Object System.Drawing.Size(580, 100) # Set a fixed size for the label $loadingLabel.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter # Optional: Center text within the label's own bounds # Add the label to the form's controls $loadingForm.Controls.Add($loadingLabel) # Event handler to center the label after the form is shown # This is crucial because $loadingLabel.Width and $loadingLabel.Height # are only accurate AFTER the form has rendered and AutoSize has taken effect. $loadingForm.Add_Shown({ # Calculate the X coordinate for horizontal centering # (Form's client width - Label's width) / 2 $x = ($loadingForm.ClientSize.Width - $loadingLabel.Width) / 2 # Calculate the Y coordinate for vertical centering # (Form's client height - Label's height) / 2 $y = ($loadingForm.ClientSize.Height - $loadingLabel.Height) / 2 # Set the new location for the label $loadingLabel.Location = New-Object System.Drawing.Point($x, $y) }) # Show the loading form $loadingForm.Show() $loadingForm.Refresh() # Ensure the form is rendered before proceeding # Get A records from the DNS server using Get-DnsServerResourceRecord $zoneRecords = Get-DnsServerResourceRecord -ComputerName $dnsServer -ZoneName $zoneName $dnsEntries = @() foreach ($record in $zoneRecords) { if ($record.RecordType -eq 'A') { $dnsEntries += [PSCustomObject]@{ HostName = $record.HostName.Replace($zoneName,'') + "." + $zoneName IPAddress = $record.RecordData.IPv4Address.IPAddressToString } } } # Create a Windows Form $form = New-Object System.Windows.Forms.Form $form.Text = "DNS RDP ($zoneName)" $form.Size = New-Object System.Drawing.Size(600, 400) # Create a ListView $listView = New-Object System.Windows.Forms.ListView $listView.View = 'Details' $listView.FullRowSelect = $true $listView.Dock = 'Fill' # Add columns to the ListView [void]$listView.Columns.Add("Hostname", 600) [void]$listView.Columns.Add("IP Address", 300) # Make the font size larger $listView.Font = New-Object System.Drawing.Font("Arial", 20) # Populate the ListView with DNS entries foreach ($entry in $dnsEntries) { $item = New-Object System.Windows.Forms.ListViewItem($entry.HostName) [void]$item.SubItems.Add($entry.IPAddress) [void]$listView.Items.Add($item) } # Handle the double-click event to open MSTSC $listView.Add_DoubleClick({ if ($listView.SelectedItems.Count -gt 0) { $selectedItem = $listView.SelectedItems[0] $hostname = $selectedItem.SubItems[0].Text Start-Process mstsc.exe -ArgumentList "/v:$hostname" } }) # also launch MSTSC when pressing Enter $listView.Add_KeyUp({ if ($_.KeyCode -eq 'Enter') { if ($listView.SelectedItems.Count -gt 0) { $selectedItem = $listView.SelectedItems[0] $hostname = $selectedItem.SubItems[0].Text Start-Process mstsc.exe -ArgumentList "/v:$hostname" } } }) # make right clicking on an item copy the hostname to the clipboard $listView.Add_MouseClick({ if ($_.Button -eq 'Right') { $selectedItem = $listView.GetItemAt($_.X, $_.Y) if ($selectedItem) { $hostname = $selectedItem.SubItems[0].Text [System.Windows.Forms.Clipboard]::SetText($hostname) } } }) # Add the ListView to the form and display it $form.Controls.Add($listView) # make the form dark mode, with white text and dark grey background $form.BackColor = [System.Drawing.Color]::FromArgb(0, 0, 0) $form.ForeColor = [System.Drawing.Color]::FromArgb(255, 255, 255) $listView.BackColor = [System.Drawing.Color]::FromArgb(64, 64, 64) $listView.ForeColor = [System.Drawing.Color]::FromArgb(255, 255, 255) # make form show maximized $form.WindowState = 'Maximized' $loadingForm.Dispose() [void]$form.ShowDialog() } catch { Write-Error "An error occurred: $_" } } # if running as admin, allow auto logins if ([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) { AllowAutoLogins EnsureDnsServerModule } LaunchDnsRdp