Skip to content

Commit 4180818

Browse files
authored
Merge pull request #13 from leonardokr/improve/code-quality-and-docs
Refactor: code quality improvements, documentation fixes, and Pester tests
2 parents 6474d88 + 94625d1 commit 4180818

18 files changed

Lines changed: 177 additions & 68 deletions

.github/workflows/validate.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ jobs:
1919
Write-Host "PowerShell Version: $($PSVersionTable.PSVersion)"
2020
Write-Host "OS: $($PSVersionTable.OS)"
2121
22-
- name: Install PSScriptAnalyzer
22+
- name: Install Modules
2323
shell: pwsh
2424
run: |
2525
Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
26+
Install-Module -Name Pester -Force -Scope CurrentUser -SkipPublisherCheck
2627
2728
- name: Run PSScriptAnalyzer
2829
shell: pwsh
@@ -96,3 +97,12 @@ jobs:
9697
} else {
9798
Write-Host "All scripts have proper documentation!"
9899
}
100+
101+
- name: Run Pester Tests
102+
shell: pwsh
103+
run: |
104+
$config = New-PesterConfiguration
105+
$config.Run.Path = "Tests"
106+
$config.Run.Exit = $true
107+
$config.Output.Verbosity = "Detailed"
108+
Invoke-Pester -Configuration $config

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ Organize scripts into appropriate categories:
103103
- **NetworkShares** - SMB shares and DFS management
104104
- **TaskScheduler** - Scheduled task management
105105
- **UserProfiles** - Windows user profile operations
106-
- **SystemMaintenance** - General system maintenance
106+
- **System** - System-level configuration and maintenance
107+
- **Registry** - Windows Registry configuration
107108
- **Monitoring** - System and service monitoring
108109
- **Security** - Security-related operations
109110

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# PowerShell Scripts Collection
22

3+
[![PowerShell Script Validation](https://github.com/leonardokr/powershell-scripts/actions/workflows/validate.yml/badge.svg)](https://github.com/leonardokr/powershell-scripts/actions/workflows/validate.yml)
4+
35
A collection of PowerShell scripts for Windows system administration, Active Directory management, and IT automation tasks.
46

57
## 📁 Repository Structure
@@ -20,7 +22,7 @@ Scripts/
2022
### Active Directory
2123
- **Get-DeletedUsers.ps1** - Exports deleted AD users within a specified date range
2224
- **Get-UserLastLogon.ps1** - Reports user last logon times and group memberships
23-
- **Send-PasswordExpiryNotification** - Password expiration notification for AD users.
25+
- **Send-PasswordExpiryNotification.ps1** - Password expiration notification for AD users
2426

2527
### File System
2628
- **Get-FolderPermissions.ps1** - Audits folder permissions across multiple servers

SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This project supports the following PowerShell versions:
1515
If you discover a security vulnerability in any of these scripts, please follow these steps:
1616

1717
1. **Do not** create a public GitHub issue
18-
2. Send an email to the repository maintainer with:
18+
2. Send an email to the repository maintainer via [GitHub profile](https://github.com/leonardokr) with:
1919
- Description of the vulnerability
2020
- Steps to reproduce
2121
- Potential impact

Scripts/ActiveDirectory/Get-DeletedUsers.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<#
1+
#Requires -Modules ActiveDirectory
2+
3+
<#
24
.SYNOPSIS
35
Exports deleted Active Directory users within a specified date range.
46
@@ -30,7 +32,8 @@
3032
Author : Leonardo Klein Rezende
3133
Prerequisite : Active Directory PowerShell module
3234
Creation Date : 2025-09-04
33-
35+
Version : 1.0.0
36+
3437
Requires Domain Admin or equivalent permissions to query deleted objects.
3538
3639
.LINK
@@ -95,7 +98,3 @@ catch {
9598

9699
Write-Information "Script execution completed." -InformationAction Continue
97100

98-
99-
100-
101-

Scripts/ActiveDirectory/Get-UserLastLogon.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<#
1+
#Requires -Modules ActiveDirectory
2+
3+
<#
24
.SYNOPSIS
35
Reports Active Directory user last logon times and group memberships.
46
@@ -34,7 +36,8 @@
3436
Author : Leonardo Klein Rezende
3537
Prerequisite : Active Directory PowerShell module
3638
Creation Date : 2025-09-04
37-
39+
Version : 1.0.0
40+
3841
LastLogon attribute may not be accurate in multi-DC environments.
3942
Consider using lastLogonTimestamp for more accurate results across DCs.
4043
@@ -98,6 +101,7 @@ try {
98101
@{Name = "Enabled"; Expression = { $_.Enabled } },
99102
@{Name = "GroupMemberships"; Expression = {
100103
if ($_.MemberOf) {
104+
# Extract group common names from DN format (e.g., "CN=GroupName,OU=Groups,DC=..." -> "GroupName")
101105
($_.MemberOf -replace '^CN=|,(OU|CN).+') -join ";"
102106
}
103107
else {
@@ -124,7 +128,3 @@ catch {
124128

125129
Write-Information "Script execution completed." -InformationAction Continue
126130

127-
128-
129-
130-

Scripts/ActiveDirectory/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ Automated password expiration notification system for Active Directory users.
6262
**Usage:**
6363

6464
```powershell
65-
# Run with default settings
66-
.\Send-PasswordExpiryNotification.ps1
65+
# Run with specified SMTP server and search base
66+
.\Send-PasswordExpiryNotification.ps1 -SmtpServer "mail.company.com" -FromAddress "noreply@company.com" -SearchBase "OU=Users,DC=domain,DC=com"
6767
6868
# Run in test mode with logging enabled
69-
.\Send-PasswordExpiryNotification.ps1 -testMode -enableLogging
70-
71-
# Specify custom search base
72-
.\Send-PasswordExpiryNotification.ps1 -searchBase "OU=CompanyUsers,DC=domain,DC=com"
69+
.\Send-PasswordExpiryNotification.ps1 -SmtpServer "mail.company.com" -FromAddress "noreply@company.com" -SearchBase "OU=Users,DC=domain,DC=com" -TestMode -EnableLogging
70+
```
7371

7472
## Prerequisites
7573

@@ -85,4 +83,3 @@ Automated password expiration notification system for Active Directory users.
8583
- Always test in non-production environment first
8684
- Review and modify configuration variables before production use
8785
- Ensure SMTP server allows relay from execution host
88-
```

Scripts/ActiveDirectory/Send-PasswordExpiryNotification.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
Author : Leonardo Klein Rezende
5151
Prerequisite : PowerShell 5.1+, ActiveDirectory Module, SMTP Server Access
5252
Creation Date : 2025-09-04
53+
Version : 1.0.0
5354
5455
.LINK
5556
https://github.com/leonardokr/powershell-scripts

Scripts/FileSystem/Get-FolderPermissions.ps1

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
Author : Leonardo Klein Rezende
3737
Prerequisite : PowerShell remoting and administrative access to target servers
3838
Creation Date : 2025-09-04
39-
39+
Version : 1.0.0
40+
4041
Requires administrative privileges on target servers.
4142
Large folder structures may take considerable time to process.
4243
@@ -69,7 +70,7 @@ Write-Information "Starting folder permissions audit..." -InformationAction Cont
6970
Write-Information "Target servers: $($ServerList -join ', ')" -InformationAction Continue
7071
Write-Information "Base path: $BasePath" -InformationAction Continue
7172

72-
$Report = @()
73+
$Report = [System.Collections.Generic.List[PSObject]]::new()
7374
$ProcessedServers = 0
7475
$TotalServers = $ServerList.Count
7576

@@ -117,7 +118,7 @@ foreach ($Server in $ServerList) {
117118
'Inherited' = $Access.IsInherited
118119
'ScanDate' = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
119120
}
120-
$Report += New-Object -TypeName PSObject -Property $Properties
121+
$Report.Add([PSCustomObject]$Properties)
121122
}
122123
}
123124
catch {
@@ -153,7 +154,3 @@ else {
153154

154155
Write-Information "Script execution completed." -InformationAction Continue
155156

156-
157-
158-
159-

Scripts/NetworkShares/New-ShareAndDFS.ps1

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
.PARAMETER ServerName
1616
The server name for DFS targets. Default is current computer name.
1717
18+
.PARAMETER ShareAccess
19+
The identity to grant full access on created SMB shares. Default is "Everyone".
20+
Modify to restrict access as needed (e.g., "Domain Users", "DOMAIN\ShareGroup").
21+
1822
.PARAMETER LogPath
1923
Path for the log file. Default is "C:\Logs".
2024
@@ -36,7 +40,8 @@
3640
Author : Leonardo Klein Rezende
3741
Prerequisite : DFSN PowerShell module, Administrative privileges
3842
Creation Date : 2025-08-10
39-
43+
Version : 1.0.0
44+
4045
Requires:
4146
- Administrative privileges
4247
- DFS Management features installed
@@ -47,8 +52,11 @@
4752
https://docs.microsoft.com/en-us/powershell/module/smbshare/
4853
#>
4954

55+
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSReviewUnusedParameter', 'ShareAccess',
56+
Justification = 'Used in New-ShareFolder function scope')]
57+
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSReviewUnusedParameter', 'EnableDebugMode',
58+
Justification = 'Used in Write-ScriptLog function scope')]
5059
[CmdletBinding(SupportsShouldProcess)]
51-
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSReviewUnusedParameter', 'EnableDebugMode', Justification = 'Used in Write-ScriptLog function scope')]
5260
param (
5361
[Parameter(Mandatory = $false)]
5462
[ValidateScript({ Test-Path $_ -PathType Container })]
@@ -60,6 +68,9 @@ param (
6068
[Parameter(Mandatory = $false)]
6169
[string]$ServerName = $env:COMPUTERNAME,
6270

71+
[Parameter(Mandatory = $false)]
72+
[string]$ShareAccess = "Everyone",
73+
6374
[Parameter(Mandatory = $false)]
6475
[string]$LogPath = "C:\Logs",
6576

@@ -119,7 +130,7 @@ function New-SMBShareSafe {
119130
}
120131

121132
if ($PSCmdlet.ShouldProcess($Name, "Create SMB share")) {
122-
New-SmbShare -Name $Name -Path $Path -Description $Description -FullAccess "Everyone"
133+
New-SmbShare -Name $Name -Path $Path -Description $Description -FullAccess $ShareAccess
123134
Write-ScriptLog "SMB share '$Name' created successfully" 'Info'
124135
return $true
125136
}
@@ -188,7 +199,7 @@ try {
188199

189200
$successCount = 0
190201
$failureCount = 0
191-
$results = @()
202+
$results = [System.Collections.Generic.List[PSObject]]::new()
192203

193204
foreach ($folder in $folders) {
194205
$folderName = $folder.Name
@@ -220,13 +231,13 @@ try {
220231
Write-ScriptLog "Failed to process folder '$folderName': $message" 'Error'
221232
}
222233

223-
$results += [PSCustomObject]@{
234+
$results.Add([PSCustomObject]@{
224235
FolderName = $folderName
225236
ShareName = $shareName
226237
FolderPath = $folderPath
227238
Status = $status
228239
Message = $message
229-
}
240+
})
230241
}
231242

232243
Write-ScriptLog "Processing completed" 'Info'

0 commit comments

Comments
 (0)