Powershell批量压缩并上载CSV数据文件到Box企业云盘
PowerShell脚本,包含配置文件读取、批量压缩、异步上传、异常处理和日志记录功能:
<#
.SYNOPSIS
CSV文件批量压缩并上传到Box的自动化脚本
.DESCRIPTION
1. 读取INI配置文件参数
2. 批量压缩指定目录的CSV文件
3. 异步上传ZIP文件到Box企业云
4. 完善的异常处理和日志记录
.NOTES
Version: 1.1
Author: AI助手
Created: 2023-10-20
#>
# 配置文件解析函数
function Get-IniContent {
param($FilePath)
$ini = @{}
switch -regex -file $FilePath {
"^\[(.+)\]$" {
$section = $matches[1]
$ini[$section] = @{}
}
"^\s*([^#].+?)\s*=\s*(.*)" {
$name,$value = $matches[1..2]
$ini[$section][$name] = $value.Trim()
}
}
return $ini
}
# 日志记录函数
function Write-Log {
param(
[string]$Message,
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "[$timestamp] [$Level] $Message"
Add-Content -Path $config["Settings"]["LogPath"] -Value $logEntry
if ($Level -eq "ERROR") { Write-Host $logEntry -ForegroundColor Red }
else { Write-Host $logEntry }
}
# Box上传函数
function Invoke-BoxUpload {
param(
[string]$FilePath,
[string]$TargetPath
)
try {
$headers = @{
"Authorization" = "Bearer $($config['Box']['APIToken'])"
"Content-Type" = "application/octet-stream"
}
$fileName = Split-Path $FilePath -Leaf
$uploadUrl = "https://upload.box.com/api/2.0/files/content?parent_id=$TargetPath"
$fileBytes = [System.IO.File]::ReadAllBytes($FilePath)
$boundary = [System.Guid]::NewGuid().ToString()
$body = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
"Content-Type: application/octet-stream`r`n",
[System.Text.Encoding]::UTF8.GetString($fileBytes),
"--$boundary--"
) -join "`r`n"
$response = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $headers `
-Body $body -ContentType "multipart/form-data; boundary=$boundary"
return $response
}
catch {
throw "Box上传失败: $_"
}
}
# 主程序
try {
# 读取配置文件
$configPath = "config.ini"
if (-not (Test-Path $configPath)) {
throw "配置文件 $configPath 不存在"
}
$config = Get-IniContent $configPath
# 验证必要参数
$requiredParams = @{
"Settings" = @("SourcePath", "ZipPath", "LogPath")
"Box" = @("APIToken", "TargetFolderID")
}
foreach ($section in $requiredParams.Keys) {
foreach ($param in $requiredParams[$section]) {
if (-not $config[$section][$param]) {
throw "缺少必要参数 [$section] $param"
}
}
}
# 创建必要目录
@($config["Settings"]["ZipPath"], (Split-Path $config["Settings"]["LogPath"])) | ForEach-Object {
if (-not (Test-Path $_)) { New-Item -Path $_ -ItemType Directory -Force | Out-Null }
}
# 获取CSV文件
$csvFiles = Get-ChildItem -Path $config["Settings"]["SourcePath"] -Filter *.csv
if (-not $csvFiles) {
Write-Log "未找到CSV文件,程序退出"
exit
}
# 批量压缩
$zipJobs = @()
foreach ($file in $csvFiles) {
try {
$zipPath = Join-Path $config["Settings"]["ZipPath"] "$($file.BaseName).zip"
Compress-Archive -Path $file.FullName -DestinationPath $zipPath -Force
Write-Log "成功压缩: $($file.Name) → $zipPath"
# 异步上传
$uploadJob = Start-Job -ScriptBlock {
param($config, $zipPath)
try {
$response = Invoke-BoxUpload -FilePath $zipPath -TargetPath $config["Box"]["TargetFolderID"]
[PSCustomObject]@{
Status = "Success"
Message = "上传成功: $($response.entries[0].name)"
File = $zipPath
}
}
catch {
[PSCustomObject]@{
Status = "Error"
Message = $_.Exception.Message
File = $zipPath
}
}
} -ArgumentList $config, $zipPath
$zipJobs += $uploadJob
}
catch {
Write-Log "压缩失败: $($file.Name) - $_" -Level ERROR
}
}
# 处理上传结果
$zipJobs | Wait-Job | ForEach-Object {
$result = Receive-Job $_
if ($result.Status -eq "Success") {
Write-Log $result.Message
}
else {
Write-Log "$($result.File) 上传失败: $($result.Message)" -Level ERROR
}
}
}
catch {
Write-Log "主程序异常: $_" -Level ERROR
exit 1
}
finally {
Get-Job | Remove-Job -Force
Write-Log "程序执行完成"
}
配置文件示例 (config.ini):
[Settings]
SourcePath=C:\CSVFiles
ZipPath=C:\ZIPArchive
LogPath=C:\Logs\FileUpload.log
[Box]
APIToken=your_box_api_token
TargetFolderID=1234567890 # Box目标文件夹ID
功能说明:
-
配置文件管理:
- 使用INI格式配置文件
- 支持路径配置和API令牌管理
- 自动验证必要参数
-
压缩功能:
- 自动遍历指定目录的CSV文件
- 为每个CSV生成独立的ZIP文件
- 支持覆盖已存在的ZIP文件
-
异步上传:
- 使用PowerShell Jobs实现异步操作
- 每个文件独立上传任务
- 自动等待所有任务完成
-
日志功能:
- 支持不同日志级别(INFO/ERROR)
- 控制台颜色区分错误信息
- 详细记录操作时间戳
-
异常处理:
- 全局异常捕获
- 文件级错误处理
- 网络请求错误处理
使用说明:
- 创建配置文件
config.ini
- 修改配置参数为实际值
- 以管理员身份运行脚本
- 查看生成的日志文件
注意事项:
- 需要PowerShell 5.1或更高版本
- Box API Token需要具有文件上传权限
- 确保网络可以访问Box API端点
- 大文件上传建议增加分块上传逻辑
- 可根据需要调整压缩级别和并发数
可以通过以下命令查看运行状态:
Get-Job | Format-Table -AutoSize