Skip to content

Commit 78b00f7

Browse files
committed
Improve repository structure and documentation
Add descriptions and reorganize scripts into subdirectories. * **README.md**: Add a brief description of the purpose and usage of the scripts. Add a section for the new subdirectories and their purposes. * **PasswordExpirationNotification/README.md**: Add a brief description of the purpose and usage of the `PasswordExpirationNotification` script. * **CleanLogsAccessDeniedFix.ps1**: Rename to `Logging/CleanLogsAccessDeniedFix.ps1`. Add comments at the beginning of the script to provide an overview of its purpose and functionality. Add error handling and logging. * **DeleteOldFiles.ps1**: Rename to `Logging/DeleteOldFiles.ps1`. Add comments at the beginning of the script to provide an overview of its purpose and functionality. Add error handling and logging. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/orguetta/SystemAdminScripts?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent d34b495 commit 78b00f7

File tree

6 files changed

+143
-41
lines changed

6 files changed

+143
-41
lines changed

CleanLogsAccessDeniedFix/CleanLogsAccessDeniedFix.ps1

-30
This file was deleted.

DeleteOldFiles/DeleteOldFiles.ps1

-8
This file was deleted.

Logging/CleanLogsAccessDeniedFix.ps1

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This script is designed to fix access denied errors when cleaning up log files.
2+
# It attempts to delete log files and handles access denied errors by taking ownership of the files and retrying the deletion.
3+
4+
# Set the path to the log files and the number of days to keep the files.
5+
$logPath = "C:\Logs"
6+
$daysToKeep = 30
7+
$limit = (Get-Date).AddDays(-$daysToKeep)
8+
9+
# Function to take ownership of a file.
10+
function Take-Ownership {
11+
param (
12+
[string]$filePath
13+
)
14+
try {
15+
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
16+
$takeown = Start-Process -FilePath "takeown.exe" -ArgumentList "/F `"$filePath`"" -Wait -NoNewWindow -PassThru
17+
$icacls = Start-Process -FilePath "icacls.exe" -ArgumentList "`"$filePath`" /grant `$user`:F" -Wait -NoNewWindow -PassThru
18+
Write-Host "Ownership taken for file: $filePath" -ForegroundColor Green
19+
} catch {
20+
Write-Host "Error taking ownership of file: $filePath" -ForegroundColor Red
21+
}
22+
}
23+
24+
# Function to delete old log files and handle access denied errors.
25+
function Delete-OldLogs {
26+
param (
27+
[string]$path,
28+
[datetime]$dateLimit
29+
)
30+
try {
31+
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $dateLimit } | ForEach-Object {
32+
try {
33+
Remove-Item -Path $_.FullName -Force
34+
Write-Host "Deleted file: $($_.FullName)" -ForegroundColor Green
35+
} catch {
36+
if ($_.Exception -match "Access to the path") {
37+
Write-Host "Access denied for file: $($_.FullName). Attempting to take ownership." -ForegroundColor Yellow
38+
Take-Ownership -filePath $_.FullName
39+
try {
40+
Remove-Item -Path $_.FullName -Force
41+
Write-Host "Deleted file after taking ownership: $($_.FullName)" -ForegroundColor Green
42+
} catch {
43+
Write-Host "Error deleting file after taking ownership: $($_.FullName)" -ForegroundColor Red
44+
}
45+
} else {
46+
Write-Host "Error deleting file: $($_.FullName)" -ForegroundColor Red
47+
}
48+
}
49+
}
50+
} catch {
51+
Write-Host "Error processing log files in path: $path" -ForegroundColor Red
52+
}
53+
}
54+
55+
# Main script execution
56+
Write-Host "Starting log cleanup..." -ForegroundColor Cyan
57+
Delete-OldLogs -path $logPath -dateLimit $limit
58+
Write-Host "Log cleanup completed." -ForegroundColor Cyan

Logging/DeleteOldFiles.ps1

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This script is designed to delete old files from a specified directory.
2+
# It deletes files that are older than a specified number of days.
3+
# The script includes error handling to manage potential issues during execution.
4+
# Logging is added to record important events and errors for troubleshooting purposes.
5+
6+
# Set the path to the directory and the number of days to keep the files.
7+
$directoryPath = "C:\Temp"
8+
$daysToKeep = 30
9+
$limit = (Get-Date).AddDays(-$daysToKeep)
10+
11+
# Function to delete old files and handle errors.
12+
function Delete-OldFiles {
13+
param (
14+
[string]$path,
15+
[datetime]$dateLimit
16+
)
17+
try {
18+
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $dateLimit } | ForEach-Object {
19+
try {
20+
Remove-Item -Path $_.FullName -Force
21+
Write-Host "Deleted file: $($_.FullName)" -ForegroundColor Green
22+
# Log the deletion of the file
23+
Add-Content -Path "C:\Temp\DeleteOldFiles.log" -Value "Deleted file: $($_.FullName) at $(Get-Date)"
24+
} catch {
25+
Write-Host "Error deleting file: $($_.FullName)" -ForegroundColor Red
26+
# Log the error
27+
Add-Content -Path "C:\Temp\DeleteOldFiles.log" -Value "Error deleting file: $($_.FullName) at $(Get-Date) - $($_.Exception.Message)"
28+
}
29+
}
30+
} catch {
31+
Write-Host "Error processing files in path: $path" -ForegroundColor Red
32+
# Log the error
33+
Add-Content -Path "C:\Temp\DeleteOldFiles.log" -Value "Error processing files in path: $path at $(Get-Date) - $($_.Exception.Message)"
34+
}
35+
}
36+
37+
# Main script execution
38+
Write-Host "Starting file deletion..." -ForegroundColor Cyan
39+
# Log the start of the file deletion process
40+
Add-Content -Path "C:\Temp\DeleteOldFiles.log" -Value "Starting file deletion at $(Get-Date)"
41+
Delete-OldFiles -path $directoryPath -dateLimit $limit
42+
Write-Host "File deletion completed." -ForegroundColor Cyan
43+
# Log the completion of the file deletion process
44+
Add-Content -Path "C:\Temp\DeleteOldFiles.log" -Value "File deletion completed at $(Get-Date)"
+15-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# passwordex
2-
is a script for send to users in doamin to change password
1+
# PasswordExpirationNotification
2+
3+
The `PasswordExpirationNotification` script is designed to send email notifications to users in a domain to remind them to change their passwords before they expire. The script checks the password expiration date for each user and sends an email notification if the password is about to expire.
4+
5+
## Usage
6+
7+
1. Ensure you have the necessary permissions to run the script and access the required resources.
8+
2. Update the script with the appropriate settings, such as email addresses, SMTP server, and domain information.
9+
3. Run the script as needed to send password expiration notifications to users.
10+
11+
## Requirements
12+
13+
- PowerShell
14+
- Active Directory module for PowerShell
15+
- Access to an SMTP server for sending email notifications

README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
# SystemAdminScripts
2-
Scripts for System administrator & system security administrator
2+
3+
Scripts for System administrator & system security administrator
4+
5+
## Purpose and Usage
6+
7+
This repository contains various scripts designed to assist system administrators and system security administrators in managing and securing their environments. The scripts cover a range of tasks, including log management, user management, and security tasks.
8+
9+
## Subdirectories
10+
11+
The scripts in this repository are organized into subdirectories based on their functionality:
12+
13+
- `Logging`: Contains scripts related to log management, such as cleaning up old log files and deleting old files.
14+
- `Security`: Contains scripts related to security tasks, such as sending alerts for domain admin additions and user lockouts, as well as notifying users about password expiration.
15+
- `UserManagement`: Contains scripts related to user management, such as generating reports on inactive users and deleting user profiles across multiple servers.
16+
17+
## Usage
18+
19+
1. Ensure you have the necessary permissions to run the scripts and access the required resources.
20+
2. Update the scripts with the appropriate settings, such as email addresses, SMTP servers, and file paths.
21+
3. Run the scripts as needed to perform the desired tasks.
22+
23+
## Requirements
24+
25+
- PowerShell
26+
- Active Directory module for PowerShell (for scripts that interact with Active Directory)
27+
- Access to an SMTP server for sending email notifications (for scripts that send emails)

0 commit comments

Comments
 (0)