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