Skip to content

Commit eb055c3

Browse files
committed
Added persist argument to allow user provided arguments to be persisted to .pscp
1 parent dfc27de commit eb055c3

File tree

1 file changed

+58
-16
lines changed

1 file changed

+58
-16
lines changed

Copy-AWS.psm1

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ function Copy-AWS
2626
[string]$Address,
2727

2828
[Parameter(Mandatory=$False)]
29-
[switch]$Recurse
29+
[switch]$Recurse,
30+
31+
[Parameter(Mandatory=$False)]
32+
[switch]$Persist,
33+
34+
[Parameter(Mandatory=$False)]
35+
[switch]$WhatIf
3036

3137
)
3238

@@ -46,32 +52,33 @@ function Copy-AWS
4652
$KeyFile=(Resolve-Path $KeyFile).Path
4753
}
4854

49-
$KeyFile= if($KeyFile){"-i `"$KeyFile`""}
5055

5156
$Username=if(!$Username){$info.username}else{$Username}
5257

5358
$Password=if(!$Password){$info.password}else{$Password}
54-
$Password=if($Password){"-pw $($Password)"}
5559

5660
$Address=if(!$Address){$info.address}else{$Address}
57-
58-
5961
}
6062

63+
$KeyFile= if($KeyFile){"-i `"$KeyFile`""}
64+
$Password=if($Password){"-pw $($Password)"}
65+
66+
6167
Write-Verbose "pscp: $pscp"
6268
Write-Verbose "root: $root"
63-
Write-Verbose "keyfile: $keyfile"
6469
Write-Verbose "username: $Username"
65-
Write-Verbose "password: $Password"
6670
Write-Verbose "address: $Address"
67-
71+
Write-Verbose "keyfile: $KeyFile"
72+
Write-Verbose "password: $Password"
73+
74+
$CanRun=$source -and $destination -and $Username -and $Address
6875
if($Recurse){$r="-r"}
6976
}
7077

7178
process {
72-
if($info)
79+
if($CanRun)
7380
{
74-
foreach($path in $source)
81+
foreach($path in $Source)
7582
{
7683
$name=$path -split "[\\/]"| Select-Object -Last 1
7784

@@ -85,16 +92,51 @@ function Copy-AWS
8592
Write-Verbose "local destination"
8693
$command="& `"$pscp`" $r $keyfile $password $username@$($address):$path $destination"
8794
}
88-
89-
Write-Verbose $command
90-
Invoke-Expression $command
95+
96+
if($WhatIf)
97+
{
98+
Write-Host "Executes: $command"
99+
}
100+
else
101+
{
102+
Write-Verbose $command
103+
Invoke-Expression $command
104+
}
91105
}
92106
}
107+
else
108+
{
109+
Write-Host "Missing Variable(s):"
110+
Write-Host "username: $Username"
111+
Write-Host "address: $Address"
112+
}
93113

94114
}
95115

96116
end{
97-
117+
if($CanRun -and $Persist)
118+
{
119+
#implement logic to write out variables to .pscp file
120+
New-Item -Path .pscp -ItemType dir -Force
121+
122+
if($Password){$Password=$Password.Replace("-pw ","")}
123+
124+
if($KeyFile)
125+
{
126+
$KeyFile=$KeyFile.Replace("-i ","")
127+
Copy-Item -Path $KeyFile -Destination .pscp
128+
$KeyFile=(Get-ChildItem $KeyFile).Name
129+
}
130+
131+
$file=@"
132+
username=$Username
133+
address=$Address
134+
keyfile=$KeyFile
135+
password=$Password
136+
"@
137+
138+
New-Item -Path .pscp\pscp_info.txt -ItemType file -Value $file -Force
139+
}
98140
}
99141

100142
<#
@@ -164,8 +206,8 @@ function Get-Root
164206
$driveRoot=[System.IO.Directory]::GetDirectoryRoot($CurrentPath)
165207
if($CurrentPath -eq $driveRoot)
166208
{
167-
Write-Host "Path: $CurrentPath is the drive root: $driveRoot"
168-
Write-Host "root path $FolderName not found."
209+
Write-Verbose "Path: $CurrentPath is the drive root: $driveRoot"
210+
Write-Verbose "root path $FolderName not found."
169211
return
170212
}
171213

0 commit comments

Comments
 (0)