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
43
44
45
46
47
48
<#
.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.
.NOTES
Created By: Jason Wasser
Modified: 7/17/2017 08:59:48 AM
 
* added credential support
#>

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

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