Protect-CredentialsToFile.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
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

<#PSScriptInfo
 
.VERSION 1.0.1
 
.GUID 241294df-0751-4d54-aff4-bc582a3988a5
 
.AUTHOR Jordan CHERKI
 
.COMPANYNAME
 
.DESCRIPTION This script allows to store the passwords of secure way in a file and then use them in a script without having to bring in them manually; First time that you use the script you have to enter the password (clearly). After you will specifie only the username and the filepath
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
 .SYNOPSIS
     Allow to store the passwords of secure way in a file and then use them in a script without having to bring in them manually.
     The script returns a PSCredential object.
   
  .DESCRIPTION
     This script allows to store the passwords of secure way in a file and then use them in a script without having to bring in them manually.
     First time that you use the script you have to enter the password (clearly). After you will specifie only the username and the filepath.
   
     First time :
         --> Use the script with the parameter Username, Password and FileName to create the file.
   
     Other Time
         --> Use the script with the parameter Username and FileName only.
   
     If you want replace the password of a file use the parameter -Force.
   
  .NOTES
     File Name : Protect-CredentialsToFile.ps1
     Author : Jordan CHERKI, cherkijordan@yahoo.fr
     Date : 2018/23/04
     Version : 1.0.1
   
  .EXAMPLE
     # First time
     $cred = Protect-CredentialsToFile -Username "Admin33" -Password "P@ssw0rd" -FileName "C:\temp\admin33.txt"
   
     # Other Time
     $cred = Protect-CredentialsToFile -Username "Admin33" -FileName "C:\temp\admin33.txt"
   
     # Other Time (replacement password)
     $cred = Protect-CredentialsToPath -Username "Admin33" -Password "N3w-P@ssw0rd" -FileName "C:\temp\admin33.txt" -Force
   
   
  .PARAMETER
     Username : Username of account. (Mandatory)
     Password : Password of account. (Optional)
     FileName : FileName where will be stored the password of secure way. (Mandatory)
     Force : Allow to replace the password of existing file. (Optional)
 
#>


    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][string]$Username,
        [Parameter(Mandatory=$false)][ValidateLength(4,60)][string]$Password,
        [Parameter(Mandatory=$true)][string]$FileName,
        [Parameter(Mandatory=$false)][switch]$Force
    )

    # Function Log2Screen
    Function Log2Screen
    {
        Param
        (
            [Parameter(Mandatory=$true)][string]$Message,
            [ValidateSet("Cyan","Green","Red")][string]$Color = "Cyan"
        )
        $date = Get-Date -Format "dd/MM/yyyy HH:mm:ss"

        Write-Host "$date - $Message`r" -ForegroundColor $Color
    }
    # -------------------------------


    If(Test-Path $FileName)
    {
        If($Password)
        {
            If ($Force)
            {
                Log2Screen "Deleting existing file" -Color Cyan
                Remove-Item $FileName -Force | Out-Null

                Log2Screen -Message "Creating file $FileName" -Color Cyan
                ConvertTo-SecureString -String $Password -AsPlainText -Force | ConvertFrom-SecureString | Out-File $FileName -Force

                Log2Screen -Message "Creating password file to : $FileName" -Color Cyan
                $Pass = Get-Content $FileName | ConvertTo-SecureString
                New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Pass
                
            } # End If Force
            Else
            {
                Log2Screen -Message "Impossible to create the file '$FileName' with this password, because the file exists already.`r Please use the parameter -Force to replace the password." -Color Red
                break
            }
        } # End If $Password

        $Pass = Get-Content $FileName | ConvertTo-SecureString
        New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Pass
        
    } # End If Test-Path
    Else
    {
        If (($Password -eq $null) -or ($Password -eq ""))
        {
            Log2Screen -Message "Error : Please, enter a valid password !" -Color Red
            break
        }
        Else
        {
            Log2Screen -Message "Creating file $FileName" -Color Cyan
            ConvertTo-SecureString -String $Password -AsPlainText -Force | ConvertFrom-SecureString | Out-File $FileName -Force

            Log2Screen -Message "Creating password file to : $FileName" -Color Cyan
            $Pass = Get-Content $FileName | ConvertTo-SecureString
            New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Pass
            
        }
    } # End Else Test-Path