-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.ps1
37 lines (32 loc) · 1.47 KB
/
import.ps1
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
# Define the directory path and input CSV file path
$directoryPath = "Z:\exportfolder"
$inputCsvPath = "metadata.csv"
# Read the metadata from the CSV file
$metadataList = Import-Csv -Path $inputCsvPath
# Iterate over each metadata entry
foreach ($metadata in $metadataList) {
# Construct the full path of the file from the relative path
$filePath = Join-Path -Path $directoryPath -ChildPath $metadata.RelativePath
# Check if the file exists
if (Test-Path -Path $filePath) {
try {
# Update file timestamps
Set-ItemProperty -Path $filePath -Name CreationTime -Value ([datetime]$metadata.CreationTime)
Set-ItemProperty -Path $filePath -Name LastWriteTime -Value ([datetime]$metadata.LastWriteTime)
Set-ItemProperty -Path $filePath -Name LastAccessTime -Value ([datetime]$metadata.LastAccessTime)
# Update ownership if possible
$owner = $metadata.Owner
if ($owner -and $owner -match "\\") {
$acl = Get-Acl -Path $filePath
$user = New-Object System.Security.Principal.NTAccount($owner)
$acl.SetOwner($user)
Set-Acl -Path $filePath -AclObject $acl
}
} catch {
Write-Error "Failed to update metadata for $filePath: $_"
}
} else {
Write-Host "File not found: $filePath" -ForegroundColor Yellow
}
}
Write-Output "Metadata has been imported and applied to files in $directoryPath"