Powershell:- Get Machine Name

Background

I needed to confirm a machine name.

It is easy to do so via accessing the machine and running a few system commands.

But, want to do so programmatically, as well.

Choices

Here are some of the options for getting a machine name in PowerShell.

  1. Get Computer Name Using Environment Variables
  2. .Net Class
    • System.Environment
    • System.Net.DNS

 

Script

Get Computer Name using Environment Variables

Outline

  1. Use Powershell $env object
  2. The various platforms use different variable names
    • MS Windows
      • Computer
    • Linux
      • HOSTNAME

Code

function getMachineNameUsingEnvironmentVariable
{
    <#
       Get computer name using environment variables
    #>
    
    [string] $computer="";
    [string] $hostname="";
    

    #Get computer name from environment variable
    $computer = $env:COMPUTERNAME;
    $hostname = $env:HOSTNAME;


    $header = "Environment Variable"
    Write-Host $header

    #Get length of header
    $len = $header.length;

    <#
        Get header separator
        Create new string using character and length
    #>
    $headerSeparator = new-object System.String( $CHAR_SEPARATOR, $len );;

    Write-Host $headerSeparator;

    <#
        Display Environment Variable - COMPUTER
    #>
    $log = [string]::Format($FORMAT_ENTRY, "`$env:COMPUTER", $computer);

    Write-Host $log;

    <#
        Display Environment Variable - HOSTNAME
    #>
    $log = [string]::Format($FORMAT_ENTRY, "`$env:HOSTNAME", $hostname);

    Write-Host $log

    Write-Host ""
    Write-Host ""
 
}

 

Get Computer Name using .Net Class System.Environment

Outline

  1. Uses Microsoft.Net System.Environment class
  2. System.Environment Properties
    • MachineName

Code


function getMachineNameUsingEnvironmentClass
{
    
    [string] $machineName = "";
    
    <#
       Get machine name from System.Environment class
    #>
    #Get computer name from environment variable
    $machineName = [System.Environment]::MachineName;


    $header = "[System.Environment] Class"
    Write-Host $header

    #Get length of header
    $len = $header.length;

    <#
        Get header separator
        Create new string using character and length
    #>
    $headerSeparator = new-object System.String( $CHAR_SEPARATOR, $len );;

    Write-Host $headerSeparator;

    <#
        Display [System.Environment]::MachineName
    #>
    $log = [string]::Format($FORMAT_ENTRY, "[System.Environment]::MachineName", $machineName);

    Write-Host $log;

    Write-Host ""
    Write-Host ""

}


 

 

Get Computer Name using .Net Class System.Net.DNS

Outline

  1. Uses Microsoft.Net System.Net.DNS class
  2. Methods
    • [System.Net.DNS]::GetHostEntry
    • [System.Net.DNS]::GetHostByName

Code


function getIPAddressListAsString([System.Net.IPHostEntry] $ipHostEntry)
{
    
    <#
        Concatenate IPAddresses from System.Net.IPHostEntry
    #>
    [int] $index=0;
    [string] $ipAddressList ="";

    
    for($index=0; $index -lt $ipHostEntry.AddressList.Length; $index++)
    {        
            
        $ipAddress = $ipHostEntry.AddressList[$index];
            
        <#
            If this is not the first match, add a separator
        #>
        if ($index -gt 0)
        {
            $ipAddressList = $ipAddressList + $CHAR_COMMA;
        }        
        
        $ipAddressList = $ipAddressList + $ipAddress;
            
    }
    
    $ipAddressList = $ipAddressList.trim();
    
    return ($ipAddressList);

}

function getMachineNameUsingDNS
{
    
    <#
        Get MachineName from DNS
    #>
    
    [string] $hostnameToUseInDNSCalls="";
    [System.Net.IPHostEntry] $ipHostEntryUsingDNSHostEntry = $null;
    [System.Net.IPHostEntry] $ipHostEntryUsingDNSHostByName = $null;    

    Write-Host ""
    Write-Host ""

    
    [Boolean] $ipHostEntryUsingDNSHostEntryUsable = $false;

    [Boolean] $ipHostEntryUsingDNSHostByNameUsable = $false;

    [String] $hostnameFromUsingDNSHostEntry = $null;
    [string] $hostnameFromUsingDNSHostByName  = $null;

    <#
        [System.Net.DNS]::GetHostEntry
    #>
    $ipHostEntryUsingDNSHostEntry  = [System.Net.DNS]::GetHostEntry(($hostnameToUseInDNSCalls));


    <#
        [System.Net.DNS]::GetHostByName
    #>
    $ipHostEntryUsingDNSHostByName = [System.Net.DNS]::GetHostByName(($hostnameToUseInDNSCalls));


    if ( $ipHostEntryUsingDNSHostEntry -ne $null)
    {

      $hostnameFromUsingDNSHostEntry =  $ipHostEntryUsingDNSHostEntry.Hostname;
      
      $ipHostEntryUsingDNSHostEntryUsable = $true;
      
    }
    else
    {
        $ipHostEntryUsingDNSHostEntryUsable = $false; 
    }
    
    if ($ipHostEntryUsingDNSHostByName -ne $null )
    {

      $hostnameFromUsingDNSHostByName = $ipHostEntryUsingDNSHostByName.Hostname;
      
      $ipHostEntryUsingDNSHostByNameUsable = $true;

    }
    else
    {
        $ipHostEntryUsingDNSHostByNameUsable = $false; 
    }
    
    $header = "DNS"

    Write-Host $header;

    $len = $header.length;

    $headerSeparator = new-object System.String ( $CHAR_SEPARATOR, $len );

    Write-Host $headerSeparator;

    if ($ipHostEntryUsingDNSHostEntryUsable)
    {
        $log = [string]::Format($FORMAT_ENTRY, "DNS::GetHostEntry", $hostnameFromUsingDNSHostEntry  );

        Write-Host $log;
        
        Write-Host ""

    }
    
    if ( $ipHostEntryUsingDNSHostEntryUsable)
    {
        
        $ipAddressList = getIPAddressListAsString ( $ipHostEntryUsingDNSHostEntry);

        $log = [string]::Format($FORMAT_ENTRY_DETAIL, "IP Address List",  $ipAddressList);

        Write-Host $log
        
        Write-Host ""

    }
    
    if ($ipHostEntryUsingDNSHostByNameUsable)
    {
        
        $log = [string]::Format($FORMAT_ENTRY, "DNS::GetHostByName", $hostnameFromUsingDNSHostByName);

        Write-Host $log
        
        Write-Host ""

    }


    if ($ipHostEntryUsingDNSHostByNameUsable)
    {
        
        $ipAddressList = getIPAddressListAsString ( $ipHostEntryUsingDNSHostByName);

        $log = [string]::Format($FORMAT_ENTRY_DETAIL, "IP Address List",  $ipAddressList);

        Write-Host $log
        
        Write-Host ""

    }
    
    Write-Host ""
    Write-Host ""


}


 

Output

Output – Microsoft

Output – Linux

 

Source Code

GitLab

Get Machine Name

Get Machine Name
link

 

Reference

  1. Microsoft
    • Docs
      • .Net
        • Docs > .NET > .NET API browser > System
          • Environment Class
            Link
        • Docs > .NET > .NET API browser > System.Net > IPHostEntry
          • IPHostEntry
            Link
          • IPHostEntry.AddressList Property
            Link
      • Powershell
        • Docs > PowerShell > Scripting
          • about_Environment_Variables
            Link
  2. StackOveflow
    • Powershell: Get FQDN Hostname
      Link 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s