#Developed by Rob Zylowski, Sr. Solutions Architect, Unidesk Corporation
#Ver 3.05 12-19-2016

#This script is used to save credentials for Email in the Unidesk 2.0 backup utility email account in encrypted format.  

Param ( [Parameter(Mandatory=$true)] 
		[String] $password)

function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

Function PW_Special($MyPW)
{
	$MyPW = $MyPW -replace "!cha!34", '"'
	$MyPW = $MyPW -replace "!cha!96", '`'
	$MyPW = $MyPW -replace "!cha!38", '&'
	$MyPW = $MyPW -replace "!cha!39", "`'"
	$MyPW = $MyPW -replace "!cha!92", '\'
	$MyPW = $MyPW -replace "!cha!47", '/'
	$MyPW = $MyPW -replace "!cha!124", '|'
	$MyPW = $MyPW -replace "!cha!60", '<'
	$MyPW = $MyPW -replace "!cha!62", '>'
	$MyPW = $MyPW -replace "!cha!94", '^'
	$MyPW = $MyPW -replace "!cha!33", '!'
	$MyPW = $MyPW -replace "!cha!40", '('
	$MyPW = $MyPW -replace "!cha!41", ')'
	$MyPW = $MyPW -replace "!cha!58", ':'
	$MyPW = $MyPW -replace "!cha!59", ';'
	$MyPW = $MyPW -replace "!cha!44", ','
	$MyPW = $MyPW -replace "!cha!64", '@'
	
	return $MyPW
}

#Main Script ----------------------------------------------------------------------------
$ScriptSource = Get-ScriptDirectory

$ScriptSource = Get-ScriptDirectory

$CredFile = "$ScriptSource\..\config\credemail.txt"
#$MyPassword = PW_Special $password
$MyPassword = [System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($Password))

$secpasswd = ConvertTo-SecureString $MyPassword -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("mailman", $secpasswd)
$mycreds.Password | ConvertFrom-SecureString | Set-Content $CredFile

If ($error.Count -eq 0) {
	Write-Host "Credentials Saved"
}


	
		

	