【问题标题】:PowerShell transfer script to exclude some files on holidaysPowerShell 传输脚本以在假期排除某些文件
【发布时间】:2021-03-14 09:49:21
【问题描述】:

我有一个 powershell 脚本,它使用 Copy-Item cmdlet 将多个文件从一台服务器传输到另一台服务器。我有一个预定的工作,每天周一到周五运行一次。我无法终止任务,因为某些文件仍需要传输。但是,我想排除脚本中其他文件的美国假期。如何在美国假期排除以下文件传输,同时允许传输文件 KEEP?

   # 

   $date = get-date -format yyyy_MM_dd
   #
   #Transfer file to SFTP server
   Copy-Item "D:\ftproot\xxx\Archive\file_$($date)_17_00_00.csv" -destination "D:\ftproot\Files\" -force
   Copy-Item "D:\ftproot\xxx\Archive\KEEP_$($date)_17_00_00.csv" -destination "D:\ftproot\Files\" - 
   force

退出

【问题讨论】:

  • 嗯,首先您必须获取或定义美国假期日期列表。
  • 从文件中排除以下假期的示例
  • 假期 2020 元旦 2020/01/01 马丁路德金日 2020/01/20 华盛顿诞辰 2020/02/17 耶稣受难日 2020/04/10 阵亡将士纪念日 2020/05/ 25 独立日 2020/07/04 劳动节 2020/09/07
  • 好的,您知道要排除哪些日期 - 您是否需要帮助确定如何测试今天的日期是其中一个日期,还是......其他日期?
  • 我不确定如何在 12 月 25 日将我想要排除的日期添加到 D:\ftproot\xxx\Archive\file_$($date)_17_00_00.csv 示例中

标签: powershell scheduled-tasks file-transfer


【解决方案1】:

假设您有一个描述即将到来的假期日期的字符串列表,其格式与文件相同,如果测试 今天的日期是否包含在该列表中,您需要做的就是:

$Holidays = @(
  "2020_01_01"  # New Years Day
  "2020_01_20"  # Martin Luther King, Jr. Day
  "2020_02_17"  # Washington's Birthday
  "2020_04_10"  # Good Friday
  "2020_05_25"  # Memorial Day
  "2020_07_04"  # Independence Day
  "2020_09_07"  # Labor Day
)

$date = get-date -format yyyy_MM_dd

if($Holidays -notcontains $date){
  # Not a holiday, let's copy the file
  Copy-Item "D:\ftproot\xxx\Archive\file_$($date)_17_00_00.csv" -destination "D:\ftproot\Files\" -force
}

# This one gets copied regardless
Copy-Item "D:\ftproot\xxx\Archive\KEEP_$($date)_17_00_00.csv" -destination "D:\ftproot\Files\" -force

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多