@@ -136,7 +136,13 @@ function New-RSAKeyPair {
136136 $Path
137137 }
138138 if (-not $Force -and (Test-Path $newPath )) {
139- throw " Key already exists at desired path: $newPath . Use -Force to overwrite the existing key or choose a different path"
139+ Write-Error " Key already exists at desired path: $newPath . Use -Force to overwrite the existing key or choose a different path"
140+ return
141+ }
142+ $parent = Split-Path $newPath - Parent
143+ if (-not (Test-Path $parent )) {
144+ Write-Host " Creating missing parent folder: $parent "
145+ New-Item - ItemType Directory $parent - Force | Out-Null
140146 }
141147 }
142148 $Length = if ($choice = Read-Host - Prompt " Enter desired key bit length (Default: 4096)" ) {
@@ -149,15 +155,17 @@ function New-RSAKeyPair {
149155 if (-not ([System.String ]::IsNullOrEmpty((Unprotect-SecureString - SecureString $Password )))) {
150156 $confirmed = Read-Host - AsSecureString - Prompt " Enter the same passphrase to confirm"
151157 if ((Unprotect-SecureString - SecureString $confirmed ) -ne (Unprotect-SecureString - SecureString $Password )) {
152- Write-Warning " Passphrases provided do not match! Exiting"
153- throw
158+ Write-Error " Passphrases provided do not match! Exiting"
159+ return
154160 }
161+ Write-Host " Generating passphrase protected key pair"
155162 $keys = [SCRTHQ.PEMEncrypt.RSA ]::Generate(
156163 $Length ,
157164 (Unprotect-SecureString - SecureString $Password )
158165 )
159166 }
160167 else {
168+ Write-Host " Generating key pair"
161169 $keys = [SCRTHQ.PEMEncrypt.RSA ]::Generate(
162170 $Length
163171 )
@@ -181,22 +189,37 @@ function New-RSAKeyPair {
181189 }
182190 }
183191 else {
184- $keys = if ($PSBoundParameters.ContainsKey (' Password' )) {
185- [SCRTHQ.PEMEncrypt.RSA ]::Generate(
186- $Length ,
187- $ (if ($Password -is [SecureString ]){(Unprotect-SecureString - SecureString $Password )}else {" $Password " })
188- )
192+ if (-not $NoFile -and -not $Force -and (Test-Path $Path )) {
193+ Write-Error " Key already exists at desired path: $Path . Use -Force to overwrite the existing key or choose a different path."
194+ return
189195 }
190196 else {
191- [SCRTHQ.PEMEncrypt.RSA ]::Generate(
192- $Length
193- )
194- }
195- if (-not $NoFile ) {
196- if (-not $Force -and (Test-Path $Path )) {
197- throw " Key already exists at desired path: $Path . Use -Force to overwrite the existing key or choose a different path."
197+ $parent = Split-Path $Path - Parent
198+ if (-not (Test-Path $parent )) {
199+ Write-Host " Creating missing parent folder: $parent "
200+ New-Item - ItemType Directory $parent - Force | Out-Null
201+ }
202+ $keys = if ($PSBoundParameters.ContainsKey (' Password' )) {
203+ Write-Host " Generating passphrase protected key pair"
204+ [SCRTHQ.PEMEncrypt.RSA ]::Generate(
205+ $Length ,
206+ $ (
207+ if ($Password -is [SecureString ]) {
208+ (Unprotect-SecureString - SecureString $Password )
209+ }
210+ else {
211+ " $Password "
212+ }
213+ )
214+ )
198215 }
199216 else {
217+ Write-Host " Generating key pair"
218+ [SCRTHQ.PEMEncrypt.RSA ]::Generate(
219+ $Length
220+ )
221+ }
222+ if (-not $NoFile ) {
200223 Write-Host " Saving private key to path : $Path "
201224 $keys.PrivatePEM | Set-Content - Path $Path - Force
202225 if (-not $NoSSH ) {
@@ -210,9 +233,9 @@ function New-RSAKeyPair {
210233 $keys.PublicPEM | Set-Content - Path $pemPath - Force
211234 }
212235 }
213- }
214- if ( $PassThru -or $NoFile ) {
215- $keys
236+ if ( $PassThru -or $NoFile ) {
237+ $keys
238+ }
216239 }
217240 }
218241 }
0 commit comments