Skip to content

Commit 28005d5

Browse files
SamErdeCopilot
andcommitted
🐛 fix(function): resolve high-severity bugs in AD, DNS, Exchange, and general scripts
F07: Move Sort-Object emit from end to process block in Get-ADUserTransitiveGroupMembership to prevent pipeline clobbering (only last input was returned to end block) F09: Fix undefined \\\ -> \\.Count\ in Export-AllADUserTransitiveGroupMemberships F16: Fix operator precedence bug in Get-InactiveADUser (add parens to -not comparison) F17: Use \\\ parameter value as export path when provided in Get-InactiveADUser F19: Replace \continue\ with \ eturn\ in catch outside loop in Get-LockedOutLocation F22: Move pipeline identity resolution from begin to process block in Get-ADObjectFromPipeline F24: Make \\\ mandatory in Update-ModuleVersion to prevent null member access F27: Replace empty placeholder strings with mandatory params in Push-DNSClientServerAddresses F29: Remove hardcoded DOMAINNAME.org placeholder from Exchange ConnectionUri Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9c2927f commit 28005d5

8 files changed

Lines changed: 28 additions & 11 deletions

Active Directory/AD Users/Get-InactiveADUser.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696

9797
if ($CheckAllDCs) {
9898
# Skip the check across all DCs if there is already a LastLogonDate within the past 14 days and if the most recent logon is more recent than the inactive date threshold.
99-
if ( $MostRecentLogon -lt (Get-Date).AddDays(-14) -and (-not $MostRecentLogon -lt $InactiveDate) ) {
99+
if ( $MostRecentLogon -lt (Get-Date).AddDays(-14) -and (-not ($MostRecentLogon -lt $InactiveDate)) ) {
100100
# Check LastLogon (non-replicated) on every domain controller.
101101
foreach ($DC in $DomainControllers) {
102102
try {
@@ -143,7 +143,7 @@
143143

144144
# Optional: Export to CSV
145145
if ($PSBoundParameters.ContainsKey('ExportCSV')) {
146-
$ExportPath = ".\InactiveUsers_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"
146+
$ExportPath = if ($ExportCSV) { $ExportCSV } else { ".\InactiveUsers_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv" }
147147
$Results | Export-Csv -Path $ExportPath -NoTypeInformation
148148
Write-Host "Results exported to: $ExportPath" -ForegroundColor Green
149149
}

Active Directory/AD Users/Get-LockedOutLocation.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
$LockedOutEvents = Get-WinEvent -ComputerName $PDCEmulator.HostName -FilterHashtable @{LogName = 'Security'; Id = 4740 } -ErrorAction Stop | Sort-Object -Property TimeCreated -Descending
7575
} catch {
7676
Write-Warning $_
77-
continue
77+
return
7878
}#end catch
7979

8080
foreach ($item in $LockedOutEvents) {

Active Directory/Export-AllADUserTransitiveGroupMemberships.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ begin {
5252
Get-ADUserTransitiveGroupMembership -UserDN $_.DistinguishedName
5353
}
5454
}
55-
Write-Verbose -Message " - Found $($UserCount) users in the domain."
55+
Write-Verbose -Message " - Found $($Users.Count) users in the domain."
5656

5757
# Export the data to a JSON file.
5858
$JsonData = $Users | ConvertTo-Json

Active Directory/Get-ADObjectFromPipeline.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ function Get-ADObjectFromPipeline {
1616
begin {
1717
Import-Module ActiveDirectory
1818
$GlobalCatalog = Get-ADDomainController -Discover -Service GlobalCatalog
19+
}
1920

21+
process {
22+
# Resolve identity type in process block where pipeline input ($Identity) is available.
2023
if ($Identity -is [Microsoft.ActiveDirectory.Management.ADUser]) {
2124
# We have an ADUser object
2225
# Might want to normalize the type to an ADObject IF we can get sidHistory from an ADObject
@@ -30,9 +33,6 @@ function Get-ADObjectFromPipeline {
3033
$Identity = Get-ADObject -Filter "Name -eq `"$Identity`""
3134
}
3235
$IdentityType = $Identity.ObjectClass
33-
}
34-
35-
process {
3636
switch ($IdentityType) {
3737
'user' {
3838
# Not Complete

Active Directory/Get-ADUserTransitiveGroupMembership.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ function Get-ADUserTransitiveGroupMembership {
8080
$TransitiveMemberOfGroupDNs = foreach ($result in ($results.properties)) {
8181
$result['distinguishedname']
8282
}
83+
# Emit deduplicated results per user in process block so pipeline results are not overwritten.
84+
$TransitiveMemberOfGroupDNs | Sort-Object -Unique
8385
}
8486

8587
end {
86-
$TransitiveMemberOfGroupDNs | Sort-Object -Unique
8788
Remove-Variable Filter, TransitiveMemberOfGroupDNs, Results, Searcher, Server, Port, UserDN -ErrorAction SilentlyContinue
8889
}
8990
} # end function Get-ADUserTransitiveGroupMembership

Exchange/Parse-TransportLogs.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Set-ExecutionPolicy RemoteSigned
1717
$ExchangeCredential = Get-Credential -Message "Please enter credentials to connect to your Exchange Server. `nThis will be used to pull message subject lines from the tracking logs."
1818
$ExchangeServer = Read-Host 'Please specify an Exchange Server name.'
19-
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchangeServer.DOMAINNAME.org/PowerShell/ -Authentication Kerberos -Credential $ExchangeCredential
19+
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$ExchangeServer/PowerShell/" -Authentication Kerberos -Credential $ExchangeCredential
2020
Import-PSSession $ExchangeSession -DisableNameChecking
2121

2222
$SMTPLogPath = Read-Host "`nWhat is the path of the folder containing your SMTP transport logs?"

General/Update-ModuleVersion.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
param (
55

66
# Specify the version to update from (or read from a module manifest).
7+
[Parameter(Mandatory)]
78
[version] $InputVersion,
89

910
# Basic version switches.

Windows/Push-DNSClientServerAddresses.ps1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
[CmdletBinding()]
2+
param (
3+
# The OU or container in Active Directory to search for servers.
4+
[Parameter(Mandatory)]
5+
[string] $SearchBase,
6+
7+
# The Active Directory domain controller to query.
8+
[Parameter(Mandatory)]
9+
[string] $ADServer,
10+
11+
# The DNS server addresses to assign to network adapters on the target servers.
12+
[Parameter(Mandatory)]
13+
[string[]] $DNSServerAddresses
14+
)
15+
116
Import-Module ActiveDirectory
2-
$servers = Get-ADComputer -SearchBase "" -Server "" -SearchScope Subtree -Filter *
17+
$servers = Get-ADComputer -SearchBase $SearchBase -Server $ADServer -SearchScope Subtree -Filter *
318
foreach ($server in $servers)
419
{
520
# Connect to the server.
@@ -20,7 +35,7 @@ foreach ($server in $servers)
2035
try {
2136
Invoke-Command -Session $s -ScriptBlock {
2237
Get-NetIPInterface | Get-DnsClientServerAddress | Where-Object { $_.ServerAddresses -like '10.10.10.*' } |
23-
Set-DnsClientServerAddress -ServerAddresses ('', '', '') -Verbose
38+
Set-DnsClientServerAddress -ServerAddresses $using:DNSServerAddresses -Verbose
2439
}
2540
}
2641
catch {

0 commit comments

Comments
 (0)