Powershell批量压缩并上载CSV数据文件到Box企业云盘
- 人工智能
- 2025-09-13 16:39:02

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 = " upload.box /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 -AutoSizePowershell批量压缩并上载CSV数据文件到Box企业云盘由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Powershell批量压缩并上载CSV数据文件到Box企业云盘”
上一篇
              xss笔记与打靶(更新中)
下一篇
              【蓝桥杯单片机】第十二届省赛
 
               
               
               
               
               
               
               
               
   
   
   
   
   
   
   
   
  