Functions/Profile/Install-ProfileEnvironment.ps1

<#
    .SYNOPSIS
    Install the profile environment for the current user.

    .DESCRIPTION
    Install the profile environment for the current user. It will perform the
    following steps if not skipped:
    - Create the environment folder structure
    - Create a dedicated profile.ps1 script
    - Link the WindowsPowerShell folder to Dropbox
    - Store encrypted credentials
    - Store encrypted secure strings

    .INPUTS
    None.

    .OUTPUTS
    None.

    .EXAMPLE
    Install-ProfileEnvironment
    Install the environment inside the default path including all additional
    options: Link Dropbox, add credentials and secure strings.

    .EXAMPLE
    Install-ProfileEnvironment -SkipDropbox -SkipCredential -SkipSecureString
    Install the environment inside the default path including skipping all
    additional optoins.

    .NOTES
    Author : Claudio Spizzi
    License : MIT License

    .LINK
    https://github.com/claudiospizzi/Spizzi.Profile
#>


function Install-ProfileEnvironment
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $false)]
        [System.String]
        $Path = (Get-ProfileEnvironmentPath),

        [Parameter(Mandatory = $false)]
        [Switch]
        $SkipDropbox,

        [Parameter(Mandatory = $false)]
        [Switch]
        $SkipCredential,

        [Parameter(Mandatory = $false)]
        [Switch]
        $SkipSecureString
    )

    # By linking the current WindowsPowerShell folder to dropbox, all profile
    # files including the modules can be synchronized
    if (-not $SkipDropbox)
    {
        $DropboxPath = Get-Content "$ENV:LOCALAPPDATA\Dropbox\info.json" -ErrorAction Stop | ConvertFrom-Json | % 'personal' | % 'path'
        $DropboxPath = Join-Path -Path $DropboxPath -ChildPath 'PowerShell'

        if (-not (Test-Path -Path $DropboxPath))
        {
            throw "Dropbox path '$DropboxPath' not found!"
        }

        Set-ProfileFolderLink -Path $DropboxPath
    }

    # Create environment folder where all files will be stored
    if (-not (Test-Path -Path $Path))
    {
        New-Item -Path $Path -ItemType Directory -Force | Out-Null
    }

    # Create an empty custom profile script
    if (-not (Test-Path -Path "$Path\profile.ps1"))
    {
        New-Item -Path "$Path\profile.ps1" -ItemType File -Value "# Environment Profile Script`r`n" | Out-Null
    }

    # With this optoins, credentials can be added to the current PowerShell
    # environment, which are available as $CRED_<Name> variables later.
    if (-not $SkipCredential)
    {
        while ($true)
        {
            $Credential = Get-Credential -Message 'Enter your credentials. Cancel the dialog if you have entered all credentials.'

            if ($Credential -ne $null -and -not [String]::IsNullOrEmpty($Credential.UserName))
            {
                Export-ProfileEnvironmentCredential -Path $Path -Credential $Credential
            }
            else
            {
                break
            }
        }
    }

    # With this optoins, secure strings can be added to the current PowerShell
    # environment, which are available as $SEC_<Name> variables later.
    if (-not $SkipSecureString)
    {
        while ($true)
        {
            $SecureString = Get-Credential -Message 'Enter your secure string. Username will be the label. Cancel the dialog if you are finished.'

            if ($SecureString -ne $null -and -not [String]::IsNullOrEmpty($SecureString.UserName))
            {
                Export-ProfileEnvironmentSecureString -Path $Path -SecureString $SecureString
            }
            else
            {
                break
            }
        }
    }

    # Create profile file which stores the module version. It's used to verify
    # in the future, if the profile environment was created with the latest
    # Install-ProfileEnvironment function
    Get-Module -Name 'Spizzi.Profile' | Select-Object -ExpandProperty 'Version' | Export-Clixml "$Path\profile.xml"
}