我在这个问题上一直在寻找几个小时。我正在寻找一些帮助来修复这个脚本。脚本将在共享文件夹中搜索子文件夹中的文件,并将该文件夹压缩到该子文件夹中。我还需要做的是添加文件或文件夹的最后访问时间,以便我可以设置它的年龄,例如1年或365天。然后7z会在压缩后删除文件,以节省服务器上的空间用于其他事情。
所以c:\ share C:\共享\文件夹1 C:\共享\文件夹2 C:\共享\ folder3 等...
我测试中的脚本就像这样
<#
.SYNOPSIS
<A brief description of the script>
.DESCRIPTION
<A detailed description of the script>
.PARAMETER <paramName>
<Description of script parameter>
.EXAMPLE
<An example of using the script>
#>
#Compress all the files based on your folder structure
Set-Location -Path 'c:\shared'
Get-ChildItem -Path 'c:\shared' -Recurse | Where-Object {$_.PSIsContainer } | ForEach-Object {
$directoryFullName = $_.FullName
$directoryName = $_.Name
Invoke-Expression -Command 'C:\7-Zip\7z.exe a "-x!*.zip" -sdel $directoryFullName\$directoryName.zip $directoryFullName\*'
}
我正在尝试下面这个但是我还不了解格式化属性的错误。
<#
.SYNOPSIS
<A brief description of the script>
.DESCRIPTION
<A detailed description of the script>
.PARAMETER <paramName>
<Description of script parameter>
.EXAMPLE
<An example of using the script>
#>
#Compress all the files based on your folder structure
Set-Location -Path 'c:\shared'
Get-ChildItem -Path 'c:\shared' -Recurse | Where-Object {$_.PSIsContainer $_.LastWriteTime -gt "01-01-1900" -and $_.LastWriteTime -lt (get-date).AddDays(-365) } | ForEach-Object {
$directoryFullName = $_.FullName
$directoryName = $_.Name
Invoke-Expression -Command 'C:\7-Zip\7z.exe a "-x!*.zip" -sdel $directoryFullName\$directoryName.zip $directoryFullName\*'
}
这是错误
At C:\Users\HarrelsonNetworks\Documents\windowspowershell\Scripts\testbackup3.ps1:14 char:75
+ ... Path 'c:\shared' -Recurse | Where-Object {$_.PSIsContainer $_.LastWri ...
+ ~~
Unexpected token '$_' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
FullyQualifiedErrorId : UnexpectedToken
感谢
求助
jharl
答案 0 :(得分:0)
我建议如下:1。创建一个临时文件夹2.将所需的一切移动(不复制)到临时文件夹3.压缩临时文件并保存到所需位置3.删除临时文件夹。
将其分解为这样的步骤可以让您更好地解决问题。
以下是我最近写的类似内容,但它并没有解决您的问题,但它应该指向正确的方向:
function archiveLogs ( [string]$OriginalLocation, [string]$NewLocation ){
## Age of files to be archived
$DeleteOlderThan = 30
# Create temp folder to store files to be archived
New-Item c:\Temp\_tempfolder -type directory
# Get all children of specific directory older than 'X' days and of ".log" file type and move to temp folder
get-childitem -Path $OriginalLocation\*.log |
where-object {$_.LastWriteTime -lt (get-date).AddDays(-$DeleteOlderThan)} |
move-item -destination "C:\Temp\_tempfolder"
## Zip all .log files in temp folder and save to '$NewLocation\(DATE)_Archive'
& "C:\temp\7za.exe" a -tzip $NewLocation\$((Get-Date).ToString('yyyy-MM-dd HH-mm'))_Archive -r c:\Temp\_tempfolder\*.log
## Delete temp folder
Remove-Item c:\temp\_tempfolder -recurse -ErrorAction SilentlyContinue
古德勒克,如果您有任何问题,请告诉我。
答案 1 :(得分:0)
您错过了逻辑运算符-and
。要解决此错误,请更改:
{$_.PSIsContainer $_.LastWriteTime -gt "01-01-1900" -and $_.LastWriteTime -lt (get-date).AddDays(-365) }
要:
{$_.PSIsContainer -and ($_.LastWriteTime -gt "01-01-1900") -and ($_.LastWriteTime -lt (get-date).AddDays(-365)) }