-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMyOrgComputers.psm1
More file actions
55 lines (52 loc) · 2.03 KB
/
GetMyOrgComputers.psm1
File metadata and controls
55 lines (52 loc) · 2.03 KB
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
<#
SAMPLE CODE: Add your own named scoped in the "switch ($scope)" section to easily
collect computer objects from different OUs and domains/forests in your environment.
#>
function Get-MyOrgComputers {
[cmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[ValidateSet('prod','test','dev')]
[string]$scope
)
switch ($scope) {
prod {
[string[]]$computerOUs = @(
'OU=Servers,DC=NewColonial,DC=GOV',
'OU=Exchange Servers,DC=NewColonial,DC=GOV'
)
$dom = "newcolonial.gov"
}
test {
[string[]]$computerOUs = @(
'OU=Servers,DC=NewColonial-Test,DC=GOV',
'OU=Exchange Servers,DC=NewColonial-Test,DC=GOV'
)
$dom = "newcolonial-test.gov"
}
dev {
[string[]]$computerOUs = @(
'OU=Servers,DC=NewColonial-Dev,DC=GOV',
'OU=Exchange Servers,DC=NewColonial-Dev,DC=GOV'
)
$dom = "newcolonial-dev.gov"
}
}
try {Import-Module ActiveDirectory -ea Stop | Out-Null} catch {
Throw "Failed to load the Active Directory PowerShell module."
}
# Fetch computer objects to investigate using the OUs provided in the $ComputerOUs parameter:
Write-Host
Write-Host 'Gathering Computer Objects...' -Fore Cyan
[string[]]$computers = @()
$lastMonth = ([datetime]::now).AddDays(-30)
foreach ($ou in $ComputerOUs) {
$computers += Get-ADComputer -Server $dom -SearchBase $ou `
-Filter * -Properties OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,LastLogonTimestamp |
Where-Object {$_.OperatingSystem -match 'Server'} |
Where-Object {([datetime]::FromFileTime($_.LastLogonTimestamp)) -gt $lastMonth } |
Select-Object -ExpandProperty Name | Sort-Object
}
$computers
}
Export-ModuleMember -Function Get-MyOrgComputers