PoshCode Archive  Artifact [b8d9322f5a]

Artifact b8d9322f5a9340f4e84c866fb39193b88e2a7fc59e8ad88786a53aefd18c57f1:

  • File Password-Functions.ps1 — part of check-in [8f3bfe20ab] at 2018-06-10 13:59:21 on branch trunk — New-Passwordfile (user: redyey size: 1307)

# encoding: ascii
# api: powershell
# title: Password Functions
# description: New-Passwordfile
# version: 0.1
# type: function
# author: redyey
# license: CC0
# function: New-PasswordFile
# x-poshcode-id: 5691
# x-archived: 2015-01-31T21:25:27
# x-published: 2015-01-14T17:29:00
#
# Get-PasswordFromEncryptedFile
#
function New-PasswordFile
{
	param (
		[parameter(Mandatory = $True)]
		[String]$PasswordFile
	)
	
	Read-Host -AsSecureString "Enter a password" | ConvertFrom-SecureString | Out-File $PasswordFile -ErrorAction Stop
}

function Get-PasswordFromEncryptedFile
{
	param (
		[parameter(Mandatory = $True)]
		[string]$PasswordFile
	)
	
	if (!(Test-Path $PasswordFile))
	{
		throw "Nonexistent Password file"
	}
	
	else
	{
		$EncryptedPass = Get-Content $PasswordFile | ConvertTo-SecureString
		$WncryptedStr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($EncryptedPass)
		[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($WncryptedStr) # Return the unencrypted string
	}
	
}

#Check if password exist else create new passwordfile
if (!(Test-Path C:\password.txt))
{
	New-PasswordFile -PasswordFile "C:\password.txt"
}

#Put password into variable
$Pass = Get-PasswordFromEncryptedFile -PasswordFile "C:\password.txt"