Powershell : Transfer yesterday‘s EOD file to SMB folder

bash 复制代码
# *****************************************************************************************************************

# PowerShell Script - Code by Moshow@https://zhengkai.blog.csdn.net/

#        -> transfer yesterday's EOD file to destination SMB folder

# *****************************************************************************************************************

 

# 1. Got yyMMdd format of yesterday's date

$yesterday = (Get-Date).AddDays(-1).ToString('yyyyMMdd')

$pattern = "YOUR_EOD_$yesterday*.TXT"

$timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')

 

# 2. Search for the file in the source folder

$sourceFolder = "E:\DataShare\EOD_IMPORT"

$file = Get-ChildItem -Path $sourceFolder -Filter $pattern | Select-Object -First 1

 

# 3. Check if the file exists and is not empty

if ($file) {

    # 4. Transfer the file to the destination SMB folder

    $destination = "\\server111\DataShare\EOD_IMPORT\$($file.Name)"

 

    Start-BitsTransfer -Source $file.FullName -Destination $destination

    $logMessage = "[$timestamp] Transfer: $($file.FullName) -> $destination"

   

} else {

    $logMessage =  "[$timestamp] Not Found: $pattern"

}

# 5. Log the transfer result

$logFile = "E:\EOD_transfer_log.txt"

Write-Host $logMessage

Add-Content -Path $logFile -Value $logMessage

代码解读

  1. 注释部分:

    bash 复制代码
    https://zhengkai.blog.csdn.net/

    这一部分是注释,说明了脚本的作者和脚本的功能。

  2. 获取昨天的日期:

    复制代码
    $yesterday = (Get-Date).AddDays(-1).ToString('yyMMdd')

    这行代码获取昨天的日期,并将其格式化为 yyMMdd 的形式。例如,如果昨天是 2023 年 5 月 25 日,$yesterday 将会是 230525。

  3. 构建文件名模式:

    复制代码
    $pattern = "LMP_CUSTACCT_FILE_D$yesterday*.TXT"

    使用昨天的日期构建一个文件名模式,表示要查找以 LMP_CUSTACCT_FILE_D 开头,后跟昨天的日期,并以 .TXT 结尾的文件。

  4. 获取当前时间戳:

    复制代码
    $timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')

    获取当前时间的时间戳,格式为 yyyy-MM-dd HH:mm:ss,用于记录日志。

  5. 搜索源文件夹中的文件:

    复制代码
    $sourceFolder = "E:\DataShare\LMP_PROD_EXTERNAL\EoD_Import"
    $file = Get-ChildItem -Path $sourceFolder -Filter $pattern | Select-Object -First 1

    在指定的源文件夹中搜索符合模式的文件。如果找到了多个文件,只选择第一个。

  6. 检查文件是否存在且不为空:

    复制代码
    if ($file) {

    如果找到了文件,则进入条件语句。

  7. 转移文件到目标 SMB 文件夹:

    复制代码
    $destination = "\\server111\DataShare\LMP_PROD_EXTERNAL\EOD_IMPORT\$($file.Name)"
    Start-BitsTransfer -Source $file.FullName -Destination $destination
    $logMessage = "[$timestamp] Transfer: $($file.FullName) -> $destination"
    • 构建目标文件夹路径。
    • 使用 Start-BitsTransfer 命令将文件从源路径转移到目标路径。
    • 记录转移的日志信息。
  8. 处理未找到文件的情况:

    复制代码
    } else {
        $logMessage =  "[$timestamp] Not Found: $pattern"
    }

    如果没有找到文件,则记录未找到的日志信息。

  9. 记录转移结果到日志文件:

    复制代码
    $logFile = "E:\EOD_transfer_log.txt"
    Write-Host $logMessage
    Add-Content -Path $logFile -Value $logMessage
    • 指定日志文件的路径。
    • 使用 Write-Host 输出日志信息到控制台。
    • 使用 Add-Content 将日志信息追加到日志文件中。
相关推荐
JoePotter1 天前
Restic 备份工具使用指南(Windows Server 2019 适配版)
powershell·数据备份·windows server·运维自动化·restic
DogDaoDao4 天前
Windows 双 Shell 全面对比:CMD 与 PowerShell 的技术剖析
windows·shell·测试·powershell·cmd·程序员开发
Vex2une6 天前
PowerShell 7.5新特性解析:跨平台自动化与AI集成实战指南
ai编程·powershell·安全合规·自动化脚本·.net 9·跨平台运维·ssh远程
Vex2une13 天前
PowerShell 发展史:从命令行外壳到跨平台自动化引擎
windows·自动化·azure·powershell·vs code·脚本语言·dsc
Vex2une15 天前
PowerShell ISE 到 VS Code 迁移史:从集成脚本环境到现代编辑器工作流
windows·vscode·powershell·powershell ise·powershell 扩展·powershell 7·脚本编辑器
Vex2une19 天前
Windows PowerShell 发展史:从命令行工具到自动化核心
windows·自动化运维·命令行·powershell·cmd·脚本语言·对象管道
iCxhust21 天前
Powershell脚本,列出近3日打开的文件夹并打开
程序人生·powershell
谷哥的小弟23 天前
基于PowerShell的Chrome无痕访问
chrome·powershell·网络环境·无痕访问
Blockbuater_drug1 个月前
setx 设完读不到?Windows 系统环境变量作用域、截断坑与 AI 工具密钥管理
注册表·powershell·api key·mcp server·windows 环境变量·setx·环境变量插值
bosins2 个月前
Windows IIS 配置本地HTTPS 全流程指南:自签名证书 + 端口绑定 + 避坑详解
powershell·openssl