1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| # INIT VARIABLES
###############################################################################################
clear
$DATE = Get-Date -Format "yyyy-MM-dd_HHmm_"
$OUTPUT_CSV = "c:\temp\" + $DATE + "NameDataMove.csv"
$CSV_CONTENT = @()
$ERRORS_CSV = "c:\temp\" + $DATE + "NameDataMoveError.csv"
$CSV_CONTENT_ERROR = @()
# Be careful with the format of the text and the '\' !
$SOURCE_FOLDER_PATH = "\\serveur1\Share\" #folder not inherited
$SOURCE_FOLDER_NAME = "Share" # NO SLASH, ONLY THE NAME
$DESTINATION_FOLDER_PATH = "\\serveur2\Share\"
$DATE_REFERENCE = (Get-Date).AddDays(-545)
# FUNCTIONS
###############################################################################################
function Get-AclInheritanceIsEnable {
param($Acl)
$expandedAclAccess = $Acl | Select-Object -ExpandProperty Access
$inheritanceEnabled = $false
foreach ($access in $expandedAclAccess) {
if ($access.IsInherited) {
$inheritanceEnabled = $true
break
}
}
$inheritanceEnabled
}
# MAIN
###############################################################################################
Write-Host("######################################################")
Write-Host("CREATING FOLDERS STRUCTURE")
Write-Host("------------------------------------------------------")
# Creating MAIN folder with right ACL
$check = Test-Path -Path ($DESTINATION_FOLDER_PATH + $SOURCE_FOLDER_NAME) -PathType Container
if($check -eq $false) {
New-Item -Path ($DESTINATION_FOLDER_PATH + $SOURCE_FOLDER_NAME) -ItemType Directory
}
$sourceACL = Get-Acl -Path $SOURCE_FOLDER_PATH
$sourceACL | Set-Acl ($DESTINATION_FOLDER_PATH + $SOURCE_FOLDER_NAME)
# Get all folders and sub-folders in MAIN folder
$folders = Get-ChildItem $SOURCE_FOLDER_PATH -Recurse | Where-Object { $_.PSIsContainer -eq $true }
# Create directories structure
foreach ($folder in $folders) {
# Get the path of the folder to create but only the right part (we delete the left part of the path until the name of the source folder)
$pos = $folder.FullName.IndexOf($SOURCE_FOLDER_NAME)
$folderPathWithoutRoot = $folder.FullName.Substring($pos) + "\"
# Create destination path by merging the destination folder with the tree of the folder to create
$destination = Join-Path -Path $DESTINATION_FOLDER_PATH -ChildPath $folderPathWithoutRoot
# Check if existing folder
if(Test-Path -Path $destination -PathType Container) {
# Nothing to do, already created
} else {
# Create the folder
New-Item -Path $destination -ItemType Directory
#$destination # display created folder
}
# Clone ACL
#Get-Acl -Path $folder.FullName | Set-Acl -Path $destination
}
Write-Host("######################################################")
Write-Host("MOVING OLD FILES")
Write-Host("------------------------------------------------------")
# Get all files and sub-files BUT NO .LNK FILES
$items = Get-ChildItem $SOURCE_FOLDER_PATH -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.LastWriteTime -lt $DATE_REFERENCE -and $_.Extension -notlike ".symlink" -and $_.Extension -notlike ".lnk" }
# Move each old modified file and create a shortcut
foreach ($item in $items) {
# Get the path of the file to move but only the right part (we delete the left part of the path until the name of the source folder)
$pos = $item.FullName.IndexOf($SOURCE_FOLDER_NAME)
$filePathWithoutRoot = $item.FullName.Substring($pos)
# Create destination path by merging the destination folder with the tree of the file to move
$destination = Join-Path -Path $DESTINATION_FOLDER_PATH -ChildPath $filePathWithoutRoot
# Move all file in the destination folder
Move-Item -Path $item.FullName -Destination $destination
# Create the link
try {
New-Item -ItemType SymbolicLink -Name $item.Name -Path $item.PSParentPath -Target $destination -Force
#$destination # display moved file
} catch {
# Add error log to CSV
$CSV_CONTENT_ERROR += [PSCustomObject]@{
'filePath' = $item.FullName
'messageError' = $_
}
}
# Add log to CSV
$CSV_CONTENT += [PSCustomObject]@{
'Source' = $item.FullName
'Destination' = $destination
}
#($item.FullName + ";" + $destination) >> $OUTPUT_CSV
}
# Create the CSV
$CSV_CONTENT | Export-Csv $OUTPUT_CSV -Delimiter ";" -NoTypeInformation
$CSV_CONTENT_ERROR | Export-Csv $ERRORS_CSV -Delimiter ";" -NoTypeInformation |
Partager