Friday, October 18, 2019

Server Cleanup - because I'll need this later

$tempfolders = @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*", "C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\*", "C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\*", "C:\Users\*\AppData\Roaming\Neos Eureka S.r.l\*", "C:\Users\*\AppData\Roaming\EurekaLab s.a.s\*", "C:\Users\*\AppData\Local\Microsoft\Windows\WebCache\*", "C:\Windows\Logs\CBS\*persist*.*","C:\ProgramData\Exclaimer Ltd\Mail Disclaimers\SentItemsUpdate\Cache\*","C:\Users\*\*.regtrans-ms","C:\Users\*\appdata\local\*.regtrans-ms","C:\Users\*\AppData\Local\Microsoft\Windows\*.regtrans-ms","C:\Users\*\AppData\Local\Microsoft\Windows\*.blf","C:\Users\*\*.tm.blf","C:\Users\*\AppData\Local\Microsoft\MessageAnalyzer\CompilationCache\*","C:\Users\*\AppData\Local\Microsoft\MessageAnalyzer\CodecCache\*","C:\Users\*\AppData\Local\Google\Chrome\User Data\Default\Cache\*","C:\Users\*\AppData\Local\Microsoft\Terminal Server Client\Cache\*","C:\Users\*\AppData\Local\Microsoft\Windows\WebCache\*", "C:\Users\*\AppData\Local\SquirrelTemp\*", "C:\Users\*\AppData\Local\Microsoft\Windows\INetCache\IE\*", "C:\Windows\debug\UserMode\*.bak")
Remove-Item $tempfolders -force -recurse

$DaysToDelete = 0

$temporaryIEDir = "C:\users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" ## Remove all files and folders in user's Temporary Internet Files.
$cachesDir = "C:\Users\*\AppData\Local\Microsoft\Windows\Caches"  ## Remove all IE caches.
$cookiesDir = "C:\Documents and Settings\*\Cookies\*" ## Delets all cookies.
$locSetDir = "C:\Documents and Settings\*\Local Settings\Temp\*"  ## Delets all local settings temp
$locSetIEDir = "C:\Documents and Settings\*\Local Settings\Temporary Internet Files\*"   ## Delets all local settings IE temp
$locSetHisDir = "C:\Documents and Settings\*\Local Settings\History\*"  ## Delets all local settings history

Get-ChildItem $temporaryIEDir, $cachesDir, $cookiesDir, $locSetDir, $locSetIEDir, $locSetHisDir -Recurse -Force -Verbose -ErrorAction SilentlyContinue | Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete)) } | remove-item -force -Verbose -recurse -ErrorAction SilentlyContinue

$DaysToDelete = 0

$crLauncherDir = "C:\Documents and Settings\%USERNAME%\Local Settings\Application Data\Chromium\User Data\Default"
$chromeDir = "C:\Users\*\AppData\Local\Google\Chrome\User Data\Default"
$chromeSetDir = "C:\Users\*\Local Settings\Application Data\Google\Chrome\User Data\Default"

$Items = @("*Archived History*", "*Cache*", "*Cookies*", "*History*", "*Login Data*", "*Top Sites*", "*Visited Links*", "*Web Data*")

$items | ForEach-Object {
$item = $_
Get-ChildItem $crLauncherDir, $chromeDir, $chromeSetDir -Recurse -Force -ErrorAction SilentlyContinue |
    Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete)) -and $_ -like $item} | ForEach-Object -Process { Remove-Item $_ -force -Verbose -recurse -ErrorAction SilentlyContinue }
}

Write-Host -ForegroundColor yellow "#######################################################"
""
Write-Host -ForegroundColor Green "Powershell commands to delete cache & cookies in Firefox, Chrome & IE browsers"
Write-Host -ForegroundColor Green "By Lee Bhogal, Paradise Computing Ltd - June 2014"
Write-Host -ForegroundColor Green "VERSION: 2"
""
Write-Host -ForegroundColor yellow "#######################################################"
""
Write-Host -ForegroundColor Green "CHANGE_LOG:
v2.4: - Resolved *.default issue, issue was with the file path name not with *.default, but issue resolved
v2.3: - Added Cache2 to Mozilla directories but found that *.default is not working
v2.2: - Added Cyan colour to verbose output
v2.1: - Added the location 'C:\Windows\Temp\*' and 'C:\`$recycle.bin\'
v2:   - Changed the retrieval of user list to dir the c:\users folder and export to csv
v1:   - Compiled script"
""
Write-Host -ForegroundColor yellow "#######################################################"
""
#########################
"-------------------"
Write-Host -ForegroundColor Green "SECTION 1: Getting the list of users"
"-------------------"
# Write Information to the screen
Write-Host -ForegroundColor yellow "Exporting the list of users to c:\users\%username%\users.csv"
# List the users in c:\users and export to the local profile for calling later
dir C:\Users | select Name | Export-Csv -Path C:\users\$env:USERNAME\users.csv -NoTypeInformation
$list=Test-Path C:\users\$env:USERNAME\users.csv
""
#########################
"-------------------"
Write-Host -ForegroundColor Green "SECTION 2: Beginning Script..."
"-------------------"
if ($list) {
    "-------------------"
    #Clear Mozilla Firefox Cache
    Write-Host -ForegroundColor Green "SECTION 3: Clearing Mozilla Firefox Caches"
    "-------------------"
    Write-Host -ForegroundColor yellow "Clearing Mozilla caches"
    Write-Host -ForegroundColor cyan
    Import-CSV -Path C:\users\$env:USERNAME\users.csv -Header Name | foreach {
            Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\* -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*.* -Recurse -Force -EA SilentlyContinue -Verbose
        Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache2\entries\*.* -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\thumbnails\* -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cookies.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\webappsstore.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\chromeappsstore.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
            }
    Write-Host -ForegroundColor yellow "Clearing Mozilla caches"
    Write-Host -ForegroundColor yellow "Done..."
    ""
    "-------------------"
    # Clear Google Chrome
    Write-Host -ForegroundColor Green "SECTION 4: Clearing Google Chrome Caches"
    "-------------------"
    Write-Host -ForegroundColor yellow "Clearing Google caches"
    Write-Host -ForegroundColor cyan
    Import-CSV -Path C:\users\$env:USERNAME\users.csv -Header Name | foreach {
            Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cache2\entries\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cookies" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Media Cache" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal" -Recurse -Force -EA SilentlyContinue -Verbose
            # Comment out the following line to remove the Chrome Write Font Cache too.
            # Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\ChromeDWriteFontCache" -Recurse -Force -EA SilentlyContinue -Verbose
            }

    Write-Host -ForegroundColor yellow "Done..."
    ""
    "-------------------"
    # Clear Internet Explorer
    Write-Host -ForegroundColor Green "SECTION 5: Clearing Internet Explorer Caches"
     "-------------------"
    Write-Host -ForegroundColor yellow "Clearing Google caches"
    Write-Host -ForegroundColor cyan
    Import-CSV -Path C:\users\$env:USERNAME\users.csv | foreach {
            Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" -Recurse -Force -EA SilentlyContinue -Verbose
        Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\WER\*" -Recurse -Force -EA SilentlyContinue -Verbose
        Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
        Remove-Item -path "C:\Windows\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
        Remove-Item -path "C:\`$recycle.bin\" -Recurse -Force -EA SilentlyContinue -Verbose
            }

    Write-Host -ForegroundColor yellow "Done..."
    ""
    Write-Host -ForegroundColor Green "All Tasks Done!"
    } else {
    Write-Host -ForegroundColor Yellow "Session Cancelled"  
    Exit
    }

Tuesday, October 08, 2019

FreeBSD 12.0 Upgrade steps, because I'll forget.

This is pulled form https://www.freebsd.org/releases/12.0R/installation.html and the portmaster man page, an online version can be found here https://gsp.com/cgi-bin/man.cgi?topic=PORTMASTER

#Edited 10/09/19 with comments

#I'm not 100% sure why you do this first, but I guess its to make sure the system you're on is up to date before upgrading
freebsd-update fetch
freebsd-update install

#This part starts thea actual upgrade to 12.0 by fetching all the components needed for the upgraded
freebsd-update upgrade -r 12.0-RELEASE

#After fetching everything this does the actual install
freebsd-update install

#now we have to reboot into the newly installed system
shutdown -r now

#this finishes the first part of the upgrade
freebsd-update install

#now at this point you need to reinstall all the pkgs/ports to the new ABI
#I'm going to use portmaster to do this and this is more or less from their man page.
cd /root
portmaster --list-origins > ~/installed-port-list
#this is because we're going to install these manually
#screen is optional and only matters if you're going to use it
vi ~/installed-port-list #remove pkg, portmaster, and screen

#I don't know why I have to do this, but it made a bunch of errors go away so I'm adding it to the process
pkg bootstrap -f
portsnap fetch auto
portmaster -ty --clean-distfiles
portmaster -Faf
pkg delete -afy
rm -rf /usr/local/lib/compat/pkg

#the man page says to copy anything you need from /usr/local/etc so I just copied everything temporarily
mkdir /root/etc
cp -Rf /usr/local/etc/* /root/etc/

#I commented this out so you don't just copy and paste and run this like a moron
#Manually check /usr/local and /var/db/pkg to make sure that they are really empty
#cd /usr/local/
#ls
#rm -rf /usr/local/*
#cd /var/db/pkg
#ls
#rm -rf /var/db/pkg/*

#now we're going to reinstall pkg, portmaster and optionally, screen
cd /usr/ports/ports-mgmt/pkg
make install clean
cd /usr/ports/ports-mgmt/portmaster
make install clean
#now using screen is totally optional, but since I'm installing all the ports again I find it helpful in case I step away or fall asleep while they're compiling.
cd /usr/ports/sysutils/screen
make install clean

cd /root
screen

#so the -P says to use a pkg if one's available and you didn't need to customize the port install
#the -y answers yes to all prompts
#the --no-confirm doesn't ask the user to confirm the list of ports
#the -D doesn't clean the distfiles
#you can change these flags to your own needs just consult the man page
portmaster -P -y --no-confirm -D `cat ~/installed-port-list`

#in case you're not familiar with screen the main things you need to know are
#ctrl+a then d to leave session
#screen -r to rejoin session

#assuming your install went well you're supposed to do this one last time, again don't know why, but that's what they tell you to do
freebsd-update install

#now we're supposed to do one final reboot before using our system in production
shutdown -r now

#if you're using ZFS (and why wouldn't you be?) remember to do this
#you can probably do this earlier, but I forgot until I did a zpool status -v
zpool upgrade
Some of this can probably be changed, I don't know, but so far it has worked.

Wednesday, January 30, 2019

A few new one-liners

I like to automate and/or simplify* a lot of processes that I run on my stuff here are a few I am posting for the sake of having them somewhere

FreeBSD Updates

cd /usr/ports/ ; df -hl ; pkg update ; pkg clean -y ; portsclean -C -D -DD -L -P -PP ; df -hl ; portsnap fetch update ; pkg version -vIL= | awk '{print $1}' | sed 's/-[0-9].*//g' | awk '{printf "%s"" ",$0}' | sed 's/^/portmaster -P -y --no-confirm -D /g' | sh ; pkg version -vIL= ; pkg audit -F ; df -hl

Requires portmaster and portupgrade to be installed and prefers ports over pkg - what can I say I hate myself

CentOS Updates

clear; yum clean all; rm -rf /var/cache/yum; yum -y upgrade --skip-broken; package-cleanup --problems; package-cleanup --dupes; rpm -Va --nofiles --nodigest;
I think this requires yum-utils be installed to do the cleaning bits

Debian Updates (also obviously works on Ubuntu)

clear; apt-get update ; apt-get upgrade ; apt-get dist-upgrade; apt-get autoclean;  apt-get autoremove
OpenBSD Updates

I cheat and use openup, a currently free service provided by mtier - more information can be find by going to https://www.mtier.org/solutions/apps/openup/


* so yeah that FreeBSD one isn't "simple" but it does a bunch of stuff I need it to do.

Feel free to use these or whatever, I don't really care.

I don't have a clue

I'm so very tired. It's almost all the time now.