forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-copy.ps1
102 lines (79 loc) · 3.2 KB
/
file-copy.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
<#
.SYNOPSIS
powershell script to copy files to remote machine
.DESCRIPTION
powershell script to copy files to remote machine using arguments -machineFile -sourcePath -destPath
.NOTES
File Name : file-copy.ps1
Author : jagilber
Version : 150413
.PARAMETER machineFile
file containing list of machines (one per line) to copy files to. Example c:\temp\machines.txt
.PARAMETER sourcePath
source path of files to copy. Example c:\temp\sourcefiles
.PARAMETER destPath
dest path share of files to copy. Example admin$\temp
.EXAMPLE
.\file-copy.ps1 -machineFile machines.txt -sourcePath .\sourcefiles -destPath admin$\temp
deploy all configuration files in default 'configs' or 'configs_templates' folder to local machine using defalut etl output folder of %systemroot%\temp
#>
Param(
[parameter(Position=0,Mandatory=$true,HelpMessage="Enter file containing list of remote machines to copy files to. one machine per line. example: c:\temp\machines.txt")]
[string] $machineFile,
[parameter(Position=1,Mandatory=$true,HelpMessage="Enter source folder containing files. example: c:\temp\sourcefiles")]
[string] $sourcePath,
[parameter(Position=2,Mandatory=$true,HelpMessage="Enter relative destination share path folder containing files. example: `"admin$\temp`"")]
[string] $destPath
)
$logfile = "file-copy.ps1.log"
# ---------------------------------------------------------------------------------------------------------------
function main()
{
if(![IO.File]::Exists($machineFile))
{
log-info "unable to find machineFile $($machineFile). exiting"
return
}
if(![IO.Directory]::Exists($sourcePath))
{
log-info "unable to find source path $($sourcePath). exiting"
return
}
if(![IO.Directory]::Exists("\\127.0.0.1\$($destPath)"))
{
log-info "unable to determine destination path \\127.0.0.1\$($destPath). exiting"
return
}
# get source files
$sourceFiles = [IO.Directory]::GetFiles($sourcePath, "*.*", [System.IO.SearchOption]::TopDirectoryOnly)
[IO.StreamReader] $reader = new-object IO.StreamReader ($machineFile)
while ($reader.Peek() -ge 0)
{
$machine = $reader.ReadLine()
foreach($sourceFile in $sourceFiles)
{
$destFile = [IO.Path]::GetFileName($sourceFile)
$destFile = "\\$($machine)\$($destPath)\$($destFile)"
log-info "copying file $($sourceFile) to $($destFile)"
try
{
[IO.File]::Copy($sourceFile, $destFile, $true)
}
catch
{
log-info "Exception:Copying File:$($sourceFile) to $($destFile): $($Error)"
$Error.Clear()
}
}
}
log-info "finished"
}
# ----------------------------------------------------------------------------------------------------------------
function log-info($data)
{
$data = "$([System.DateTime]::Now):$($data)`n"
Write-Host $data
out-file -Append -InputObject $data -FilePath $logFile
}
# ----------------------------------------------------------------------------------------------------------------
main