-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1034.ps1
More file actions
200 lines (183 loc) · 5.82 KB
/
1034.ps1
File metadata and controls
200 lines (183 loc) · 5.82 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
##################################################
# cmdlets
##################################################
#-------------------------------------------------
# New-SharpSession
#-------------------------------------------------
# Usage: New-SharpSession -?
#-------------------------------------------------
function New-SharpSession {
<#
.Synopsis
Used to open an SSH or SCP Session to the specified server.
.Description
Uses SharpSSH to open an SSH or SCP Session to the specified server.
.Parameter Host
The hostname or IP address that you want to connect to.
.Parameter UserName
The username string used for the connection.
.Parameter Key
The switch that tells function to execute with key parameters.
.Parameter KeyFile
The path and file name to the key file used for key based
authentication.
.Parameter User
The switch that tells function to execute with user parameters.
.Parameter Password
The password string used for the connection.
#>
[CmdletBinding(DefaultParameterSetName="KeySet")]
param(
[Parameter(Mandatory=$True)][String]$Host,
[Parameter(Mandatory=$True)][String]$UserName,
[Parameter(ParameterSetName="KeySet")][Switch]$Key,
[Parameter(ParameterSetName="KeySet", Mandatory=$True)][String]$KeyFile,
[Parameter(ParameterSetName="User")][Switch]$User,
[Parameter(ParameterSetName="User", Mandatory=$True)][String]$Password,
[Switch]$SCP,
[Switch]$PassThru
)
try {
# Default will always be to do SSH...
if ($SCP){
$ConnectType = "Tamir.SharpSsh.scp"
}
else{
$ConnectType = "Tamir.SharpSsh.SshShell"
}
if ($Key -and (Test-Path $KeyFile)){
$Global:SharpSession = New-Object $ConnectType $Host, $UserName
$Global:SharpSession.AddIdentityFile((Resolve-Path $KeyFile))
}
else{
$Global:SharpSession = New-Object $ConnectType $Host, $UserName, $Password
}
$Global:SharpSession.Connect()
if ($ConnectType -eq "Tamir.SharpSsh.SshShell"){
$Global:SharpSession.RemoveTerminalEmulationCharacters = $True
}
if($PassThru){
return $Global:SharpSession
}
}
catch {
throw $_
}
}
#-------------------------------------------------
# Transfer-SharpFile
#-------------------------------------------------
# Usage: Transfer-SharpFile -?
#-------------------------------------------------
function Transfer-SharpFile {
<#
.Synopsis
Used to tranfser(get or put) a file using a valid SCP SharpSession.
.Description
Uses SharpSSH to tranfer (get or put) a file via SCP.
.Parameter From
The full path and file name to the file you are transferring.
.Parameter To
The full path and file name to where you want to copy the file to.
#>
param(
[Parameter(Mandatory=$True)][String]$From,
[Parameter(Mandatory=$True)][String]$To,
[Switch]$Get,
[Switch]$Put
)
try {
if (!($Get -or $Put)) {
throw Write-Host "Get or Put must be defined!" -ForeGroundColor Red
}
if ($SharpSession.GetType().Fullname -ne "Tamir.SharpSsh.Scp"){
throw Write-Host "Not currently connected using SCP!" -ForeGroundColor Red
}
else{
# The action scriptblock for an EventObject must have
# things defined in the Global scope. Yuck!
$Global:SharpSessiontransferDone = $Null
$Global:SharpSessiontotalBytes = $Null
$Global:SharpSessiontransferredBytes = $Null
[void] (Register-ObjectEvent -InputObject $SharpSession -EventName OnTransferProgress -SourceIdentifier Scp.OnTransferProgress `
-Action {$Global:SharpSessiontotalBytes = $args[3]; $Global:SharpSessiontransferredBytes = $args[2]})
[void] (Register-ObjectEvent -InputObject $SharpSession -EventName OnTransferEnd -SourceIdentifier Scp.OnTransferEnd `
-Action {$Global:SharpSessiontransferDone = $True})
# Default is always to get.
if ($Get){
$SharpSession.Get($From, $To)
try{
do {Write-Progress -Activity "Get - $($From)" -Status "Copying" `
-PercentComplete (($SharpSessiontransferredBytes/$SharpSessiontotalBytes)*100)} while ($SharpSessiontransferDone -eq $Null)
}
catch{
Write-Warning "File is not there..."
}
}
else{
$SharpSession.Put($From, $To)
try{
do {Write-Progress -Activity "Get - $($From)" -Status "Copying" `
-PercentComplete (($SharpSessiontransferredBytes/$SharpSessiontotalBytes)*100)} while ($SharpSessiontransferDone -eq $Null)
}
catch{
Write-Warning "File is not there..."
}
}
}
}
catch {
throw $_
}
finally {
if (Get-EventSubscriber -SourceIdentifier Scp.OnTransferProgress){
[void] (Unregister-Event -SourceIdentifier Scp.OnTransferProgress)
}
if (Get-EventSubscriber -SourceIdentifier Scp.OnTransferEnd){
[void] (Unregister-Event -SourceIdentifier Scp.OnTransferEnd)
}
}
}
#-------------------------------------------------
# Remove-SharpSession
#-------------------------------------------------
# Usage: Remove-SharpSession -?
#-------------------------------------------------
function Remove-SharpSession {
<#
.Synopsis
Used to close an active SSH or SCP Session.
.Description
Uses SharpSSH to close an active SSH or SCP Session.
#>
try {
if($SharpSession){
if ($SharpSession.GetType().Fullname -eq "Tamir.SharpSsh.SshShell"){
$SharpSession.WriteLine("exit")
sleep -milli 500
if($SharpSession.ShellOpened) {
Write-Warning "Shell didn't exit cleanly, closing anyway."
}
$SharpSession.Close()
}
else{
$SharpSession.Close()
}
}
else{
throw "There is no session to remove!"
}
}
catch {
throw $_
}
finally {
$Global:SharpSession = $Null
}
}
##################################################
# Main
##################################################
Export-ModuleMember New-SharpSession
Export-ModuleMember Transfer-SharpFile
Export-ModuleMember Remove-SharpSession