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!
LVL 9
samiam41Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
I've set the time stamp to yyyyMMdd; that way, an alphabetic sort will automatically sort them by date, too.
If you insist on MMddYYYY, just change the $Timestamp definition in line 12.
$ServerList = @'
	"Name",			"Path"
	"MGT01",		"E:\Program Files\Tivoli\TSM\baclient\dsmsched.log"
	"w2k8sentry01",	"C:\Program Files\Tivoli\TSM\baclient\dsmsched.log"
	"w2k12share01",	"C:\Program Files\Tivoli\TSM\baclient\dsmsched.log"
'@ | ConvertFrom-Csv
## Put a "{0}" at the position in the string where the time stamp should be added:
$LogFile = "\\w2k12FS01\PUBLIC\Tivoli_Master_Logs\Combined_{0}.log"

$Yesterday = (Get-Date).AddDays(-1)
$Pattern = $Yesterday.ToString('MM\/dd\/yyyy') + ' * --- SCHEDULEREC STATUS BEGIN*'
$Timestamp = $Yesterday.ToString('yyyyMMdd')
$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 $($LogFile -f $TimeStamp)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dustin SaundersDirector of OperationsCommented:
No points please but:

For the record, pretty much all the documentation you need is on MSDN.  You can learn more about Get-Date here:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-5.1

You can see that in the description it says:
The Get-Date cmdlet gets a DateTime object

So you can look up the DateTime object on MSDN and see how to interact with it (see Methods):
https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx#Methods

And you can see how to AddDays (click on that link from the method section).  Scrolling down to the Examples:
https://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx#Examples

You can now see how to use the method.
samiam41Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.