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.
- Get Computer Name Using Environment Variables
- .Net Class
- System.Environment
- System.Net.DNS
Script
Get Computer Name using Environment Variables
Outline
- Use Powershell $env object
- The various platforms use different variable names
- MS Windows
- Computer
- Linux
- HOSTNAME
- MS Windows
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
- Uses Microsoft.Net System.Environment class
- 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
- Uses Microsoft.Net System.Net.DNS class
- 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
- Microsoft
- StackOveflow
- Powershell: Get FQDN Hostname
Link
- Powershell: Get FQDN Hostname