I have tried everything I can find online for posting my powershell script result to a text file with no luck. I only get results in the console and no text file is created.
See code below
$rootSitePath = "\\MyServer\JWS_SUL"
$paths = ($rootSitePath "\" "UNITED\Image\Ticket\Loadout"),
($rootSitePath "\" "UNITED\Image\Ticket\Pit2"),
($rootSitePath "\" "UNITED\Image\Ticket\Photo\Loadout")
($rootSitePath "\" "UNITED\Image\Ticket\Photo\Pit2")
$folder = $paths
foreach ($folder in $paths){
}
if ($_.LastWriteTime.Date -ne (Get-Date).ToDay) {
Write-Output $folder | Out-File -Path c:\temp\Test\BackupResults.txt
}
Console Output
PS C:\WINDOWS\system32> C:\temp\JWS\TEST-1.ps1
\\MyServer\JWS_SUL\UNITED\Image\Ticket\Photo\Pit2
PS C:\WINDOWS\system32>
Your code updated with my network share
# --- Setup ---
$rootSitePath = "\\server\JWS_SUL"
$OutputPath = "C:\temp\BackupResults.txt"
$CurDate = (Get-Date).ToShortDateString()
#--- Cleanup from previous runs ---
If (Test-Path -Path "$OutputPath" ) {
Remove-Item -Path "$OutputPath"
}
#--- Initialize paths - always use Join-Path ---
$paths = @((Join-Path -Path "$rootSitePath" -Childpath "\UNITED\Image\Ticket\Pit2" )
(Join-Path -Path "$rootSitePath" -Childpath "\UNITED\Image\Ticket\Loadout")
(Join-Path -Path "$rootSitePath" -Childpath "\UNITED\Image\Ticket\Photo\Loadout"))
foreach ($folder in $paths){
#--- Retrieve Folder time as ShortDate
$FInfo = (Get-item -Path "$Folder").LastWriteTime.Date.ToShortDateString()
if ($FInfo -ne $CurDate ) {
Write-Output $folder |
Out-File -FilePath "C:\temp\BackupResults.txt" -Append
}
} #End Foreach
Powershell console just shows this
PS Microsoft.PowerShell.Core\FileSystem::\\server\JWS_SUL> C:\temp\JWS\TEST-123.ps1
CodePudding user response:
Here's a cleaned up version of your code with comments. It uses several of mkelements comments which I didn't see as I was writing and testing the code.
# --- Setup ---
$rootSitePath = "G:\BEKDocs"
$OutputPath = "G:\Test\BackupResults.txt"
$CurDate = (Get-Date).ToShortDateString()
#--- Cleanup from previous runs ---
If (Test-Path -Path "$OutputPath" ) {
Remove-Item -Path "$OutputPath"
}
#--- Initialize paths - always use Join-Path ---
$paths = @((Join-Path -Path "$rootSitePath" -Childpath "Money" )
(Join-Path -Path "$rootSitePath" -Childpath "Outlook Files")
(Join-Path -Path "$rootSitePath" -Childpath "Access"))
foreach ($folder in $paths){
#--- Retrieve Folder time as ShortDate
$FInfo = (Get-item -Path "$Folder").LastWriteTime.Date.ToShortDateString()
if ($FInfo -ne $CurDate ) {
Write-Output $folder |
Out-File -FilePath "G:\Test\BackupResults.txt" -Append
}
} #End Foreach
Sample File Output:
G:\BEKDocs\Outlook Files
G:\BEKDocs\Access
The Money directory was changed today. Note the conversion to ShortDateString to eliminate the time element in the compare.