Automatically Deleting Files and Folders When Disk Space is Low

If you need to automatically delete files and folders when disk space on your drive falls below a certain threshold, you can use a PowerShell script to accomplish this task. Here’s how you can set it up.

PowerShell Script

This script will delete files and directories under a specified path when the available disk space is less than 5 GB.

# Define the path to the directory
$directoryPath = "C:\DeleteFilesFolder\

# Set the threshold for disk space (in bytes)
$threshold = 5GB

# Get the drive information
$drive = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -eq "C:\" }

# Check the available free space on the drive
if ($drive.Free -lt $threshold) {
# Get all files in the specified directory and subdirectories
$files = Get-ChildItem -Path $directoryPath -Recurse -File

# Loop through and delete each file
foreach ($file in $files) {
Remove-Item -Path $file.FullName -Force
Write-Output "Deleted file: $($file.FullName)"
}

# Get all directories in the specified directory and subdirectories
$directories = Get-ChildItem -Path $directoryPath -Recurse -Directory | Sort-Object -Property FullName -Descending

# Loop through and delete each directory if it is empty
foreach ($directory in $directories) {
Remove-Item -Path $directory.FullName -Recurse -Force
Write-Output "Deleted directory: $($directory.FullName)"
}

Write-Output "All files and directories in $directoryPath and its subdirectories have been deleted due to low disk space."
} else {
Write-Output "Sufficient disk space available: $($drive.Free / 1GB) GB free."
}

Explanation of the Script

  1. $directoryPath: Specifies the directory path where files and folders will be deleted.
  2. $threshold: Sets the disk space threshold to 5 GB.
  3. $drive: Checks the free space available on the C: drive.
  4. if ($drive.Free -lt $threshold): Checks if the available free space is less than 5 GB.
  5. Get-ChildItem -Path $directoryPath -Recurse -File: Gets all files in the specified directory and subdirectories.
  6. Remove-Item -Path $file.FullName -Force: Deletes each file without user interaction.
  7. Get-ChildItem -Path $directoryPath -Recurse -Directory | Sort-Object -Property FullName -Descending: Gets all directories in descending order to ensure deletion from the deepest level first.
  8. foreach ($directory in $directories): Deletes each directory and its contents without user interaction if it’s empty.

Running the Script Manually

  1. Open PowerShell as Administrator.
  2. Run the Script:
    C:\Scripts\ClearIncomingQueue.ps1

Automating the Script with Task Scheduler

You can use Task Scheduler to automate the script.

  1. Open Task Scheduler: Open it by typing taskschd.msc in the Run dialog.
  2. Create a New Task:
    • Right-click and select “Create Task”.
    • General tab: Name your task (e.g., “Clear Files and Folders”).
    • Triggers tab: Click “New” and choose the schedule (e.g., daily).
    • Actions tab: Click “New” and select “Start a program”. Enter powershell.exe as the program/script.
    • In the “Add arguments (optional)” field, enter:
      -File "C:\Scripts\ClearIncomingQueue.ps1"
    • Conditions tab: Check “Start the task only if the computer is on AC power”.
    • Settings tab: Check “If the task fails, restart every” and set an interval.

This setup ensures that files and directories under the specified path are automatically deleted when the available disk space falls below 5 GB, without any user interaction.

Simple , Easy, Useful

Have a nice day !

How to Upgrade Failover Cluster Functional Level

When you add new nodes with a higher operating system to your Windows Failover cluster environment, you will receive warnings about "Functional Level" mismatch in your failover cluster logs.

The main reason for these warnings is that the operating systems and functional levels of the clusters connected to the nodes in your environment are different from each other.

You can follow the steps below to change the functional level compatibility;

  • To view the Failover Cluster Functional Level version
    Get-Cluster | select ClusterFunctionalLevel
  • To upgrade the Failover Cluster Functional Level version
    Update-ClusterFunctionalLevel
  • To view the upgrade process of the Failover Cluster Functional Level version
    Get-Cluster | select ClusterFunctionalLevel
  • In Windows Server 2019 the Clustering team introduced a new PowerShell cmdlet to check how many nodes of the cluster are running on which level
    "Get-ClusterNodeSupportedVersion" helps you to identify the Cluster Functional Level and the Cluster Upgrade Version.

The table below shows the values and each corresponding functional level:

 

For more detailed information;

Have a nice day!

How to Fix Failed While Applying Switch Port Settings ‘Ethernet Switch Port VLAN Settings’

If you get the following error when you want to make a VLAN change on your virtual server that you use on Hyperv;

"Error applying Network Adapter changes"
"The operation failed. Failed while applying switch port settings 'Ethernet Switch Port VLAN Settings' on switch 'Vs': One or more arguments are invalid (0x80070057)."

The root cause of this problem is the network mode you use on your virtual server, for example F5 virtual server appliance.
Let’s continue with the example there are more than one ethernet card over the F5 virtual server appliance, some of which are trunk and some are access mode.

You will get an error when you make the VLAN change on the Trunk mode card from the Hyper V Failover Cluster gui.

To fix this problem, you can follow the steps below;

  • First of all, we view the cards and their modes on the virtual server.
    Get-VMNetworkAdapterVlan -VMName servername
  • The tag is removed the network card with trunk mode.
    Set-VMNetworkAdapterVlan -VMName servername -VMNetworkAdapterName NIC3 -untagged
  • We define access mode and VLAN ID.
    Set-VMNetworkAdapterVlan -VMName servername -VMNetworkAdapterName NIC3 -Access -VlanId 715
  • To view the change made.
    Get-VMNetworkAdapterVlan -VMName servername

Have a nice day!

 

How to In-Place Upgrade Windows Server 2016 to Windows Server 2019 ?

What is the in-place upgrade?
In the simplest terms, it is the process of upgrading your Windows operating system without losing your data.

If you are going to do this on a physical server, it is useful to check the hardware compatibility first.
Hardware requirements for Windows Server

You can also see the version transitions of the operating systems in the table below.

If your server is located on a virtual platform, it is useful to take a snapshot/checkpoint before doing the in-place upgrade.

After connecting the “Windows Server 2019” .iso file to your server with the “Windows Server 2016” operating system, "setup.exe" should be run.

On the next screen, we are asked whether to install the updates. The recommended action here is to make updates, and we continue by ticking this option.

Select the windows version.

Select Accept to accept the terms of your licensing agreement

Attention! Since our preference is an in-place upgrade, we continue with the "Keep personal files and apps" option.

Depending on the size of your data, the in-place upgrade process may take an average of 30 minutes to 1 hour.

 

If you have a server with the "Windows Server 2012 R2" operating system, you can’t upgrade directly to “Windows Server 2019”.
You will upgrade  "Windows Server 2016" first, then you can upgrade to "Windows Server 2019".

 

Ps: Disable UAC before starting the in-place upgrade, then enable it again.

Have a nice day!