如果文件夹 creation_date 包含今天的日期,则使用 powershell 创建一个 zip 文件

如果文件夹 creation_date 包含今天的日期,则使用 powershell 创建一个 zip 文件

我编写了以下脚本,尽管文件夹 creation_date 等于今天的日期,但它永远不会返回创建的 zip 文件

# Define the path to your folder and backup folder
$sourceFolder = "C:\Deployment"
$backup
Folder = "C:\Backup"
$source = Get-Item $sourceFolder
$today = Get-Date

if (-not (Test-Path $backupFolder)) {
    New-Item -ItemType Directory -Path $backupFolder
    Write-Host "Backup Folder created successfully."
} else {
    Write-Host "Backup Folder already exists."
}

# Check if the folder creation has todays date
if ($sourceFolder.CreationTime.Date -eq $today.Date) {
    Write-Host "Deployment Folder was created today."
    # Compress the folder into a zip file
    $zipFileName = "$backupFolder\backup_$today.zip"
    Add-Type -A 'System.IO.Compression.FileSystem'
    [System.IO.Compression.ZipFile]::CreateFromDirectory("$sourceFolder", $zipFileName)
    Write-Host "Folder zipped successfully. Backup created at $zipFileName"
} else {
    Write-Host "Deployment Folder was not created today."
}

如果上述脚本有任何错误,请告诉我

答案1

它有很多琐碎的错误。

首先,您尝试获取CreationDate字符串的属性:$sourceFolder.CreationTime.Date$source其类型为DirectoryInfo,应该改用 。

然后你得到如下内容:

$backup
Folder = "C:\Backup"

这是无效语法,我认为这是复制粘贴错误。您应该确保每次提问时,最小可重现示例都是有意义的并且可以执行。

相关内容