Purge old files and folders from directory

So at work we have a couple of directories that generate logs on a regular basis. These logs really are not that important. So I keep them for 60 days.

Problem of course is that some also create folders.

So I created this powershell script from some pieces of code I found that will go through the folder and clean out all the old files and then clean out the old folders.

I’m sure I could clean this up a bit, but seeing it does what I want now it’s good enough. You just need to change some of the variables

#Days older than

$HowOld = -60

#Path to the root folder

$Path = "C:\directory you want purged\"

#Deletion files task

get-childitem $Path -recurse | where {$_.lastwritetime -lt (get-date).adddays($HowOld) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -verbose}

#Deletion empty folders task

do {

$dirs = gci $Path -directory -recurse | Where { (gci $_.fullName -Force).count -eq 0 } | select -expandproperty FullName

$dirs | Foreach-Object { Remove-Item $_ }

} while ($dirs.count -gt 0)

Powershell script to backup hyper-v servers

So while I do not have a hyper-v server now I did have one. I cobbled together this script out of some stuff I found online to basically allow me to backup a hyper-v server (meaning not a windows server just straight hyper-v) to a backup server and keep a couple of copies in case a backup went bad.

So here’s the script.

# This script it designed to retrieve all virtual machines from a remote hyper-v server and export them to a location of your choosing.
# You can set this up as a scheduled task based on your needs. So if you need backups daily, weekly, you can set that based on your needs.
# It will then remove any backups older than the amount of backups you configure.

# Enter the name of the server you want to backup
$server = "HYPERV-SERVER"
# Enter the full path for the backup folder
$backuppath = "\\backup server\Hyper-V Backups" # Example: "\\backupserver\fileshare\HyperVBackups"
# How many backups of VMs you want to keep
$backupstokeep = 7

# Get all Hyper-V VMs
$vms = Get-VM -computername $hypervserver
# Grab the current date
$today = Get-Date -Format MM-dd-yy

foreach ($vm in $vms) {
$vmname = $vm.Name
Write-Host "Backing up $vmname..."
# Create the folders for the backup
New-Item -ItemType Directory -Path "$backuppath\$vmname" # We run this in case it is a new VM. Normally it will fail if the VM folder already exists, which is fine
New-Item -ItemType Directory -Path "$backuppath\$vmname\$today"
# Export the VM
Export-VM -VM $vm -Path "$backuppath\$vmname\$today"
# Remove any backups older than the past 7 days
Get-ChildItem "$backuppath\$vmname" | Sort-Object -Property CreationTime -Descending | Select-Object -Skip $backupstokeep | Remove-Item -Recurse
}

Worked great for my needs. Especially when the only other options I could find cost $$

Windows Command Line Repair commands

Lately I find that doing some repair commands at the command prompt in windows seems to fix just about any of the “something isn’t working right” problems, but I know they’re not hardware related.

Anyway from an elevated command prompt these are the commands

1.

sfc /scannow

That fixes most of the issues.

2.

DISM.exe /Online /Cleanup-image /Scanhealth && DISM.exe /Online /Cleanup-image /Restorehealth

Now there’s two parts to the script. There’s the scan health part and the restore health part. You can run the first one by just running everything before the && but I typically run just both at the same time.

Now not that I’ve ever ran into it honestly but if it says something about “cannot find source files”

You need to get an iso of the exact same system  Then run this.

DISM /Online /Cleanup-Image /RestoreHealth /source:WIM:X:\Sources\Install.wim:1 /LimitAccess

Where the X is you need to change to the location of the drive where the iso is.

I’ll be honest I’d say 80% of the time the SFC command works if it’s just something that seems corrupt. The DISM I can’t say I’ve had a computer get fixed by that one, but I always run it to make certain things are working fine.

They’re fairly simple things you can do and it doesn’t hurt to try.

Pi-Hole Debian setup on VMware

So personally I don’t have any raspberry PIs. Just have 3 big VMware servers. So I decided to setup a PI-hole server for my network using a supported linux distro that PI-hole said it supported.

So here’s the results. For me it took 3 attempts to do it simply.

  • Ubuntu 18.04 – Had trouble with the network connection. After setup I couldn’t ping anything outside my network and dns didn’t appear to work.
  • Centos 7 – Couldn’t access the web interface.
  • Debian 9.5 – Was the choice that worked with minimal effort.

So here’s how it was done.

  • First I did an install of debian 9.5.0 from a net install iso. I gave the virtual machine 1 core of 1 processor and 2 gigs of ram
  • As far as the debian install I did the most basic install possible because for me the only thing this machine is going to do is function as a PI-hole.
  • After debian completed it’s install I had to install curl by doing.
apt-get install curl
    • Then once it was installed I did.
curl -sSL https://install.pi-hole.net | bash
  • Then I just went through the setup prompts of PI-hole, which are pretty self explanatory.

And that was pretty much it. After this everything works and is pretty much ready to be set up to your liking.

Delete files older than… in powershell

I have a couple of directories that create files

Here’s a script I use for deleting files older than 30 days in powershell.

$Path = "C:\temp"
$Daysback = "-30"
 
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

You need to chance the $Path location and if you want do delete files after another time frame you just have to edit the 30 to however many days you want.

Block a network device at a particular times

So I have to “turn off” the internet for computers in my house and I have an Edge Router X at the time of this post it is at v1.10.6

Here’s the steps you need to take.

  • Click Firewall/NAT
  • Click Firewall Policies
  • Click Add New Ruleset
  • Start Like this

  • Then click actions >> edit ruleset

  • Then click add new rule and make it look like this

  • Click source you can either put in the mac address or the ip address if the device has a static

  • The click time and put in the military time of when you want the block to start and end.

 

Welcome to my site!

This website was designed to both be an about me, a collection of things that I need as well as a simple straight forward blog of things that I’ve done that could help others.

For the longest time if I needed to do something that I was unfamiliar with I’d just google it. And while that most of the time works I have ran into a number of times where something hasn’t worked entirely correctly because.. well things change.

I’m really not big on following instructions from the web and then finding out about halfway through that the current version of Ubuntu doesn’t have the piece of software and someone just wrote the article based on what they knew about the previous version of Ubuntu.

Also for me software is a tool. I’m not big on the whole Windows, Mac, Linux game. I’m more about what is the easiest way to setup what I want to do.

So hopefully something here helps you out.