powershell 仅在控制台中显示的结果不会写入文件

u5rb5r59  于 2022-12-18  发布在  Shell
关注(0)|答案(1)|浏览(131)

我已经尝试了一切我可以找到网上张贴我的powershell脚本结果到一个文本文件没有运气。我只得到结果在控制台和没有文本文件创建。
参见以下代码

$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
}

控制台输出

PS C:\WINDOWS\system32> C:\temp\JWS\TEST-1.ps1
\\MyServer\JWS_SUL\UNITED\Image\Ticket\Photo\Pit2

PS C:\WINDOWS\system32>

您的代码已使用我的网络共享更新

# --- 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控制台只是显示了这个

PS Microsoft.PowerShell.Core\FileSystem::\\server\JWS_SUL> C:\temp\JWS\TEST-123.ps1
nzkunb0c

nzkunb0c1#

这是一个带有注解的代码清理版本,它使用了一些mkelements注解,我在编写和测试代码时没有看到这些注解。

# --- 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

示例文件输出:

G:\BEKDocs\Outlook Files
G:\BEKDocs\Access

Money目录今天被更改了。注意到ShortDateString的转换以消除比较中的时间元素。

相关问题