-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperCopy.ps1
123 lines (99 loc) · 5.26 KB
/
SuperCopy.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
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
121
122
123
# CreatedBy : Fadhly Permata
# CreatedOn : 2017.01.31
# EXEC SAMPLE:
# PS D:\- JOBS\PowerShell> .\SuperCopy.ps1
# -Server "10.53.30.154"
# -ServerUID "dtmp0dg"
# -ServerPID "********"
# -Src "D:\- SHARE\tfsRelease\WS_DPLK"
# -Dst "D:\- SHARE\tfsRelease\Copyan"
# -ActionType "copy-except"
# -Files @('*.config')
param
(
[Parameter()] [string]$Server = $(throw "PS-Script SettingsCopy error: ""Server"" parameter is mandatory, please define the value."),
[Parameter()] [string]$ServerUID = $(throw "PS-Script SettingsCopy error: ""ServerUID"" parameter is mandatory, please define the value."),
[Parameter()] [string]$ServerPID = $(throw "PS-Script SettingsCopy error: ""ServerPID"" parameter is mandatory, please define the value."),
[Parameter()] [string]$Src = $(throw "PS-Script SettingsCopy error: ""Src"" parameter is mandatory, please define the value."),
[Parameter()] [string]$Dst = $(throw "PS-Script SettingsCopy error: ""Dst"" parameter is mandatory, please define the value."),
[Parameter()] [string]$ActionType = $(throw "PS-Script SettingsCopy error: ""ActionType"" parameter is mandatory, please define the value."),
[Parameter()] [string[]]$Files
)
### HELPER METHODS ###
function console-log {
param (
[Parameter()] [string]$logText = $(throw "PS-Script log error: ""LogText"" parameter is mandatory, please define the value.")
)
Write-Host "==> $logText" -ForegroundColor Yellow -BackgroundColor Black;
}
### FILE or FOLDER COPIER METHODS ###
function create-path-and-copy {
param (
[Parameter()] [string]$fileSource,
[Parameter()] [string]$fileDest
)
console-log -logText $fileSource;
console-log -logText $fileDest;
$sPath = [System.IO.Path]::GetDirectoryName($fileDest);
if (!(Test-Path $sPath)) { [System.IO.Directory]::CreateDirectory($sPath); }
Copy-Item -Path $fileSource -Destination $fileDest -Verbose -Force;
}
function copy-all {
console-log "Copy files (recursively) from [$Src] into [$Dst]";
## check source and destination folder
if (!(Test-Path $Src)) { throw "PS-Script copy-all error: Source directory is not found."; }
if ($Src.Substring($Src.Length - 1) -ne "*") { $Src = [System.IO.Path]::Combine($Src, "*"); }
if (!(Test-Path $Dst)) { [System.IO.Directory]::CreateDirectory($Dst); }
Copy-Item -Path $Src -Destination $Dst -Recurse -Force -Verbose;
}
function copy-only {
if ($Files.Length -eq 0) {
throw "PS-Script copy-only error: ""Files"" parameter is mandatory, please define the files that will be processed.";
} else {
console-log "Copy files (recursively) from [$Src] into [$Dst]";
## check source and destination folder
if (!(Test-Path $Src)) { throw "PS-Script copy-only error: Source directory is not found."; }
if (!(Test-Path $Dst)) { [System.IO.Directory]::CreateDirectory($Dst); }
$include = $Files;
$listFiles = Get-ChildItem $Src -Include $include -Recurse | Where-Object { $_.PSIsContainer -eq $False };
foreach ($file in $listFiles) {
$sDest = Join-Path $Dst $file.FullName.Substring(([System.String]$Src).Length);
create-path-and-copy -fileSource $file.FullName -fileDest $sDest;
}
}
}
function copy-except {
If ($Files.Length -eq 0) {
throw "PS-Script copy-except error: ""Files"" parameter is mandatory, please define the files that will not be processed.";
} else {
console-log "Copy files (recursively) from [$Src] into [$Dst]";
## check source and destination folder
if (!(Test-Path $Src)) { throw "PS-Script copy-except error: Source directory is not found."; }
if (!(Test-Path $Dst)) { [System.IO.Directory]::CreateDirectory($Dst); }
$exclude = $Files;
$listFiles = Get-ChildItem $Src -Exclude $exclude -Recurse | Where-Object { $_.PSIsContainer -eq $False };
foreach ($file in $listFiles) {
$sDest = Join-Path $Dst $file.FullName.Substring(([System.String]$Src).Length);
create-path-and-copy -fileSource $file.FullName -fileDest $sDest;
}
}
}
#### MAIN PROC'S ####
Clear-Host;
try {
console-log -logText "Try to Log-in into $Server";
net use "\\$Server" "$ServerPID" /USER:"$ServerUID";
If ($ActionType -eq 'copy-except') { copy-except; }
ElseIf ($ActionType -eq 'copy-only') { copy-only; }
ElseIf ($ActionType -eq 'copy-all') { copy-all; }
Else {
$(throw "PS-Script SettingsCopy error: Unknown value for ""ActionType"" parameter.");
}
}
catch [System.Exception] {
console-log -logText "Could not copy files to remote server $Server... $_";
}
finally {
console-log -logText "Log-out from $Server machine.";
net use "\\$Server" /delete;
}