Skip to main content

Final exam part 2

Publication date: 2025-07-09 13:00
Submission date: 2025-07-09 15:00
Submission mode: Omnivox
Modality: Individual exam
Authorized material: Course notes, your practical work and exercises, online documentation, but no external help (no communication with other people)
Use of AI: According to the College policy. You can use AI for research, but the code must be entirely written by yourself.

Automated System Logs Management Script​

Context​

You are a system administrator in a company. Your manager asks you to create a PowerShell script to automate the management of log files. This script should allow for the regular cleaning, organizing, and analyzing of logs.

Work Objectives (100 points)​

Your PowerShell script must automatically perform the following actions:

1. Creation of the work environment (25 points)​

Your script must use the environment variable ENTERPRISE_DATA (already defined with a valid path, for example C:\EnterpriseLogs) to automatically create the following folder structure in $env:ENTERPRISE_DATA\AdminLogs:

$env:ENTERPRISE_DATA\AdminLogs
│
├── Logs_Actuels
│ ├── Systeme
│ ├── Applications
│ └── Securite
├── Archives
│ └── Anciens
└── Rapports
warning

The folders might already exist. Your script must check their existence and not cause errors if the folders are already present.

Then, the script must also create 5 fictitious log files in the Logs_Actuels\Systeme folder:

  • system_2025-07-01.log (content: "INFO: System startup successful")
  • system_2025-07-05.log (content: "ERROR: Hard drive error")
  • system_2025-07-08.log (content: "WARNING: Low memory")
  • system_2025-07-09.log (content: "INFO: System up to date")
  • old_system_2024-12-15.log (content: "INFO: Old log")

2. Analysis and search in logs (25 points)​

Your script must:

  • Search for all .log files in the $env:ENTERPRISE_DATA\AdminLogs\Logs_Actuels tree
  • Create a list of files containing the word "ERROR" and save it in $env:ENTERPRISE_DATA\AdminLogs\Rapports\erreurs_trouvees.txt

3. Automatic archiving (25 points)​

Your script must:

  • Identify all log files older than 7 days (use the date in the file name)
  • Move these old files to the Archives\Anciens folder and rename them by adding the prefix ARCHIVE_
    • Example: old_system_2024-12-15.log → ARCHIVE_old_system_2024-12-15.log
warning

Be careful, there might already be files in the Archives\Anciens folder. Your script must not add ARCHIVE_ to files already archived. (So there should not be a file named ARCHIVE_ARCHIVE_old_system_2024-12-15.log)

Your script can overwrite an existing file if the name is identical.

4. Final report and verification (25 points)​

Your script must generate a final report (rapport_gestion_logs.txt) in the $env:ENTERPRISE_DATA\AdminLogs\Rapports folder containing:

  • The date and time of the script execution
  • The number of archived files
  • The number of files containing errors
  • A confirmation message: "Script executed successfully"

Technical Requirements​

  1. Script structure: Your script must be well organized with comments explaining each important section.

  2. Idempotence: The script must be able to be executed multiple times without causing errors.

Useful PowerShell Commands​

  • New-Item: Create files and folders
  • Set-Content / Add-Content: Write to files
  • Get-ChildItem: List files and folders
  • Select-String: Search text in files
  • Copy-Item: Copy files
  • Move-Item: Move files
  • Rename-Item: Rename files
  • Test-Path: Check the existence of a file/folder
  • Get-Date: Get the current date

Deliverable​

You must submit your PowerShell script named gestion_logs.ps1 on Omnivox.

Good luck!