Skip to content

Commit fb12935

Browse files
SamErdeCopilot
andcommitted
🐛 fix(scripts): address PR review findings
Resolve remaining PR review feedback for pipeline output, unused variables, progress preference restoration, preserved error records, DNS lookup API usage, SID comments, and PSSession cleanup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b532b69 commit fb12935

7 files changed

Lines changed: 25 additions & 31 deletions

Active Directory/Export-AllADUserTransitiveGroupMemberships.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,17 @@ begin {
148148
}
149149

150150
$CurrentProgressPreference = Get-Variable -Name ProgressPreference -ValueOnly
151-
Set-Variable -Name ProgressPreference -Value 'SilentlyContinue' -Scope Global -Force -ErrorAction SilentlyContinue
152-
# Check if the global catalog server is available on the specified port.
153-
if (-not (Test-NetConnection -ComputerName $Server -Port $Port -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
154-
if (-not (Test-NetConnection -ComputerName $Server -Port $AltPort -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
155-
throw "Unable to connect to the global catalog server '$Server' on port '$Port' or '$AltPort.'"
151+
try {
152+
Set-Variable -Name ProgressPreference -Value 'SilentlyContinue' -Scope Global -Force -ErrorAction SilentlyContinue
153+
# Check if the global catalog server is available on the specified port.
154+
if (-not (Test-NetConnection -ComputerName $Server -Port $Port -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
155+
if (-not (Test-NetConnection -ComputerName $Server -Port $AltPort -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
156+
throw "Unable to connect to the global catalog server '$Server' on port '$Port' or '$AltPort.'"
157+
}
156158
}
159+
} finally {
160+
Set-Variable -Name ProgressPreference -Value $CurrentProgressPreference -Scope Global -Force -ErrorAction SilentlyContinue
157161
}
158-
Set-Variable -Name ProgressPreference -Value $CurrentProgressPreference -Scope Global -Force -ErrorAction SilentlyContinue
159162
}
160163

161164
process {

Active Directory/Get-ADObjectFromPipeline.ps1

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ function Get-ADObjectFromPipeline {
1515

1616
begin {
1717
Import-Module ActiveDirectory
18-
$GlobalCatalog = Get-ADDomainController -Discover -Service GlobalCatalog
1918
}
2019

2120
process {
@@ -36,25 +35,15 @@ function Get-ADObjectFromPipeline {
3635
switch ($IdentityType) {
3736
'user' {
3837
# Not Complete
39-
$User = Get-ADUser -Identity $Identity -Properties PrimaryGroup,SidHistory
38+
Get-ADUser -Identity $Identity -Properties PrimaryGroup,SidHistory
4039
}
4140
'computer' {
4241
# Not Complete
43-
$Computer = Get-ADComputer -Identity $Identity -Properties PrimaryGroup,SidHistory
42+
Get-ADComputer -Identity $Identity -Properties PrimaryGroup,SidHistory
4443
}
4544
Default {
4645
Write-Error "Identity type not supported."
4746
}
4847
}
4948
}
50-
51-
end {
52-
# Do something and/or return the resulting object to the pipeline.
53-
if ($User) {
54-
$User
55-
}
56-
if ($Computer) {
57-
$Computer
58-
}
59-
}
6049
}

Active Directory/Get-ADUserTransitiveGroupMembership.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ function Get-ADUserTransitiveGroupMembership {
5757
}
5858

5959
$CurrentProgressPreference = Get-Variable -Name ProgressPreference -ValueOnly
60-
Set-Variable -Name ProgressPreference -Value 'SilentlyContinue' -Force -Scope Global -ErrorAction SilentlyContinue
61-
# Check if the global catalog server is available on the specified port.
62-
if (-not (Test-NetConnection -ComputerName $Server -Port $Port -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
63-
if (-not (Test-NetConnection -ComputerName $Server -Port $AltPort -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
64-
throw "Unable to connect to the global catalog server '$Server' on port '$Port' or '$AltPort.'"
60+
try {
61+
Set-Variable -Name ProgressPreference -Value 'SilentlyContinue' -Force -Scope Global -ErrorAction SilentlyContinue
62+
# Check if the global catalog server is available on the specified port.
63+
if (-not (Test-NetConnection -ComputerName $Server -Port $Port -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
64+
if (-not (Test-NetConnection -ComputerName $Server -Port $AltPort -InformationLevel Quiet -ErrorAction SilentlyContinue)) {
65+
throw "Unable to connect to the global catalog server '$Server' on port '$Port' or '$AltPort.'"
66+
}
6567
}
68+
} finally {
69+
Set-Variable -Name ProgressPreference -Value $CurrentProgressPreference -Force -Scope Global -ErrorAction SilentlyContinue
6670
}
67-
Set-Variable -Name ProgressPreference -Value $CurrentProgressPreference -Force -Scope Global -ErrorAction SilentlyContinue
6871
}
6972

7073
process {

DDI/Get Hostnames from CSV IP Addresses.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ $IPAddressList | foreach-object {
1010
$_.Hostname = ([System.Net.Dns]::GetHostEntry($ip)).HostName
1111
}
1212
catch {
13-
Write-Error $_ #.Exception.Message.Split(':')[1]
13+
Write-Error -ErrorRecord $_ #.Exception.Message.Split(':')[1]
1414
}
1515
}
1616
# Write the data back to the CSV with the hostnames added.

Exchange/Parse-TransportLogs.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ foreach ($item in $TestSet) {
5656
$item.MessageID = $data
5757
$item.Subject = $subject
5858
try {
59-
$item.Hostname = ([System.Net.DNS]::GetHostbyAddress($item.IPAddress)).Hostname
59+
$item.Hostname = ([System.Net.DNS]::GetHostEntry($item.IPAddress)).HostName
6060
} catch {
6161
# Hostname not found for this IP; leave blank.
6262
}

Windows/Activate and Get License.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ $registryPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\MfaRequiredI
2222
$registryValueName = 'Verify Multi-factor Authentication in ClipRenew'
2323
$registryValueData = 0 # DWORD value of 0
2424
$sid = New-Object System.Security.Principal.SecurityIdentifier('S-1-5-4')
25-
# SID for the Everyone group
26-
# or SID S-1-5-4 for the interactive group
25+
# SID S-1-5-4 is the Interactive group.
2726

2827
# Check if the registry key already exists
2928
if (-not (Test-Path -Path $registryPath)) {
@@ -35,7 +34,7 @@ if (-not (Test-Path -Path $registryPath)) {
3534
Write-Output 'Registry key already exists. No changes made.'
3635
}
3736

38-
# Add read permissions for SID (S-1-1-0, Everyone) to the registry key with inheritance
37+
# Add read permissions for the Interactive SID (S-1-5-4) to the registry key with inheritance
3938
$acl = Get-Acl -Path $registryPath
4039
$ruleSID = New-Object System.Security.AccessControl.RegistryAccessRule($sid, 'ReadKey', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
4140
$acl.AddAccessRule($ruleSID)

Windows/Push-DNSClientServerAddresses.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ foreach ($server in $servers)
4242
Write-Output "Failed to change the DNS client server address on $serverName"
4343
}
4444
finally {
45-
if ($s) { Remove-PSSession -Session $s }
45+
if ($s) { Remove-PSSession -Session $s -ErrorAction SilentlyContinue }
4646
}
4747
} # End foreach server loop.

0 commit comments

Comments
 (0)