-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount of legacy contractors.ps1
More file actions
32 lines (30 loc) · 1.64 KB
/
Copy pathCount of legacy contractors.ps1
File metadata and controls
32 lines (30 loc) · 1.64 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
#Count of legacy contractors
#enabled and not expired
$users = @()
$legacyUsers = @()
$OUs = "OU=Contractors,OU=Users,OU=Managed Users & Computers,DC=corp,DC=gianteagle,DC=com"
$LDAP_Lookup = '(&' # Define LDAP as a AND across all filters
$LDAP_Lookup += '(objectCategory=person)' # LDAP Person
$LDAP_Lookup += '(objectClass=user)' # LDAP User
$LDAP_Lookup += '(!userAccountControl:1.2.840.113556.1.4.803:=2)' # LDAP Enabled
$LDAP_Lookup += '(!memberOf=CN=SG_Expired_Accounts,OU=Security Groups,DC=corp,DC=gianteagle,DC=com)' # LDAP Not a member of Expired Group
$LDAP_Lookup += ')' # Close LDAP
foreach($OU in $OUs) {
$users += Get-ADUser -LDAPFilter $LDAP_Lookup -SearchBase $OU -Property msDS-ExternalDirectoryObjectId, UserPrincipalName, DisplayName, lastLogonDate, whenCreated #| Where-Object { $_.DistinguishedName -notlike '*OU=Leadership,*'}
}
$users | ForEach-Object {
if ($_.SamAccountName.length -lt 7){
if ($_.samaccountname -like "8*"){
$legacyUsers += $_
}
if ($_.samaccountname -like "9*"){
$legacyUsers += $_
}
}else {
Write-Host "$_.Samaccountname Does not meet filter."
}
if ($_.samaccountname -like "vend*"){
$legacyUsers += $_
}
}
$legacyUsers | Export-Csv -Path C:\Temp\LegacyUsers2.CSV