-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-workflow.ps1
85 lines (74 loc) · 3.31 KB
/
test-workflow.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
<#
.SYNOPSIS
Copies files from a specified source directory to a destination directory on remote computers with progress reporting.
.DESCRIPTION
This script defines a function `Copy-WithProgress` that:
1. Takes a source directory, a destination directory, and a list of remote hosts.
2. Connects to each remote host using a PowerShell session.
3. Copies files from the source directory to the destination directory on each remote host.
4. Displays progress information for each file being copied.
.PARAMETER Source
The path to the source directory on the local machine from which files will be copied.
.PARAMETER Destination
The path to the destination directory on the remote computers where files will be copied to.
.PARAMETER RemoteHost
An array of remote computer names or IP addresses to which files will be copied.
.EXAMPLE
Copy-WithProgress -Source "C:\projects" -Destination "U:\Backup\test" -RemoteHost "vm1", "vm2"
This example copies files from "C:\projects" on the local machine to "U:\Backup\test" on the remote computers "vm1" and "vm2".
Progress is displayed for each file being copied.
.NOTES
- The function uses PowerShell remoting (New-PSSession) to connect to remote computers.
- Ensure that PowerShell remoting is enabled and properly configured on the remote computers.
- The `-UseSSL` parameter in `New-PSSession` requires that the remote session be secured with SSL.
Author : Ivica Agatunovic
WebSite: https://github.com/ivicaagatunovic
Linkedin: www.linkedin.com/in/ivica-agatunovic-96090024
#>
Function Copy-WithProgress
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$Source,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$Destination,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$RemoteHost
)
$Source=$Source.tolower()
$Filelist=Get-Childitem "$Source" -Recurse
$Total=$Filelist.count
$Position=0
Write-Host "Number of items to copy: $Total"
Write-Host "Source: $Source"
Write-Host "Destination $Destination"
foreach($Computer in $RemoteHost){
Write-Output "attempting to open a session to $Computer"
try{
$session = New-PSSession -ComputerName $Computer -Name copysession -UseSSL
Write-Output "Successfully opened a connection to $Computer"
Write-Output "Copying the files to $Computer"
foreach ($File in $Filelist)
{
$Filename=$File.Fullname.tolower().replace($Source,'')
$DestinationFile=($Destination+$Filename)
Write-Progress -Activity "Copying data from '$source' to '$Destination'" -Status "Copying File $Filename" -PercentComplete (($Position/$total)*100)
Copy-Item $File.FullName -Destination $DestinationFile -ToSession $session -Force
$Position++
}
Remove-PSSession -Session $session
}
catch{
Write-Output "connection to $Computer failed"
}
}
}
Copy-WithProgress -Source "source" -Destination "c:\TEMP" -RemoteHost vm1, vm2