You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本输出至文本文件/邮件问题求助

解决PowerShell文件更新检测结果输出到文件/邮件并每日调度的问题

先修复你代码里的语法错误

你尝试的代码存在明显语法问题:((Get-Date) - (Get-Item "P:\cognosdata\cubes\CheckAnalysis.mdc").LastWriteTime)).Day 多了一个闭合括号,直接导致脚本执行失败。修正后的单文件检测代码如下:

$filePath = "$env:USERPROFILE\Documents\cubereport.txt"

# 检查CheckAnalysis.mdc是否今日更新
if ((Get-Date) - (Get-Item "P:\cognosdata\cubes\CheckAnalysis.mdc").LastWriteTime).Day -lt 1 {
    Write-Output "Check Analysis updated today" | Out-File -FilePath $filePath -Append -Encoding utf8
}

注:加上-Encoding utf8可避免中文乱码,不需要的话可以去掉。

完整批量检测并输出到文件的脚本

为避免重复代码,把所有待检测文件整理成数组循环处理,同时支持清空旧报告(不需要保留历史可删除Clear-Content行):

$reportPath = "$env:USERPROFILE\Documents\cubereport.txt"
$checkItems = @(
    @{Path = "P:\cognosdata\cubes\CheckAnalysis.mdc"; Name = "Check Analysis"},
    @{Path = "P:\cognosdata\cubes\coupon tracking.mdc"; Name = "Coupon Tracking"},
    @{Path = "P:\cognosdata\cubes\rest summary menu mix.mdc"; Name = "Rest Summary Menu Mix"},
    @{Path = "P:\cognosdata\cubes\porchmenumix.mdc"; Name = "Porch menu mix"},
    @{Path = "P:\cognosdata\cubes\CashDaypart.mdc"; Name = "Cash Daypart"}
)

# 清空旧报告(可选,保留历史则删除此行)
Clear-Content -Path $reportPath -ErrorAction SilentlyContinue

# 添加报告头部
Write-Output "=== 每日Cube更新检测报告 $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') ===" | Out-File -Path $reportPath -Append -Encoding utf8

foreach ($item in $checkItems) {
    try {
        $file = Get-Item -Path $item.Path -ErrorAction Stop
        if ((Get-Date) - $file.LastWriteTime).Day -lt 1 {
            Write-Output "$($item.Name) updated today" | Out-File -Path $reportPath -Append -Encoding utf8
        } else {
            Write-Output "$($item.Name) NOT updated today" | Out-File -Path $reportPath -Append -Encoding utf8
        }
    } catch {
        Write-Output "$($item.Name) - 文件不存在或无法访问" | Out-File -Path $reportPath -Append -Encoding utf8
    }
}

将检测结果直接发送为邮件

如果不需要生成本地文件,可把结果收集到变量后直接发邮件:

$smtpServer = "你的SMTP服务器地址" # 示例:smtp.office365.com
$fromAddress = "发件人邮箱@domain.com"
$toAddresses = @("收件人1@domain.com", "收件人2@domain.com")
$emailSubject = "每日Cube更新检测报告 $(Get-Date -Format 'yyyy-MM-dd')"

$checkItems = @(
    @{Path = "P:\cognosdata\cubes\CheckAnalysis.mdc"; Name = "Check Analysis"},
    @{Path = "P:\cognosdata\cubes\coupon tracking.mdc"; Name = "Coupon Tracking"},
    @{Path = "P:\cognosdata\cubes\rest summary menu mix.mdc"; Name = "Rest Summary Menu Mix"},
    @{Path = "P:\cognosdata\cubes\porchmenumix.mdc"; Name = "Porch menu mix"},
    @{Path = "P:\cognosdata\cubes\CashDaypart.mdc"; Name = "Cash Daypart"}
)

# 构建邮件正文
$emailBody = "=== 每日Cube更新检测报告 $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') ===`n`n"
foreach ($item in $checkItems) {
    try {
        $file = Get-Item -Path $item.Path -ErrorAction Stop
        if ((Get-Date) - $file.LastWriteTime).Day -lt 1 {
            $emailBody += "$($item.Name) updated today`n"
        } else {
            $emailBody += "$($item.Name) NOT updated today`n"
        }
    } catch {
        $emailBody += "$($item.Name) - 文件不存在或无法访问`n"
    }
}

# 发送邮件(需验证的SMTP服务器添加 -Credential (Get-Credential) 参数)
Send-MailMessage -SmtpServer $smtpServer -From $fromAddress -To $toAddresses -Subject $emailSubject -Body $emailBody -Encoding utf8

配置每日自动运行(Windows任务计划程序)

  1. 把脚本保存为.ps1文件,比如CubeUpdateCheck.ps1
  2. 打开任务计划程序,点击「创建任务」
  3. 「常规」标签:填写任务名称,勾选「不管用户是否登录都要运行」(后台执行需选此项)
  4. 「触发器」标签:新建触发器,选择「每日」,设置运行时间
  5. 「操作」标签:新建操作,选择「启动程序」,程序/脚本填powershell.exe,添加参数填-ExecutionPolicy Bypass -File "你的脚本完整路径"(示例:-ExecutionPolicy Bypass -File "C:\Scripts\CubeUpdateCheck.ps1")
  6. 「设置」标签:按需勾选「任务运行超时停止」等选项,点击确定完成配置

内容的提问来源于stack exchange,提问作者Jr Weischedel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 17:28:22