Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

ISE additional spacing and temperamental colur issues w/ function

$
0
0

Hi All,

We've got some weird DNS issues where our DNS server doesn't seem to hold the latest addresses for our machines as provided by our DHCP server.

This is causing me a bit of a headache when trying to administer these machines remotely as I have to double check that the hostname points to the correct IP as otherwise I'll be making changes to a completely different machine than intended.

I thought a quick win would be a little PowerShell script. It seems to return the results as I'd expect, but there seem to be some inconsistencies with spacing when the script terminates, and issues with the colours that I've applied to some of the write-host outputs.

Weirdly, the colour output of the write-host doesn't appear to be an issue if I pass the Hostname parameter whilst calling the function. Only seems to be when I prompt the user by the Read-Host.

The idea is:

1) Get the IP of a given hostname

2) Get the hostname of that given IP (to check that this matches the original hostname)

3) Act accordingly with the results of the above

I've tweaked the code so you can run with 'test' as the hostname

Running w/ PowerShell 4.

Set-PSDebug    -Strict
Set-StrictMode -Version Latest

Clear-Host

Function Test-DNS
{
    [CmdletBinding()]
        Param 
        (
            # Allow the user to specify the FQDN if it hasn't already been supplied

            $Hostname =   (Read-Host "Please specify Hostname"),
            # Allow passing of the domain name. Corin FQDN by default.

            $Domain   =   'test.co.uk'
        )

    # Clear the screen after the read-host input

    Clear-Host

    # Throw an error if the hostname is not defined

    if(!($Hostname))
    {
        Write-Host        'You have not specified a hostname. This script will now terminate.'
        Write-Host        ''

        Break
    } 

    # Define variables and assign null values

    $IP_Response        = @()
    $IP_Count           = $null
    $Reverse_Lookup     = $null
    $Full_DNS_Name      = $null

    # Create the FQDN for a given hostname (if not already present)
    
    if($Hostname -like "*$Domain")
    {
        $Full_DNS_Name  = ($Hostname)
    }
    else
    {
        $Full_DNS_Name  = ($Hostname + '.' + $Domain)
    }

    # Display the hostname

    Write-Host            $Hostname
    Write-Host            ''

    # Try to grab the IP of a hostname and provide an error if this fails
    
    Try
    {
        $IP_Response   += '10.15.1.124' #([System.Net.Dns]::GetHostAddresses($Hostname) | Where {!($_.IsIPv6LinkLocal)}).IPAddressToString
        $IP_Count       = $IP_Response.Count
    }

    Catch [Exception]
    {
        Write-Host        'Error:' $_.Exception.Message -ForegroundColor Red
        Write-Host        ''
        
        Break
    }

    # Check the number of results returned and warn the user if there are more than one

    if($IP_Count -gt 1)
    {
         Write-Host       'WARNING:' $IP_Count 'addresses have been detected for this hostname.' -ForegroundColor Yellow
         Write-Host       ''
    }

    # Process each of the IPs that have been returned

    ForEach($IP in $IP_Response)
    {
        Try
            {
                $Reverse_Lookup = 'test' #([System.Net.Dns]::GetHostByAddress($IP).HostName)

                # If the reverse lookup matches the original hostname

                if(($Reverse_Lookup -eq $Hostname) -or ($Reverse_Lookup -eq $Full_DNS_Name))
                {
                    Write-Host    'Ping Response: ' $IP
                    Write-Host    'Reverse Lookup:' $Reverse_Lookup
                    Write-Host    ''
            
                    Write-Host    'Success: Reverse DNS lookup was successful.' -ForegroundColor Green
                    Write-Host    ''        
                }

                 # If the above is not true

                else
                {
                    Write-Host    'Ping Response: ' $IP
                    Write-Host    'Reverse Lookup:' $Reverse_Lookup
                    Write-Host    ''

                    Write-Host    'Error: Reverse DNS lookup did not match the provided hostname.' -ForegroundColor Red
                    Write-Host    ''
                }
            }
                 
        Catch [Exception]
            {
                Write-Host        $IP
                Write-Host        'Error:' $_.Exception.Message -ForegroundColor Red
                Write-Host        ''   
            }  
    }
}

Test-DNS

This is what happens after a few runs (usually when I've terminated the script part way through), without passing the Hostname parameter:

http://s16.postimg.org/sb2lehqdx/Output_1.png

(It also applies to some of the other write-host outputs), including error messages and the dbg messages that appear when I set breakpoints)

Now, onto the spacing issue...

If I continue to refresh the screen or rerun the script, it will sometimes randomly put two blank spaces before terminating. I've only specified one. I can't understand why this happens

This is how it appears when there are two spaces

http://s23.postimg.org/bgtg0hx5n/Output_3.png

I have just noticed that when it does display correctly (1 space), the first two characters of 'Success' are white, while the rest remain green.

Can anybody shed any light on this? It's driving me round the bend!

Cheers.


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>