PoshCode Archive  Artifact [9f77668381]

Artifact 9f77668381b6d2e86a710e16d9677d9cc0956c25623569cc220850d5609b935b:

  • File Get-Hostname.ps1 — part of check-in [9bd637a0f2] at 2018-06-10 14:08:25 on branch trunk — Print the hostname of the system. Complete with v2 comment-based help, but works fine on v1. (user: halr9000 size: 1301)

# encoding: ascii
# api: powershell
# title: Get-Hostname
# description: Print the hostname of the system. Complete with v2 comment-based help, but works fine on v1.
# version: 0.1
# type: script
# author: halr9000
# license: CC0
# x-poshcode-id: 6120
# x-archived: 2016-03-19T02:52:31
# x-published: 2016-11-27T21:39:00
#
#
# .SYNOPSIS
#	Print the hostname of the system.
# .DESCRIPTION
#	This function prints the hostname of the system. You can additionally output the DNS
#	domain or the FQDN by using the parameters as described below.
# .PARAMETER Short
#	(Default) Print only the computername, i.e. the same value as returned by $env:computername
# .PARAMETER Domain
#	If set, print only the DNS domain to which the system belongs. Overrides the Short parameter.
# .PARAMETER FQDN
#	If set, print the fully-qualified domain name (FQDN) of the system. Overrides the Domain parameter.

param (
	[switch]$Short		= $true,
	[switch]$Domain		= $false,
	[switch]$FQDN		= $false
)

$ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
if ( $FQDN ) {
	return "{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName
}
if ( $Domain ) {
	return $ipProperties.DomainName
}
if ( $Short ) {
	return $ipProperties.HostName
}