Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

Save log file with the name of yesterday's date - Powershell

Experts, here is a script that outputs the info to a log file but I need the filename to include yesterday's date.  I've tried a few things in the attempt to learn on my own but haven't had much luck.  Looking for some help.

$ServerList = @'
	"Name",			"Path"
	servers
'@ | ConvertFrom-Csv

$Pattern = (Get-Date).AddDays(-1).ToString('MM\/dd\/yyyy') + ' * --- SCHEDULEREC STATUS BEGIN*'
$in = $false
$ServerList | ForEach-Object {
	$Server = $_
	"========== $($Server.Name) ============================================================"
	Try {
		Switch -Wildcard (Get-Content -Path "\\$($Server.Name)\$($Server.Path.Replace(':', '$'))" -ErrorAction Stop) {
			$Pattern					{$in = $true}
			'*SCHEDULEREC STATUS END*'	{$in = $false}
			default						{If ($in) {$_}}
		}
	} Catch {
		"ERROR accessing '$($Server.Path)': $($_.Exception.Message)"
	}
} | Set-Content -Path \\w2k12FS01\PUBLIC\Tivoli_Master_Logs\Combined[yesterdaysdate].log

Open in new window


format MMddYYYY

Thanks Experts!
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of samiam41

ASKER

Script works and the file name saves as designed!  Great job.

@Dustin, thank you for the links.  It should give me some more insight into PS.