否则不会在foreach中执行

时间:2016-05-18 13:06:41

标签: powershell powershell-v4.0

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

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

variables.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***")

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

您应该使用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***")

答案 1 :(得分:0)

你在那里得到了那种逻辑。

$File中的每个$Files始终是某些,但$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***")

答案 2 :(得分:0)

#----- 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每个都不会处理$ null值 所以如果value不为null则包括foreach
  • 如果不输入$ null值,那么“-ne $ null”不是必需的

希望这适合你,

此致

Kvprasoon