我们需要检查一个要求,确保文件夹中的 .csv 文件中的多个文件名称以数字开头。如果是,则将继续执行后续步骤。如果任何文件丢失,则向企业发送电子邮件,并附上丢失的文件名。这可以通过批处理脚本或 PowerShell 实现吗?
提前致谢!
答案1
这是脚本,不过请先在重复文件夹中测试一下。由于我没有文件(名称),所以未经测试
# Define the folder path
$folderPath = "C:\Where\All\CSVs\Are"
# Get a list of all CSV files in the folder
$csvFiles = Get-ChildItem -Path $folderPath -Filter "*.csv"
# Initialize variables for tracking sequence and missing files
$expectedNumber = 1
$missingFiles = @()
# Loop through each CSV file
foreach ($csvFile in $csvFiles) {
# Get the base name of the file without extension
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($csvFile.Name)
# Check if the base name starts with the expected number
if ($baseName -match "^$expectedNumber") {
$expectedNumber++
} else {
$missingFiles += $csvFile.Name
}
}
if ($missingFiles.Count -gt 0) {
Write-Host "Missing Files:"
foreach ($missingFile in $missingFiles) {
Write-Host $missingFile
}
# Convert the list of missing files to a string
$missingFilesString = $missingFiles -join "`r`n"
# Email configuration
$emailParams = @{
SmtpServer = "smtp.gmail.com"
Port = 587
UseSsl = $true
Credential = Get-Credential
From = "[email protected]"
To = "[email protected]"
Subject = "Missing CSV Files"
Body = "The following CSV files are missing or out of sequence:`r`n`r`n$missingFilesString"
}
# Send the email
Send-MailMessage @emailParams
} else {
Write-Host "All CSV files have names starting with numbers in sequence."
}
此脚本将要求您提供凭据。我建议为此脚本创建一个应用密码,并使用它来代替凭据
编辑:看来楼主想将凭证存储在外部加密文件中。我建议创建一个应用程序密码专门针对此脚本,并使用它来代替您的谷歌帐户密码。
首先,您需要使用此 PS 脚本创建该 XML 文件:
$credential = Get-Credential
$credential | Export-CliXml -Path "C:\Where\To \Save\The\Credentials\myCred_${env:USERNAME}_${env:COMPUTERNAME}.xml"
注意:您无需编辑用户名和计算机名,它们是自动设置的。您需要记住,此加密 XML 文件特定于您的用户和你的机器两者皆可。这意味着如果您尝试在另一台计算机上或以不同的用户身份加载 XML,它将不是工作。如果您希望能够在不同的机器上运行该脚本,您别无选择,只能以纯文本形式存储密码。
这是该用例的主要脚本
# Define the folder path
$folderPath = "C:\Where\All\CSVs\Are"
# Get a list of all CSV files in the folder
$csvFiles = Get-ChildItem -Path $folderPath -Filter "*.csv"
# Initialize variables for tracking sequence and missing files
$expectedNumber = 1
$missingFiles = @()
# Loop through each CSV file
foreach ($csvFile in $csvFiles) {
# Get the base name of the file without extension
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($csvFile.Name)
# Check if the base name starts with the expected number
if ($baseName -match "^$expectedNumber") {
$expectedNumber++
} else {
$missingFiles += $csvFile.Name
}
}
if ($missingFiles.Count -gt 0) {
Write-Host "Missing Files:"
foreach ($missingFile in $missingFiles) {
Write-Host $missingFile
}
# Convert the list of missing files to a string
$missingFilesString = $missingFiles -join "`r`n"
$password = ConvertTo-SecureString "MyPlainTextAppSpecificPassword" -AsPlainText -Force
$cred = Import-CliXml -Path "C:\Where\To \Save\The\Credentials\myCred_${env:USERNAME}_${env:COMPUTERNAME}.xml"
# Email configuration
$emailParams = @{
SmtpServer = "smtp.gmail.com"
Port = 587
UseSsl = $true
Credential = $cred
From = "[email protected]"
To = "[email protected]"
Subject = "Missing CSV Files"
Body = "The following CSV files are missing or out of sequence:`r`n`r`n$missingFilesString"
}
# Send the email
Send-MailMessage @emailParams
} else {
Write-Host "All CSV files have names starting with numbers in sequence."
}