Snippet Name: Dynamic_SSRS_Log_Cleanup
Tags:
Created Date: 2025-06-16 13:12:39
Last Modified Date: 2025-05-31 10:31:47
Dependencies:
DBA Notes:
Sample Output:
Title: Dynamic SSRS Log Cleanup
Author: user
i had an issue where the SSRS log folder had a lot of *.log and *mdmp files that was causing disk space alerts. i wrote the code attached, which works perfectly, but now i need it to be more flexible, and not hardcoded to a specific drive or specific SQL version name, and folder assumption. help me modify the code to check for each drive on the server, and find any folder with the subdirectories \Reporting Services\LogFiles
Author: ChatGPT
Lowell wrote a PowerShell script to clean SSRS log files and dump files that currently uses hardcoded paths for the drive, SQL version, and folder assumptions. He wants to modify it to dynamically check each drive on the server and find folders with the subdirectory `\Reporting Services\LogFiles`.
Author: tool
Model set context updated.
Author: ChatGPT
To modify your PowerShell script to dynamically check each drive and locate any folder containing the `\Reporting Services\LogFiles` subdirectory, you can use a combination of `Get-PSDrive` to get the available drives and `Get-ChildItem` to recursively search for the folder. Here's an updated approach to make it more flexible:
```powershell
# Function to find and clean SSRS log files across all drives
function Clean-SSRSLogFiles {
# Get all the logical drives
$drives = Get-PSDrive -PSProvider FileSystem
foreach ($drive in $drives) {
try {
# Search for \Reporting Services\LogFiles folder on each drive
$logFileDirs = Get-ChildItem -Path "$($drive.Root)" -Recurse -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like '*\Reporting Services\LogFiles' }
foreach ($dir in $logFileDirs) {
Write-Host "Processing log files in: $($dir.FullName)"
# Find and remove *.log and *.mdmp files
Get-ChildItem -Path $dir.FullName -Filter *.log -Recurse -ErrorAction SilentlyContinue |
ForEach-Object {
Write-Host "Removing log file: $($_.FullName)"
Remove-Item $_.FullName -Force
}
Get-ChildItem -Path $dir.FullName -Filter *.mdmp -Recurse -ErrorAction SilentlyContinue |
ForEach-Object {
Write-Host "Removing dump file: $($_.FullName)"
Remove-Item $_.FullName -Force
}
}
}
catch {
Write-Host "Error searching in drive: $($drive.Root)" -ForegroundColor Red
}
}
}
# Run the clean-up function
Clean-SSRSLogFiles
```
### Key Modifications:
1. **Dynamic Drive Checking**: Uses `Get-PSDrive` to get all file system drives dynamically.
2. **Recursive Search**: Searches for any `\Reporting Services\LogFiles` folder using `Get-ChildItem` with `-Recurse` and filters the path.
3. **File Deletion**: Deletes both `*.log` and `*.mdmp` files within the discovered folders.
You can further enhance this by adding logging, error handling, and more options (like dry run mode) as needed. Let me know if you need additional features!
Author: user
your code looks like it requires being run ont he server, rather than remotely, am i incorrect?
Author: ChatGPT
You're correct. The script as written assumes it will be run locally on the server because it accesses the drives directly using `Get-PSDrive` and `Get-ChildItem`. If you want to run it remotely from another machine, you'd need to modify it to use PowerShell Remoting to access the server and perform these actions.
To enable this, you can use `Invoke-Command` or `Enter-PSSession` to run the script remotely. Here's how you could adjust it using `Invoke-Command` for remoting:
### Steps to Enable PowerShell Remoting
1. Ensure PowerShell remoting is enabled on the target server:
```powershell
Enable-PSRemoting -Force
```
2. You can then modify the script to run on the remote server like this:
```powershell
$server = "YourServerName"
Invoke-Command -ComputerName $server -ScriptBlock {
# Function to find and clean SSRS log files across all drives
function Clean-SSRSLogFiles {
# Get all the logical drives
$drives = Get-PSDrive -PSProvider FileSystem
foreach ($drive in $drives) {
try {
# Search for \Reporting Services\LogFiles folder on each drive
$logFileDirs = Get-ChildItem -Path "$($drive.Root)" -Recurse -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like '*\Reporting Services\LogFiles' }
foreach ($dir in $logFileDirs) {
Write-Host "Processing log files in: $($dir.FullName)"
# Find and remove *.log and *.mdmp files
Get-ChildItem -Path $dir.FullName -Filter *.log -Recurse -ErrorAction SilentlyContinue |
ForEach-Object {
Write-Host "Removing log file: $($_.FullName)"
Remove-Item $_.FullName -Force
}
Get-ChildItem -Path $dir.FullName -Filter *.mdmp -Recurse -ErrorAction SilentlyContinue |
ForEach-Object {
Write-Host "Removing dump file: $($_.FullName)"
Remove-Item $_.FullName -Force
}
}
}
catch {
Write-Host "Error searching in drive: $($drive.Root)" -ForegroundColor Red
}
}
}
# Run the clean-up function
Clean-SSRSLogFiles
}
```
### Key Changes for Remote Execution:
- **`Invoke-Command`**: This is used to run the entire script block on the specified remote server.
- **PowerShell Remoting**: Ensure that remoting is enabled on the target server, and that the client machine has the appropriate permissions.
This way, the script will run on the remote server, but you can trigger it from your local machine. Does this approach work for your setup?