-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-ComputerSite.ps1
60 lines (52 loc) · 1.63 KB
/
Get-ComputerSite.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
49
50
51
52
53
54
55
56
57
58
59
60
function Get-ComputerSite {
<#
.SYNOPSIS
Determines the Active Directory site of a specified computername(s). Relies on nltest.exe that comes with Windows
.DESCRIPTION
Determines the Active Directory site of a specified computername(s). Relies on nltest.exe that comes with Windows
.PARAMETER ComputerName
The computername you want to run the command against, defaults to $env:COMPUTERNAME. Aliased to 'CN', 'Server'
.PARAMETER IncludeInput
Switch that will display input parameter in the output. Aliased to 'IncludeComputerName'
.EXAMPLE
Get-ComputerSite
Example result
CORP
.EXAMPLE
Get-ComputerSite -IncludeInput
Example result
ComputerName Site
------------ ----
DEMOLAPTOP CORP
#>
[CmdletBinding(ConfirmImpact='None')]
param
(
[Parameter(ValueFromPipeline)]
[Alias('CN', 'Server')]
[string[]] $ComputerName = $env:COMPUTERNAME,
[Alias('IncludeComputerName')]
[switch] $IncludeInput
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
foreach ($curCN in $ComputerName) {
$site = nltest.exe /server:$curCN /dsgetsite 2>$null
if ($LASTEXITCODE -eq 0) {
if ($IncludeInput) {
New-Object -TypeName 'psobject' -Property ([ordered] @{
ComputerName = $curCN
Site = $site[0]
})
} else {
$site[0]
}
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}