Get-ADSiteName.ps1

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 85847c8d-15d5-4a70-8431-4a4530aa51d5
 
.AUTHOR Chris Carter
 
.COMPANYNAME
 
.COPYRIGHT 2017 Chris Carter. All rights reserved.
 
.TAGS ActiveDirectory Site Domain
 
.LICENSEURI http://creativecommons.org/licenses/by-sa/4.0/
 
.PROJECTURI https://gallery.technet.microsoft.com/Get-the-Active-Directory-ce33008b
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES Initial Release
 
 
#>


<#
.SYNOPSIS
Gets the Active Directory site name of computers.
 
.DESCRIPTION
Get-ADSiteName retrieves the name of the site the computer(s) specified to the ComputerName parameter is a part of.
.PARAMETER ComputerName
The name of the computer to retrieve the site name of. The default is the local computer.
 
Type the NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list. To specify the local computer, type the computer name, localhost, or a dot (.).
 
See Get-Help Invoke-Command -Parameter ComputerName for more information.
.PARAMETER Credential
Specifies a user account that has permission to perform this action. The default is the current user.
 
Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.
 
This parameter only works when querying a remote computer, otherwise it has no effect on the local computer.
.INPUTS
System.String
 
 
 
 
Get-ADSiteName accepts string objects by value and by property name on the pipline.
.OUTPUTS
System.String
 
 
 
 
Get-ADSiteName outputs a string object representing the name of the AD site the computer belongs to.
.EXAMPLE
PS C:\> Get-ADSiteName
 
This command will get the name of the AD site the local computer is a part of.
.EXAMPLE
PS C:\> "Server01","Server02" | Get-ADSiteName
 
This command will get the names of the sites that machines "Server01" and Server02" are a part of.
.EXAMPLE
PS C:\> Get-ADSiteName -ComputerName Workstation1 -Credential (Get-Credential)
 
This command will get the name of the site that machine "Workstation1" is a part of using the Credential provided at the credential prompt shown after execution of the command.
.NOTES
This command gets its information from the registry key Site-Name located at HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine.
.LINK
Invoke-Command
.LINK
Get-ItemProperty
#>


#Requires -Version 3.0
[CmdletBinding()]

Param(
    [Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [Alias("Cn","Server")]
        [String[]]$ComputerName=".",

    [Parameter(Position=1)]
        [PSCredential]$Credential
)

Begin {
    #Create parameter hashtable to splat later
    $Params = @{}
    #Add Credential to hashtable if exists
    if ($Credential) {
        $Params = @{ Credential=$Credential }
    }

    #Location of key
    $RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine'
}

Process {
    foreach ($c in $ComputerName) {
        #Only call Invoke-command if it is not the local machine
        if ($c -notin $env:COMPUTERNAME, "localhost", ".") {
            #Add computer name to parameter hashtable
            $Params.ComputerName = $c
            #Invoke on remote computer using
            Invoke-Command { (Get-ItemProperty -Path $using:RegistryPath -Name Site-Name).{Site-Name} } @Params
        }
        else {
            #Run on local machine
            (Get-ItemProperty -Path $RegistryPath -Name Site-Name).{Site-Name}
        }
    }
}