PoshCode Archive  Hex Artifact Content

Artifact 9b1accd910612692d12a07dff073c32b0cab2d0eea583e3e1b103d684d93c53d:

  • File Save-Credentials.ps1 — part of check-in [c627d3e4c1] at 2018-06-10 13:26:25 on branch trunk — The script saves a username and password, encrypted with a custom key to to a file. The key is coded into the script but can be changed if required. The key allows the password to be decrypted by any user who has the key, on any machine. If the key parameter is omitted from ConvertFrom-SecureString, only the user who generated the file on the computer that generated the file can decrypt the password. (user: Samuel Mulhearn size: 3000)

0000: 23 20 65 6e 63 6f 64 69 6e 67 3a 20 61 73 63 69  # encoding: asci
0010: 69 0d 0a 23 20 61 70 69 3a 20 70 6f 77 65 72 73  i..# api: powers
0020: 68 65 6c 6c 0d 0a 23 20 74 69 74 6c 65 3a 20 53  hell..# title: S
0030: 61 76 65 2d 43 72 65 64 65 6e 74 69 61 6c 73 0d  ave-Credentials.
0040: 0a 23 20 64 65 73 63 72 69 70 74 69 6f 6e 3a 20  .# description: 
0050: 54 68 65 20 73 63 72 69 70 74 20 73 61 76 65 73  The script saves
0060: 20 61 20 75 73 65 72 6e 61 6d 65 20 61 6e 64 20   a username and 
0070: 70 61 73 73 77 6f 72 64 2c 20 65 6e 63 72 79 70  password, encryp
0080: 74 65 64 20 77 69 74 68 20 61 20 63 75 73 74 6f  ted with a custo
0090: 6d 20 6b 65 79 20 74 6f 20 74 6f 20 61 20 66 69  m key to to a fi
00a0: 6c 65 2e 20 54 68 65 20 6b 65 79 20 69 73 20 63  le. The key is c
00b0: 6f 64 65 64 20 69 6e 74 6f 20 74 68 65 20 73 63  oded into the sc
00c0: 72 69 70 74 20 62 75 74 20 63 61 6e 20 62 65 20  ript but can be 
00d0: 63 68 61 6e 67 65 64 20 69 66 20 72 65 71 75 69  changed if requi
00e0: 72 65 64 2e 20 54 68 65 20 6b 65 79 20 61 6c 6c  red. The key all
00f0: 6f 77 73 20 74 68 65 20 70 61 73 73 77 6f 72 64  ows the password
0100: 20 74 6f 20 62 65 20 64 65 63 72 79 70 74 65 64   to be decrypted
0110: 20 62 79 20 61 6e 79 20 75 73 65 72 20 77 68 6f   by any user who
0120: 20 68 61 73 20 74 68 65 20 6b 65 79 2c 20 6f 6e   has the key, on
0130: 20 61 6e 79 20 6d 61 63 68 69 6e 65 2e 20 49 66   any machine. If
0140: 20 74 68 65 20 6b 65 79 20 70 61 72 61 6d 65 74   the key paramet
0150: 65 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72  er is omitted fr
0160: 6f 6d 20 43 6f 6e 76 65 72 74 46 72 6f 6d 2d 53  om ConvertFrom-S
0170: 65 63 75 72 65 53 74 72 69 6e 67 2c 20 6f 6e 6c  ecureString, onl
0180: 79 20 74 68 65 20 75 73 65 72 20 77 68 6f 20 67  y the user who g
0190: 65 6e 65 72 61 74 65 64 20 74 68 65 20 66 69 6c  enerated the fil
01a0: 65 20 6f 6e 20 74 68 65 20 63 6f 6d 70 75 74 65  e on the compute
01b0: 72 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 64  r that generated
01c0: 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20 64 65   the file can de
01d0: 63 72 79 70 74 20 74 68 65 20 70 61 73 73 77 6f  crypt the passwo
01e0: 72 64 2e 0d 0a 23 20 76 65 72 73 69 6f 6e 3a 20  rd...# version: 
01f0: 31 2e 30 0d 0a 23 20 74 79 70 65 3a 20 73 63 72  1.0..# type: scr
0200: 69 70 74 0d 0a 23 20 61 75 74 68 6f 72 3a 20 53  ipt..# author: S
0210: 61 6d 75 65 6c 20 4d 75 6c 68 65 61 72 6e 0d 0a  amuel Mulhearn..
0220: 23 20 6c 69 63 65 6e 73 65 3a 20 43 43 30 0d 0a  # license: CC0..
0230: 23 20 78 2d 70 6f 73 68 63 6f 64 65 2d 69 64 3a  # x-poshcode-id:
0240: 20 33 34 38 36 0d 0a 23 20 78 2d 64 65 72 69 76   3486..# x-deriv
0250: 65 64 2d 66 72 6f 6d 2d 69 64 3a 20 33 34 38 37  ed-from-id: 3487
0260: 0d 0a 23 20 78 2d 61 72 63 68 69 76 65 64 3a 20  ..# x-archived: 
0270: 32 30 31 36 2d 30 35 2d 32 39 54 30 31 3a 32 34  2016-05-29T01:24
0280: 3a 34 32 0d 0a 23 20 78 2d 70 75 62 6c 69 73 68  :42..# x-publish
0290: 65 64 3a 20 32 30 31 33 2d 30 36 2d 32 39 54 30  ed: 2013-06-29T0
02a0: 35 3a 30 32 3a 30 30 0d 0a 23 0d 0a 23 20 73 65  5:02:00..#..# se
02b0: 65 20 68 74 74 70 3a 2f 2f 62 73 6f 6e 70 6f 73  e http://bsonpos
02c0: 68 2e 63 6f 6d 2f 61 72 63 68 69 76 65 73 2f 32  h.com/archives/2
02d0: 35 34 20 66 6f 72 20 6d 6f 72 65 20 69 6e 66 6f  54 for more info
02e0: 2e 0d 0a 23 0d 0a 3c 23 20 0d 0a 2e 53 59 4e 4f  ...#..<# ...SYNO
02f0: 50 53 49 53 20 0d 0a 20 20 20 20 54 68 65 20 73  PSIS ..    The s
0300: 63 72 69 70 74 20 73 61 76 65 73 20 61 20 75 73  cript saves a us
0310: 65 72 6e 61 6d 65 20 61 6e 64 20 70 61 73 73 77  ername and passw
0320: 6f 72 64 2c 20 65 6e 63 72 79 70 74 65 64 20 77  ord, encrypted w
0330: 69 74 68 20 61 20 63 75 73 74 6f 6d 20 6b 65 79  ith a custom key
0340: 20 74 6f 20 74 6f 20 61 20 66 69 6c 65 2e 0d 0a   to to a file...
0350: 2e 44 45 53 43 52 49 50 54 49 4f 4e 20 0d 0a 20  .DESCRIPTION .. 
0360: 20 20 20 54 68 65 20 73 63 72 69 70 74 20 73 61     The script sa
0370: 76 65 73 20 61 20 75 73 65 72 6e 61 6d 65 20 61  ves a username a
0380: 6e 64 20 70 61 73 73 77 6f 72 64 2c 20 65 6e 63  nd password, enc
0390: 72 79 70 74 65 64 20 77 69 74 68 20 61 20 63 75  rypted with a cu
03a0: 73 74 6f 6d 20 6b 65 79 20 74 6f 20 74 6f 20 61  stom key to to a
03b0: 20 66 69 6c 65 2e 20 54 68 65 20 6b 65 79 20 69   file. The key i
03c0: 73 20 63 6f 64 65 64 20 69 6e 74 6f 20 74 68 65  s coded into the
03d0: 20 73 63 72 69 70 74 20 62 75 74 20 73 68 6f 75   script but shou
03e0: 6c 64 20 62 65 20 63 68 61 6e 67 65 64 20 62 65  ld be changed be
03f0: 66 6f 72 65 20 75 73 65 2e 20 54 68 65 20 6b 65  fore use. The ke
0400: 79 20 61 6c 6c 6f 77 73 20 74 68 65 20 70 61 73  y allows the pas
0410: 73 77 6f 72 64 20 74 6f 20 62 65 20 64 65 63 72  sword to be decr
0420: 79 70 74 65 64 20 62 79 20 61 6e 79 20 75 73 65  ypted by any use
0430: 72 20 77 68 6f 20 68 61 73 20 74 68 65 20 6b 65  r who has the ke
0440: 79 2c 20 6f 6e 20 61 6e 79 20 6d 61 63 68 69 6e  y, on any machin
0450: 65 2e 20 69 66 20 74 68 65 20 6b 65 79 20 70 61  e. if the key pa
0460: 72 61 6d 65 74 65 72 20 69 73 20 6f 6d 69 74 74  rameter is omitt
0470: 65 64 20 66 72 6f 6d 20 43 6f 6e 76 65 72 74 46  ed from ConvertF
0480: 72 6f 6d 2d 53 65 63 75 72 65 53 74 72 69 6e 67  rom-SecureString
0490: 2c 20 6f 6e 6c 79 20 74 68 65 20 75 73 65 72 20  , only the user 
04a0: 77 68 6f 20 67 65 6e 65 72 61 74 65 64 20 74 68  who generated th
04b0: 65 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 63 6f  e file on the co
04c0: 6d 70 75 74 65 72 20 74 68 61 74 20 67 65 6e 65  mputer that gene
04d0: 72 61 74 65 64 20 74 68 65 20 66 69 6c 65 09 63  rated the file.c
04e0: 61 6e 20 64 65 63 72 79 70 74 20 74 68 65 20 70  an decrypt the p
04f0: 61 73 73 77 6f 72 64 2e 0d 0a 09 73 65 65 20 68  assword....see h
0500: 74 74 70 3a 2f 2f 62 73 6f 6e 70 6f 73 68 2e 63  ttp://bsonposh.c
0510: 6f 6d 2f 61 72 63 68 69 76 65 73 2f 32 35 34 20  om/archives/254 
0520: 66 6f 72 20 6d 6f 72 65 20 69 6e 66 6f 2e 0d 0a  for more info...
0530: 09 0d 0a 09 54 6f 20 72 65 74 72 69 65 76 65 20  ....To retrieve 
0540: 74 68 65 20 70 61 73 73 77 6f 72 64 3a 0d 0a 09  the password:...
0550: 09 24 6b 65 79 20 3d 20 5b 62 79 74 65 5d 35 37  .$key = [byte]57
0560: 2c 38 36 2c 35 39 2c 31 31 2c 37 32 2c 37 35 2c  ,86,59,11,72,75,
0570: 31 38 2c 35 32 2c 37 33 2c 34 36 2c 30 2c 32 31  18,52,73,46,0,21
0580: 2c 35 36 2c 37 36 2c 34 37 2c 31 32 0d 0a 09 09  ,56,76,47,12....
0590: 24 56 43 43 72 65 64 20 3d 20 49 6d 70 6f 72 74  $VCCred = Import
05a0: 2d 43 73 76 20 27 43 3a 5c 50 41 54 48 5c 46 49  -Csv 'C:\PATH\FI
05b0: 4c 45 2e 54 58 54 27 0d 0a 09 09 24 56 43 43 72  LE.TXT'....$VCCr
05c0: 65 64 2e 50 61 73 73 77 6f 72 64 20 3d 20 28 24  ed.Password = ($
05d0: 56 43 43 72 65 64 2e 50 61 73 73 77 6f 72 64 7c  VCCred.Password|
05e0: 20 43 6f 6e 76 65 72 74 54 6f 2d 53 65 63 75 72   ConvertTo-Secur
05f0: 65 53 74 72 69 6e 67 20 2d 4b 65 79 20 24 6b 65  eString -Key $ke
0600: 79 29 0d 0a 09 09 24 56 43 43 72 65 64 20 3d 20  y)....$VCCred = 
0610: 28 4e 65 77 2d 4f 62 6a 65 63 74 20 2d 74 79 70  (New-Object -typ
0620: 65 6e 61 6d 65 20 53 79 73 74 65 6d 2e 4d 61 6e  ename System.Man
0630: 61 67 65 6d 65 6e 74 2e 41 75 74 6f 6d 61 74 69  agement.Automati
0640: 6f 6e 2e 50 53 43 72 65 64 65 6e 74 69 61 6c 20  on.PSCredential 
0650: 2d 41 72 67 75 6d 65 6e 74 4c 69 73 74 20 24 56  -ArgumentList $V
0660: 43 43 72 65 64 2e 55 73 65 72 6e 61 6d 65 2c 24  CCred.Username,$
0670: 56 43 43 72 65 64 2e 50 61 73 73 77 6f 72 64 29  VCCred.Password)
0680: 0d 0a 2e 4e 4f 54 45 53 20 0d 0a 20 20 20 20 46  ...NOTES ..    F
0690: 69 6c 65 20 4e 61 6d 65 20 20 3a 20 53 61 76 65  ile Name  : Save
06a0: 43 72 65 64 65 6e 74 69 61 6c 73 2e 70 73 31 0d  Credentials.ps1.
06b0: 0a 20 20 20 20 41 75 74 68 6f 72 20 20 20 20 20  .    Author     
06c0: 3a 20 53 61 6d 75 65 6c 20 4d 75 6c 68 65 61 72  : Samuel Mulhear
06d0: 6e 0d 0a 20 20 20 20 56 65 72 73 69 6f 6e 20 48  n..    Version H
06e0: 69 73 74 6f 72 79 3a 20 0d 0a 09 56 65 72 73 69  istory: ...Versi
06f0: 6f 6e 20 31 2e 30 20 20 0d 0a 09 09 32 38 20 4a  on 1.0  ....28 J
0700: 75 6e 20 32 30 31 32 2e 0d 0a 09 09 52 65 6c 65  un 2012.....Rele
0710: 61 73 65 0d 0a 2e 4c 49 4e 4b 20 0d 0a 20 20 20  ase...LINK ..   
0720: 20 68 74 74 70 3a 2f 2f 70 6f 73 68 63 6f 64 65   http://poshcode
0730: 2e 6f 72 67 2f 33 34 38 35 0d 0a 2e 45 58 41 4d  .org/3485...EXAM
0740: 50 4c 45 20 0d 0a 20 20 20 20 43 61 6c 6c 20 74  PLE ..    Call t
0750: 68 65 20 73 63 72 69 70 74 20 77 69 74 68 20 2e  he script with .
0760: 5c 53 61 76 65 43 72 65 64 65 6e 74 69 61 6c 73  \SaveCredentials
0770: 2e 70 73 31 20 6e 6f 20 61 72 67 75 6d 65 6e 74  .ps1 no argument
0780: 73 20 6f 72 20 70 61 72 61 6d 65 74 65 72 73 20  s or parameters 
0790: 61 72 65 20 72 65 71 75 69 72 65 64 0d 0a 23 3e  are required..#>
07a0: 20 0d 0a 0d 0a 24 6b 65 79 20 3d 20 5b 62 79 74   ....$key = [byt
07b0: 65 5d 35 37 2c 38 36 2c 35 39 2c 31 31 2c 37 32  e]57,86,59,11,72
07c0: 2c 37 35 2c 31 38 2c 35 32 2c 37 33 2c 34 36 2c  ,75,18,52,73,46,
07d0: 30 2c 32 31 2c 35 36 2c 37 36 2c 34 37 2c 31 32  0,21,56,76,47,12
07e0: 0d 0a 57 72 69 74 65 2d 48 6f 73 74 20 22 4b 65  ..Write-Host "Ke
07f0: 79 20 6c 65 6e 67 74 68 20 69 73 3a 22 20 24 6b  y length is:" $k
0800: 65 79 2e 6c 65 6e 67 74 68 20 22 54 68 65 20 6b  ey.length "The k
0810: 65 79 20 6c 65 6e 67 74 68 20 69 73 20 61 63 63  ey length is acc
0820: 65 70 74 61 62 6c 65 20 69 66 20 31 36 20 6f 72  eptable if 16 or
0830: 20 33 32 22 0d 0a 57 72 69 74 65 2d 48 6f 73 74   32"..Write-Host
0840: 20 22 54 68 69 73 20 73 63 72 69 70 74 20 73 61   "This script sa
0850: 76 65 73 20 61 20 75 73 65 72 6e 61 6d 65 20 61  ves a username a
0860: 6e 64 20 70 61 73 73 77 6f 72 64 20 69 6e 74 6f  nd password into
0870: 20 61 20 66 69 6c 65 22 0d 0a 57 72 69 74 65 2d   a file"..Write-
0880: 48 6f 73 74 20 22 53 65 6c 65 63 74 20 61 6e 20  Host "Select an 
0890: 6f 75 74 70 75 74 20 66 69 6c 65 3a 22 0d 0a 5b  output file:"..[
08a0: 53 79 73 74 65 6d 2e 52 65 66 6c 65 63 74 69 6f  System.Reflectio
08b0: 6e 2e 41 73 73 65 6d 62 6c 79 5d 3a 3a 4c 6f 61  n.Assembly]::Loa
08c0: 64 57 69 74 68 50 61 72 74 69 61 6c 4e 61 6d 65  dWithPartialName
08d0: 28 22 53 79 73 74 65 6d 2e 77 69 6e 64 6f 77 73  ("System.windows
08e0: 2e 66 6f 72 6d 73 22 29 20 7c 4f 75 74 2d 4e 75  .forms") |Out-Nu
08f0: 6c 6c 0d 0a 24 53 61 76 65 46 69 6c 65 44 69 61  ll..$SaveFileDia
0900: 6c 6f 67 20 3d 20 4e 65 77 2d 4f 62 6a 65 63 74  log = New-Object
0910: 20 53 79 73 74 65 6d 2e 57 69 6e 64 6f 77 73 2e   System.Windows.
0920: 46 6f 72 6d 73 2e 53 61 76 65 46 69 6c 65 44 69  Forms.SaveFileDi
0930: 61 6c 6f 67 0d 0a 24 53 61 76 65 46 69 6c 65 44  alog..$SaveFileD
0940: 69 61 6c 6f 67 2e 69 6e 69 74 69 61 6c 44 69 72  ialog.initialDir
0950: 65 63 74 6f 72 79 20 3d 20 24 69 6e 69 74 69 61  ectory = $initia
0960: 6c 44 69 72 65 63 74 6f 72 79 0d 0a 24 53 61 76  lDirectory..$Sav
0970: 65 46 69 6c 65 44 69 61 6c 6f 67 2e 66 69 6c 74  eFileDialog.filt
0980: 65 72 20 3d 20 22 41 6c 6c 20 66 69 6c 65 73 20  er = "All files 
0990: 28 2a 2e 2a 29 7c 20 2a 2e 2a 22 0d 0a 24 53 61  (*.*)| *.*"..$Sa
09a0: 76 65 46 69 6c 65 44 69 61 6c 6f 67 2e 53 68 6f  veFileDialog.Sho
09b0: 77 44 69 61 6c 6f 67 28 29 20 7c 20 4f 75 74 2d  wDialog() | Out-
09c0: 4e 75 6c 6c 0d 0a 24 4f 75 74 46 69 6c 65 20 3d  Null..$OutFile =
09d0: 20 24 53 61 76 65 46 69 6c 65 44 69 61 6c 6f 67   $SaveFileDialog
09e0: 2e 66 69 6c 65 6e 61 6d 65 0d 0a 24 6e 75 6c 6c  .filename..$null
09f0: 20 7c 20 4f 75 74 2d 46 69 6c 65 20 2d 46 69 6c   | Out-File -Fil
0a00: 65 50 61 74 68 20 24 4f 75 74 66 69 6c 65 0d 0a  ePath $Outfile..
0a10: 24 63 72 65 64 65 6e 74 69 61 6c 20 3d 20 47 65  $credential = Ge
0a20: 74 2d 43 72 65 64 65 6e 74 69 61 6c 0d 0a 23 7c  t-Credential..#|
0a30: 20 43 6f 6e 76 65 72 74 46 72 6f 6d 2d 53 65 63   ConvertFrom-Sec
0a40: 75 72 65 53 74 72 69 6e 67 20 2d 4b 65 79 20 24  ureString -Key $
0a50: 6b 65 79 29 0d 0a 24 6f 62 6a 20 3d 20 4e 65 77  key)..$obj = New
0a60: 2d 4f 62 6a 65 63 74 20 2d 74 79 70 65 6e 61 6d  -Object -typenam
0a70: 65 20 53 79 73 74 65 6d 2e 4f 62 6a 65 63 74 0d  e System.Object.
0a80: 0a 24 6f 62 6a 20 7c 20 41 64 64 2d 4d 65 6d 62  .$obj | Add-Memb
0a90: 65 72 20 2d 4d 65 6d 62 65 72 54 79 70 65 20 6e  er -MemberType n
0aa0: 6f 74 65 50 72 6f 70 65 72 74 79 20 2d 6e 61 6d  oteProperty -nam
0ab0: 65 20 55 73 65 72 6e 61 6d 65 20 2d 76 61 6c 75  e Username -valu
0ac0: 65 20 24 63 72 65 64 65 6e 74 69 61 6c 2e 55 73  e $credential.Us
0ad0: 65 72 4e 61 6d 65 0d 0a 24 6f 62 6a 20 7c 20 41  erName..$obj | A
0ae0: 64 64 2d 4d 65 6d 62 65 72 20 2d 4d 65 6d 62 65  dd-Member -Membe
0af0: 72 54 79 70 65 20 6e 6f 74 65 50 72 6f 70 65 72  rType noteProper
0b00: 74 79 20 2d 6e 61 6d 65 20 50 61 73 73 77 6f 72  ty -name Passwor
0b10: 64 20 2d 76 61 6c 75 65 20 28 24 63 72 65 64 65  d -value ($crede
0b20: 6e 74 69 61 6c 2e 50 61 73 73 77 6f 72 64 20 7c  ntial.Password |
0b30: 20 43 6f 6e 76 65 72 74 46 72 6f 6d 2d 53 65 63   ConvertFrom-Sec
0b40: 75 72 65 53 74 72 69 6e 67 20 2d 6b 65 79 20 24  ureString -key $
0b50: 6b 65 79 29 0d 0a 24 6f 62 6a 20 7c 20 45 78 70  key)..$obj | Exp
0b60: 6f 72 74 2d 43 73 76 20 2d 50 61 74 68 20 24 4f  ort-Csv -Path $O
0b70: 75 74 46 69 6c 65 0d 0a 77 72 69 74 65 2d 68 6f  utFile..write-ho
0b80: 73 74 20 22 55 73 65 72 6e 61 6d 65 20 61 6e 64  st "Username and
0b90: 20 70 61 73 73 77 6f 72 64 20 68 61 76 65 20 62   password have b
0ba0: 65 65 6e 20 73 61 76 65 64 20 74 6f 20 24 6f 75  een saved to $ou
0bb0: 74 66 69 6c 65 22 0d 0a                          tfile"..