From 084a48c9b73023882760ed030385b2e41f3a0f82 Mon Sep 17 00:00:00 2001 From: Jonathan Colon Date: Sat, 15 Jun 2024 08:31:01 -0400 Subject: [PATCH] Fix Add DHCP Infrastructure information #180 --- CHANGELOG.md | 1 + Src/Private/Get-AbrDHCPinAD.ps1 | 79 ++++++++++++++++++++++++++++ Src/Private/Get-AbrForestSection.ps1 | 5 ++ 3 files changed, 85 insertions(+) create mode 100644 Src/Private/Get-AbrDHCPinAD.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 744c62a..7fcd05d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix [#174](https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.AD/issues/174) - Fix [#176](https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.AD/issues/176) - Fix [#178](https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.AD/issues/178) +- Fix [#180](https://github.com/AsBuiltReport/AsBuiltReport.Microsoft.AD/issues/180) ## [0.8.1] - 2024-05-16 diff --git a/Src/Private/Get-AbrDHCPinAD.ps1 b/Src/Private/Get-AbrDHCPinAD.ps1 new file mode 100644 index 0000000..36cd64d --- /dev/null +++ b/Src/Private/Get-AbrDHCPinAD.ps1 @@ -0,0 +1,79 @@ +function Get-AbrDHCPinAD { + <# + .SYNOPSIS + Used by As Built Report to retrieve Microsoft AD DHCP Servers information + .DESCRIPTION + + .NOTES + Version: 0.8.2 + Author: Jonathan Colon + Twitter: @jcolonfzenpr + Github: rebelinux + .EXAMPLE + + .LINK + + #> + [CmdletBinding()] + param ( + ) + + begin { + Write-PScriboMessage "Collecting AD DHCP Servers information of $($ForestInfo.toUpper())." + } + + process { + $DomainInfo = Invoke-Command -Session $TempPssSession { Get-ADDomain ($using:ADSystem).RootDomain -ErrorAction Stop } + if ($DomainInfo) { + $DHCPServers = try { + Get-ADObjectSearch -DN "CN=NetServices,CN=Services,CN=Configuration,$(($DomainInfo).DistinguishedName)" -Filter { objectclass -eq 'dHCPClass' -AND Name -ne 'dhcproot' } -Properties "*" -SelectPrty 'Name' -Session $TempPssSession + } catch { Out-Null } + } + try { + if ($DHCPServers ) { + try { + $DCServersinAD = @(foreach ($Domain in $ADSystem.Domains) { + (Invoke-Command -Session $TempPssSession -ErrorAction Stop { Get-ADDomain -Identity $using:Domain }).ReplicaDirectoryServers + }) + } catch { Out-Null } + Section -Style Heading3 'DHCP Infrastructure' { + Paragraph "The following section provides a summary of the DHCP infrastructure configured on Active Directory." + BlankLine + $DCHPInfo = @() + foreach ($DHCPServer in $DHCPServers) { + try { + $inObj = [ordered] @{ + 'Server Name' = $DHCPServer.Name + 'Is Domain Controller?' = Switch ($DHCPServer.Name -in $DCServersinAD) { + $True {'Yes'} + $false {'No'} + default {'Unknown'} + } + } + $DCHPInfo += [pscustomobject]$inobj + } catch { + Write-PScriboMessage -IsWarning "$($_.Exception.Message) (DHCP Item)" + } + } + + $TableParams = @{ + Name = "DHCP Infrastructure - $($ForestInfo.toUpper())" + List = $false + ColumnWidths = 50, 50 + } + if ($Report.ShowTableCaptions) { + $TableParams['Caption'] = "- $($TableParams.Name)" + } + $DCHPInfo | Sort-Object -Property 'Server Name' | Table @TableParams + } + } else { + Write-PScriboMessage -IsWarning "No DHCP Infrastructure information found in $($ForestInfo.toUpper()), disabling the section." + } + } catch { + Write-PScriboMessage -IsWarning "$($_.Exception.Message) (DHCP Table)" + } + } + + end {} + +} \ No newline at end of file diff --git a/Src/Private/Get-AbrForestSection.ps1 b/Src/Private/Get-AbrForestSection.ps1 index b79bc29..a64b9c5 100644 --- a/Src/Private/Get-AbrForestSection.ps1 +++ b/Src/Private/Get-AbrForestSection.ps1 @@ -53,6 +53,11 @@ function Get-AbrForestSection { } catch { Write-PScriboMessage -IsWarning $_.Exception.Message } + try { + Get-AbrDHCPinAD + } catch { + Write-PScriboMessage -IsWarning $_.Exception.Message + } } } catch { Write-PScriboMessage -IsWarning "Error: Unable to retreive Forest: $ForestInfo information."