如何修复工作脚本以便能够在多个目录中使用它?
How to fix a working script to be able to use it in multiple directories?
首先让我说这个地方是一个很好的资源,它帮助我开发了一个运行良好的脚本,我正在考虑扩展它,但我在这样做时遇到了一些麻烦。
所以,基本上我能够根据那些年的年份和月份将一个包含日志文件的目录组织到多个目录中。当我指向那些目录目录时,我可以这样做。
我想要做的是将这些脚本应用于多个目录,基本上(每个)多个目录中有数百个日志,但所有这些目录都在一个主目录中。
我尝试使用 foreach cmdlet 应用脚本,但它似乎不起作用。
#--------
# Creation of an "Archive" folder within the root directory of the targeted dir.
# Comments: Archives Folder is created in each targeted directory.
#--------
foreach($folder in (Get-ChildItem '\drive\software\logstoreage \directories' -Directory)){
New-Item -ItemType directory -Path ($folder.fullname+"\Archive")
}
#--------
# Organization of Logs into Yearly and Monthly directories.
# Comments: Folders are created based on the Year capturing logs through the end of 2018.
#--------
$date = (Get-Date -Month 1 -Day 1 -Year 2019).ToString("01-01-2019")
$files = Get-ChildItem '\drive\software\logstoreage\directories\SoftwareA' -Recurse | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer}
$files
$targetPath = '\drive\software\logstoreage\directories\SoftwareA\Archive'
foreach ($file in $files){
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()
$file.Name
$year
$month
$Directory = $targetPath + "\" + $year + "\" + $month
if (!(Test-Path $Directory)){
New-Item $directory -type directory
}
$file | Move-Item -Destination $Directory
}
所以你有一个循环来获取所有目标文件夹,你只需要将脚本的其余部分包含在该循环中,并根据它在循环中进行一些修改。
foreach($folder in (Get-ChildItem '\drive\software\logstoreage\directories' -Directory)){
#--------
# Creation of an "Archive" folder within the root directory of the targeted dir.
# Comments: Archives Folder is created in each targeted directory.
#--------
$ArchiveFolder = New-Item -ItemType directory -Path ($folder.fullname+"\Archive") -Force
#--------
# Organization of Logs into Yearly and Monthly directories.
# Comments: Folders are created based on the Year capturing logs through the end of 2018.
#--------
$date = Get-Date -Month 1 -Day 1 -Year 2019
$files = Get-ChildItem $folder -Recurse -File | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer}
$files
$targetPath = $ArchiveFolder.FullName
foreach ($file in $files){
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()
$file.Name
$year
$month
$Directory = $targetPath + "\" + $year + "\" + $month
if (!(Test-Path $Directory)){
New-Item $directory -type directory
}
$file | Move-Item -Destination $Directory
}
}
首先让我说这个地方是一个很好的资源,它帮助我开发了一个运行良好的脚本,我正在考虑扩展它,但我在这样做时遇到了一些麻烦。
所以,基本上我能够根据那些年的年份和月份将一个包含日志文件的目录组织到多个目录中。当我指向那些目录目录时,我可以这样做。
我想要做的是将这些脚本应用于多个目录,基本上(每个)多个目录中有数百个日志,但所有这些目录都在一个主目录中。
我尝试使用 foreach cmdlet 应用脚本,但它似乎不起作用。
#--------
# Creation of an "Archive" folder within the root directory of the targeted dir.
# Comments: Archives Folder is created in each targeted directory.
#--------
foreach($folder in (Get-ChildItem '\drive\software\logstoreage \directories' -Directory)){
New-Item -ItemType directory -Path ($folder.fullname+"\Archive")
}
#--------
# Organization of Logs into Yearly and Monthly directories.
# Comments: Folders are created based on the Year capturing logs through the end of 2018.
#--------
$date = (Get-Date -Month 1 -Day 1 -Year 2019).ToString("01-01-2019")
$files = Get-ChildItem '\drive\software\logstoreage\directories\SoftwareA' -Recurse | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer}
$files
$targetPath = '\drive\software\logstoreage\directories\SoftwareA\Archive'
foreach ($file in $files){
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()
$file.Name
$year
$month
$Directory = $targetPath + "\" + $year + "\" + $month
if (!(Test-Path $Directory)){
New-Item $directory -type directory
}
$file | Move-Item -Destination $Directory
}
所以你有一个循环来获取所有目标文件夹,你只需要将脚本的其余部分包含在该循环中,并根据它在循环中进行一些修改。
foreach($folder in (Get-ChildItem '\drive\software\logstoreage\directories' -Directory)){
#--------
# Creation of an "Archive" folder within the root directory of the targeted dir.
# Comments: Archives Folder is created in each targeted directory.
#--------
$ArchiveFolder = New-Item -ItemType directory -Path ($folder.fullname+"\Archive") -Force
#--------
# Organization of Logs into Yearly and Monthly directories.
# Comments: Folders are created based on the Year capturing logs through the end of 2018.
#--------
$date = Get-Date -Month 1 -Day 1 -Year 2019
$files = Get-ChildItem $folder -Recurse -File | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer}
$files
$targetPath = $ArchiveFolder.FullName
foreach ($file in $files){
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()
$file.Name
$year
$month
$Directory = $targetPath + "\" + $year + "\" + $month
if (!(Test-Path $Directory)){
New-Item $directory -type directory
}
$file | Move-Item -Destination $Directory
}
}