Get-HostName.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<#
.Synopsis
   Get the hostname of a local or remote computer.
.DESCRIPTION
   Get the hostname of a local or remote computer. This can be used to
   get the real hostname of computer that may have an alias.
.EXAMPLE
   Get-HostName -ComputerName ALIASSERVER
   Get the real hostname of the the aliasserver.
#>

function Get-Hostname
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]$ComputerName=$env:COMPUTERNAME
    )

    Begin
    {
    }
    Process
    {
        foreach ($Computer in $ComputerName) {
            try {
                $Hostname = Get-WmiObject -Class win32_operatingsystem -ComputerName $Computer -ErrorAction Stop | Select-Object -Property @{Label='Hostname';Expression={$_.PSComputerName}}
                $Hostname
                }
            catch {
                Write-Error "Unable to get the hostname of $Computer."
                }
            }
    }
    End
    {
    }
}