Posts

Showing posts with the label PowerShell

Compress Old Files with PowerShell

 http://www.thesysadminhimself.com/2014/01/compress-old-files-with-powershell.html The following  PowerShell  script will compress files that are older than the specified amount of time. It is handy for archiving  IIS Logs, SQL Backups , etc. The script uses  7-zip  so you obviously have to have it installed (or the exe copied somewhere). It's using maximum compression which is resource intensive, if you don't want that just remove the " -mx9 -m0=lzma2 " parameters. The Script 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $path = "C:\inetpub\logs\LogFiles\" $mask = "*.log"   $days = 7   $files = dir $path -Recurse -Include $mask | where {($_.LastWriteTime -lt ( Get-Date ).AddDays(- $days ).AddHours(- $hours ).AddMinutes(- $mins )) -and ($_.psIsContainer -eq $false )}     ForEach ( $file in $files ) {        & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 ( $file .FullName + ".7z" ) $file .Ful...

Powershell Scripting : Windows Firewall Block by Country

 Powershell Scripting : Windows Firewall Block by Country https://www.itlumberjack.com/microsoft/21-windows-firewall-block-by-country This script is designed to place an extra layer of security on Windows machines. Servers running IIS, Apache, or Nginx are extremely vulnerable since they require ports 80 and 443 to be open to the internet. The script pulls a list of IP addresses from  www.ipdeny.com  . IP Deny provides a list of IP ranges for each country.  The script can be run as a task. This will help in making sure that the IP addresses being blocked are current. Before scheduling a task please be sure to review IP Deny's usage policy.  https://www.ipdeny.com/usagelimits.php Please note that this should not be your first line of defense in blocking unwanted inbound traffic. Best practice would be to block the traffic on an edge device such as a hardware firewall. In the event, the attacker is able to get past the hardware firewall, this would serve as a...

Setting the PowerShell Execution Policy

Image
  Solution There are situations when you are executing a PowerShell script and it will give you the below error message: File C:\scripts\regSvr.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get- help about_signing" for more details. At line:1 char:19 + c:\scripts\regSvr.ps1 <<<< The reason for the above error is a security setting built into Windows PowerShell called  "execution policy".  Execution Policy determines how (or if) PowerShell runs scripts. By default, PowerShell's  execution policy  is set to  Restricted ; this means that scripts will not run. You can verify the execution policy setting by using the  Get-ExecutionPolicy  PowerShell command as shown below. get-executionpolicy You can change the PowerShell script execution behavior using  "Set-ExecutionPolicy" . The  Set-ExecutionPolicy  cmdlet enables you to determine which Windows PowerShell scripts will be all...

POWERSHELL: DELETE FILES OLDER THAN X DAYS EXCLUDE SOME FILES

https://www.thomasmaurer.ch/2010/12/powershell-delete-files-older-than/ POWERSHELL: DELETE FILES OLDER THAN X DAYS This example will use PowerShell to delete files older than 90 days. -Exclude a.jpg, a.png # Delete all Files in C:\temp older than 90 day(s) $Path = "C:\temp" $Daysback = "-90"   $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $Path  -Exclude a.jpg, a.png | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item If you need to delete files in subfolders too, you can use this script. This is the same script with the Get-Childitem parameter “-Recurse”. # Delete all Files and sub folder files in C:\temp older than 90 day(s) $Path = "C:\temp" $Daysback = "-90"   $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $Path -Recurse   -Exclude a.jpg, a.png | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

Move older files with PowerShell

Move older files with PowerShell set the source path set the destination path set the days 30days old = -30 set exclude files save it as move.ps1 =========================================== $YourPath = "source" $YourDest = "destination" $daysback = "-30” Get-ChildItem -Path $YourPath -Exclude abc.txt, abc.png, abc.jpg, xyz_voucher_bottom.jpg, bbc.jpg | where {$_.LastWriteTime -lt ((Get-Date).AddDays($daysback ))} | move-item -Destination $YourDest