否则不在foreach中执行

else not being executed within foreach

我正在尝试编写以下脚本,它检查一个文件夹中的一组文件,如果它们存在,将它们移动到 "archive" 文件夹。如果他们不这样做,请将错误消息写入屏幕和日志文件。

文件移动正常,因此 IF 的第一部分工作正常,但如果没有要移动的文件,else 应该启动并输出错误....但事实并非如此。

变量。ps1:

#----- define parameters -----#
#----- Treat All Errors as Terminating -----#
$ErrorActionPreference = "Stop" 
#----- Set count to 0 -----#
$count = 0
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "0"
#----- define folder where files are located ----#
$SourceFolder = "C:\HG1\Test\Files"
#----- define folder where files are to be moved to ----#
$DestFolder = "C:\HG1\Test\Files\Archive"
#----- define folder where files are to be moved to ----#
$LogPath = "C:\HG1\archive.log"
#----- define extension ----#
$Extension = "*.log"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $SourceFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

archive_files.ps1

#----- Call variables file variables.ps1 - MUST BE IN SAME LOCATION AS SCRIPT ----#
. ./variables.ps1

foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        move-item -path $File.FullName -destination $DestFolder
        Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: Archived File $File")
        }
    else
        {
        write-host "ERROR: No files to archive" -ForegroundColor "Red"
        Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t ERROR: No files to archive")
        }
    } 
Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: ***Archiving script completed successfully***")

如有任何帮助,我们将不胜感激。

您应该使用 Test-Path cmdlet 检查文件是否存在:

foreach ($File in $Files)
    {
    if (Test-Path $File)
        {
        move-item -path $File.FullName -destination $DestFolder
        Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: Archived File $File")
        }
    else
        {
        write-host "ERROR: No files to archive" -ForegroundColor "Red"
        Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t ERROR: No files to archive")
        }
    } 
Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: ***Archiving script completed successfully***")

你的逻辑倒过来了。

$Files 中的每个 $File 将始终是 某物 ,但 $Files 集合本身可能为空:

if(-not $Files)
{
    foreach($File in $Files)
    {
        Move-Item -Path $File.FullName -Destination $DestFolder
        Add-Content $LogPath -Value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: Archived File $File")
    }
}
else
{
    Write-Host "ERROR: No files to archive" -ForegroundColor "Red"
    Add-Content $LogPath -Value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t ERROR: No files to archive")
}

Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: ***Archiving script completed successfully***")
#----- Call variables file variables.ps1 - MUST BE IN SAME LOCATION AS SCRIPT ----#
. ./variables.ps1


if ($File)
    {
     foreach ($File in $Files)
     {
        move-item -path $File.FullName -destination $DestFolder
        Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: Archived File $File")
     }

    }
else
    {
    write-host "ERROR: No files to archive" -ForegroundColor "Red"
    Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t ERROR: No files to archive")
    }

Add-Content $LogPath -value ("$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) `t INFO: ***Archive script completed successfully***")

这里,

  • Foreach each 不会处理 $null 值 因此,如果值不为 null
  • ,则包括 foreach
  • 如果不会输入 $null 值,则不需要“-ne $null”

希望这对你有用,

此致,

Kvprasoon