How to output the results to a log file using Powershell?

Hey Experts!  Here is a script that I would like to modify to include the ability to write the output to a log file.

$in = $false
$pattern = (get-date (get-date).AddDays(-1) -f 'MM\/dd\/yyyy') + ' * --- SCHEDULEREC STATUS BEGIN*'
$(
  switch -wildcard -file C:\tools\logs\dsmsched.log
  {
    $pattern   { $in = $true }
    '*SCHEDULEREC STATUS END*'  { $in = $false }
    default { $_ | ? { $in } }
  }
)

Open in new window


Thanks Experts!
LVL 9
samiam41Asked:
Who is Participating?
 
oBdACommented:
Since you already have a subexpression around it, you can just pipe the output to Set-Content:
$in = $false
$pattern = (get-date (get-date).AddDays(-1) -f 'MM\/dd\/yyyy') + ' * --- SCHEDULEREC STATUS BEGIN*'
$(
	Switch -Wildcard -File C:\tools\logs\dsmsched.log {
		$pattern					{$in = $true}
		'*SCHEDULEREC STATUS END*'	{$in = $false}
		default						{If ($in) {$_}}
	}
) | Set-Content -Path C:\Temp\Whatever.log

Open in new window

1
 
samiam41Author Commented:
love working with you.  thanks oBdA
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.