-
Notifications
You must be signed in to change notification settings - Fork 1
/
Set-DirectoryStructure.ps1
58 lines (53 loc) · 2.3 KB
/
Set-DirectoryStructure.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
$Clients = "ABC", "NMO", "XYZ"
$Folders = "1. Vendor Info", "2. Network Infrastructure", "2. Network Infrastructure\1. Firewall Configs", "3. Reports"
$ClientArchive = "Z. Archived"
$OrphanFolder = "Z. Orphans"
$TargetFolder = "D:\Sample"
#Check if path already exists, if it doesn't, create it
function New-DirectoryIfNeeded {
param (
[Parameter(Mandatory = $true)]
$Path
)
if (!(Test-Path $Path)) {
New-Item $Path -ItemType Directory
}
return (Test-Path $Path)
}
if (Test-Path $TargetFolder) {
ForEach ($Client in $Clients) {
#Make the Client Folder
$ClientFolder = New-DirectoryIfNeeded "$TargetFolder\$Client"
if ($ClientFolder) {
$Folders | ForEach-Object {
$Folder = New-DirectoryIfNeeded "$TargetFolder\$Client\$_"
}
#Create Orphan Folder
$OrphanFolderObj = New-DirectoryIfNeeded "$TargetFolder\$Client\$OrphanFolder"
#Move Orphaned Objects to the Orphan folder
if ($OrphanFolderObj) {
$ExistingFolders = Get-ChildItem "$TargetFolder\$Client"
$ExistingFolders | ForEach-Object {
if ($_.Name -notin $Folders -and $_.Name -ne $OrphanFolder) {
$NewName = "$($_.Name) - Archived $(Get-Date -Format "yyyy-MM-dd_hhmmss")"
$NewObject = Rename-Item -Path $_.FullName -NewName $NewName -PassThru
Move-Item -Path $NewObject.FullName -Destination "$TargetFolder\$Client\$OrphanFolder\"
}
}
}
}
}
#Create Client Archive
$ClientArchiveFolder = New-DirectoryIfNeeded "$TargetFolder\$ClientArchive"
#Move any items from the root folder into archived if they aren't current clients
if ($ClientArchiveFolder) {
$ExistingClientFolders = Get-ChildItem $TargetFolder
$ExistingClientFolders | ForEach-Object {
if ($_.Name -notin $Clients -and $_.Name -ne $ClientArchive) {
$NewName = "$($_.Name) - Archived $(Get-Date -Format "yyyy-MM-dd_hhmmss")"
$NewObject = Rename-Item -Path $_.FullName -NewName $NewName -PassThru
Move-Item -Path $NewObject.FullName -Destination "$TargetFolder\$ClientArchive"
}
}
}
}