-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemote-Computer-File-Transfer.ps1
111 lines (93 loc) · 4.4 KB
/
Remote-Computer-File-Transfer.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
<#
.SYNOPSIS
This script transfers files to remote computers.
.DESCRIPTION
This script transfers files to remote computers. In '.\Files Paths.txt' file user can write full paths to files for transfer, and
in '.\Remote Computers.txt' user can write list of remote computers to which files are transferred, ether by hostname or IP address.
Script generates detailed log file, '.\File Transfer Log.log', and report that is sent via email to system administrators.
.NOTES
Version: 1.6
Author: Zoran Jankov
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Clears the contents of the DNS client cache
Clear-DnsClientCache
#Loading script configuration
$Settings = Get-Content "$PSScriptRoot\Settings.cfg" | ConvertFrom-StringData
$PSScriptRoot
#Defining network drive thru which files will be to transferred
$NetworkDrive = "T"
[System.Object[]]$FileList = Get-Content -Path $Settings.FileList
[System.Object[]]$ComputerList = Get-Content -Path $Settings.ComputerList
$SuccessfulTransfers = 0
$FailedTransfers = 0
#-----------------------------------------------------------[Functions]------------------------------------------------------------
Import-Module "$PSScriptRoot\Modules\Write-Log.psm1"
Import-Module "$PSScriptRoot\Modules\Start-FileTransfer.psm1"
Import-Module "$PSScriptRoot\Modules\Send-EmailReport.psm1"
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Write-Log -Message $Settings.LogTitle -NoTimestamp
Write-Log -Message $Settings.LogSeparator -NoTimestamp
#Get credential from user input
Write-Log -Message "Enter username and password"
$Credential = Get-Credential
Write-Log -Message ("User " + $Credential.UserName + " has entered credentials")
foreach($File in $FileList) {
if (Test-Path -Path $File) {
Write-Log -Message "Successfully checked $File file - ready for transfer"
}
else {
$Message = "Failed to access $File file - it does not exist.`nScript stopped - MISSING FILE ERROR"
Write-Log -Message $Message
Write-Log -Message $Settings.LogSeparator -NoTimestamp
Send-EmailReport -Settings $Settings -FinalMessage $Message
Exit
}
}
Write-Log -Message "Successfully accessed all files - ready for transfer"
#Start file transfer
foreach ($Computer in $ComputerList) {
if (Test-Connection -TargetName $Computer -Quiet -Count 1) {
Write-Log -Message "Successfully connected to $Computer remote computer"
$Partition = $Settings.Partition
$NetworkPath = "\\$Computer\$Partition$"
if (New-PSDrive -Name $NetworkDrive -Persist -PSProvider "FileSystem" -Root $NetworkPath -Credential $Credential) {
Write-Log -Message "Successfully mapped network drive to $Partition partition on the $Computer remote computer"
Start-FileTransfer -FileList $FileList -Destination ($NetworkDrive + ":\" + $Settings.TransferFolder) | `
ForEach-Object {
$SuccessfulTransfers += $_.Successful
$FailedTransfers += $_.Failed
}
Remove-PSDrive -Name $NetworkDrive
}
else {
Write-Log -Message "Failed to map network drive to C partition on the $Computer remote computer"
$FailedTransfers += $FileList.Length
Write-Log -Message "Canceled file transfer to $Computer remote computer"
}
}
else {
Write-Log -Message "Failed to connected to $Computer remote computer"
$FailedTransfers += $FileList.Length
Write-Log -Message "Canceled file transfer to $Computer remote computer"
}
}
if($SuccessfulTransfers -gt 0) {
Write-Log -Message "Successfully transferred $SuccessfulTransfers files"
}
if ($FailedTransfers -gt 0) {
Write-Log -Message "Failed to transfer $FailedTransfers files"
}
if(($SuccessfulTransfers -gt 0 ) -and ($FailedTransfers -eq 0)) {
$Message = "Successfully transferred all files"
}
elseif (($SuccessfulTransfers -gt 0 ) -and ($FailedTransfers -gt 0)) {
$Message = "Successfully transferred some files with some failed"
}
elseif (($SuccessfulTransfers -eq 0 ) -and ($FailedTransfers -gt 0)) {
$Message = "Failed to transfer any file"
}
Write-Log -Message $Message
Write-Log -Message $Settings.LogSeparator -NoTimestamp
#Sends email with detailed report and deletes temporary report log file
Send-EmailReport -Settings $Settings -FinalMessage $Message