FileMaintenance Quick Tutorials - 7k2mpa/FileMaintenace GitHub Wiki

Quick Tutorials

FileMaintenance.ps1

Preparation

Place FileMaintenance.ps1 and CommonFunctions.ps1 in a same folder that you want.

Execute 'kickme_first.bat' with administrator privilege, and new event source 'Infra' is registered in Windows EventLog.

PowerShell 5.0 or later is required. If you run the scripts on the Windows Server 2008 , 2008R2 , 2012 ,2012R2 , install WMF(Windows Management Framework)5.0 or later.

https://docs.microsoft.com/ja-jp/powershell/scripting/install/installing-windows-powershell?view=powershell-7#upgrading-existing-windows-powershell

Quick Start

At first execute and select a dummy folder for tutorials, you will get a list of files in the folder.

PS D:\FileMaintenace> .\FileMaintenance.ps1 -TargetFolder D:\TEST -verbose

Learn how to use -Action option.

At first you should specify -WhatIf option. With -WhatIf option you can test option's usage safely.
With -WhatIf option all actions such as Delete or Copy etc, will be 'WhatIf' in the log.

PS D:\FileMaintenace> .\FileMaintenance.ps1 -TargetFolder D:\TEST -Action Copy -MoveToFolder .\OldLogs  -WhatIf  
PS D:\FileMaintenace> .\FileMaintenance.ps1 -TargetFolder D:\TEST -Action Delete -WhatIf

If you confirm all actions , remove -WhatIf option.

PS D:\FileMaintenace> .\FileMaintenance.ps1 -TargetFolder D:\TEST -Action Delete

Next, learn how to find files in the folders. -Days and -RegularExpression options are useful criteria.

-Days option find files which match number of days elapsed.
-RegularExpression option select files which match Regular Expression. In the sample it means "files ends with .txt extension"

PS D:\FileMaintenace> .\FileMaintenance.ps1 -TargetFolder D:\TEST -Action Delete -Days 3 -RegularExpression '\.txt$'

Try complex action with two action options -PreAction and -Action.
This sample has 2 actions.

-PreAction option manages file to compress or archive or add time stamp to filename. With -PreAction option , 'alert.log' is compressed and added time stamp. New File like 'alert_YYYYMMDD_HHMMSS.log.zip' is created and placed in -MoveToFolder 'D:\OLD'
Only -PreAction option accepts multiple arguments , such as 'Compress,AddTimeStamp' , 'AddTimeStamp,MoveNewFile'

-Action option makes original 'alert.log' to clear null.

If middleware output log in same filename continuously , use this combined actions.

PS D:\FileMaintenace> .\FileMaintenance.ps1 -TargetFolder D:\TEST -PreAction AddTimeStamp,Compress,MoveNewFile -MoveToFolder D:\OLD -Action NullClear -RegularExpression '^alert\.log$'

FileMaintenance.ps1 has 3 action sections (-PreAction , -Action , -PostAction) And you can make complex file management.

Differential Copy

With -Verbose and -Confirm options, you can make differential copy safely.

If a file in destination path(-MoveToFolder) exists already, this script prompts to override or not.

With -Verbose option you can get Last Write Time and Size information of source and destination file, with the information you decide to override in the destination or not.

PS D:\FileMaintenace> .\FileMaintenance -TargetFolder .\TEST1 -Action Copy -MoveToFolder .\TEST2 -OverRideForce -Verbose -Confirm

Information   EventID      2  --- Start processing [File] D:\FileMaintenace\TEST1\2.txt ---
Information   EventID      1  destination folder of the file [D:\FileMaintenace\TESt2\] exists.
Information   EventID      1  Check existence of D:\FileMaintenace\TESt2\2.txt
Warning       EventID     10  Same name file [D:\FileMaintenace\TESt2\2.txt] exists already.
VERBOSE: Destination LastWriteTime[04/26/2020 10:50:41] Size[0]
VERBOSE: Source      LastWriteTime[04/26/2020 10:50:41] Size[0]
Warning       EventID     10  A same name file exists in the destination already, but specified -OverRide[True] option, thus overrides the file in the destination [D:\FileMaintenace\TESt2\2.txt]
確認
この操作を実行しますか?
対象 "項目: D:\FileMaintenace\TEST1\2.txt コピー先: D:\FileMaintenace\TESt2\2.txt" に対して操作 "ファイルのコピー" を実行しています。
[Y] はい(Y) [A] すべて続行(A) [N] いいえ(N) [L] すべて無視(L) [S] 中断(S) [?] Help (default is "はい(Y)"): y
Success       EventID     73  Successfully completed to [Copy] [D:\FileMaintenace\TEST1\2.txt] to [D:\FileMaintenace\TESt2\2.txt]

Wrapper.ps1

If you make FileMaintenance.ps1 to process multiple folders, use Wrapper.ps1.

At first, you make a command file 'command.txt' with an editor software. Write commands in the command file, and save it. Execute Wrapper.ps1 with specification command path and command file, you can manage multiple folders.


command.txt
-TargetFolder D:\TEST -Action Delete -Days 3 -RegularExpression '\.txt$'
-TargetFolder D:\TEST -PreAction AddTimeStamp,Compress,MoveNewFile -MoveToFolder D:\OLD -Action NullClear -RegularExpression '^alert\.log$'

PS D:\Wrapper.ps1 -CommandPath .\FileMaintenace.ps1 -CommandFile .\command.txt