PSGISP.Tools.psm1

#---------------------------------------------
#------------------ Classes ------------------
#---------------------------------------------

#---------------------------------------------
#------------- Private Functions -------------
#---------------------------------------------

#---------------------------------------------
#-------------- Public Functions -------------
#---------------------------------------------
Function Get-Path {
    <#
        .SYNOPSIS
        GUI to choose path
 
        .DESCRIPTION
        This function opens a window to choose a specific path
 
        .PARAMETER RootFolder
        Folder in which the window will open
 
        .PARAMETER Description
        Window title
 
        .INPUTS
        You can pipe the value of the parameter (path) to this function / None
 
        .OUTPUTS
        System.Object[] / None
 
        .EXAMPLE
        Get-Path
 
        .LINK
        https://github.com/gisp497/psgisp
    #>

    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $false,
            HelpMessage = "help message"
        )]
        [string]$RootFolder = [Environment]::GetFolderPath('Desktop'),
        [Parameter(
            Mandatory = $false,
            HelpMessage = "help message"
        )]
        [string]$Description = 'Select Folder or File'
    )
    Begin {
        #load assembly
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    }
    Process {
        #create object
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        
        #set root folder
        $OpenFileDialog.initialDirectory = $RootFolder
        
        #remove filter
        $OpenFileDialog.filter = "All files (*.*)| *.*"
        
        #set windows title
        $OpenFileDialog.Title = $Description
        
        #show window
        $OpenFileDialog.ShowDialog() | Out-Null
    }
    End {
        #output value
        return $OpenFileDialog.filename
    }
}
Function Get-RandomString {
    <#
        .SYNOPSIS
        Creates random strings
 
        .DESCRIPTION
        This functions creates random strings. It is possible to generate Secure Strings.
 
        .PARAMETER Length
        Determined the length of the string.
 
         
        .PARAMETER Securestring
        Can be used, to generate securestrings.
 
        .INPUTS
        System.Int[]
        System.Switch[]
 
        .OUTPUTS
        System.String[]
        System.Securestring[]
 
        .EXAMPLE
        Get-RandomString -Length 23 -SecureString
 
        .LINK
        https://github.com/gisp497/psgisp
    #>

    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $false,
            HelpMessage = "length of the password")]
        [int]$Length = 24,
        [Parameter(
            Mandatory = $false,
            HelpMessage = "number of special characters in password")]
        [int]$SpecialCharacter = 8,
        [Parameter(
            Mandatory = $false,
            HelpMessage = "Creates Output as secure string")]
        [switch]$SecureString = $false
    )
    Begin {
        #check if password lenght is bigger than special character number
        if ($SpecialCharacter -ge $Length){
            Throw "Can't use more special characters than password length."
        }

        #calculate password length
        $NormalCharacter = $Length - $SpecialCharacter

        #set of possible characters
        $Normal = Get-Random -Count $NormalCharacter -InputObject ([Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
        if($SpecialCharacter -gt 0){
            $Special = Get-Random -Count $SpecialCharacter -InputObject ([Char[]]'!#$%&*+-/=?@_')
            $StringSet = $Normal + $Special
        }else{
            $StringSet = $Normal
        }
    }
    Process {
        #Check if Securestring is wanted
        if ($SecureString) {
            $outputobject = (Get-Random -Count $Length -InputObject $StringSet) -join '' | ConvertTo-SecureString -AsPlainText -Force
        }else{
            [string]$outputobject = (Get-Random -Count $Length -InputObject $StringSet) -join ''
        }
        
    }
    End {
        #return value
        return $outputobject
    }
}
Function New-SecurePersistentPassword {
    <#
        .SYNOPSIS
        Creates Secure Password and stores them in external File
 
        .DESCRIPTION
        This functions creates Secure Passwords and exports the credetials into a specific path.
        Only the same user on the same machine is able to encrypt the Password, without entering a password.
 
        .PARAMETER Path
        Path to safe encrypted Password File
 
        .PARAMETER Password
        Insert Password if you're not trying to get a generated one
 
        .PARAMETER GeneratedPassword
        Determined if the password should be generated.
 
        .PARAMETER PasswordLength
        If password is generated choose length.
 
        .PARAMETER PasswordComplexity
        Choose the Complexity of the Password (Low, Middle, Strong)
 
        .INPUTS
        System.Int[]
        System.Switch[]
        System.String
 
        .OUTPUTS
        none
 
        .EXAMPLE
        New-SecurePersistentPassword -Path "C:\temp" -Password "Password"
 
        .LINK
        https://github.com/gisp497/psgisp
    #>

    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $true,
            HelpMessage = "Path to safe encrypted Password File")]
        [String]$Path,
        [Parameter(
            Mandatory = $false,
            ParameterSetName = "InputPassword",
            HelpMessage = "Insert Password if you're not trying to get a generated one")]
        [String]$Password,
        [Parameter(
            Mandatory = $false,
            ParameterSetName = "GeneratePassword",
            HelpMessage = "Determined if the password should be generated")]
        [switch]$GeneratedPassword = $false,
        [Parameter(
            Mandatory = $false,
            ParameterSetName = "GeneratePassword",
            HelpMessage = "If password is generated choose length")]
        [int]$PasswordLength = 24,
        [Parameter(
            Mandatory = $false,
            ParameterSetName = "GeneratePassword",
            HelpMessage = "Choose the Complexity of the Password (Low, Middle, Strong)")]
        [ValidateSet('Low', 'Middle', 'Strong')]
        [String]$PasswordComplexity ='Strong'
    )
    Begin {
    }
    Process {
        # Check If Password should be generated or not
        if($GeneratedPassword -eq $true){
           # Check which password complexity is wanted
           switch ($PasswordComplexity){
            'Low' {
                $SpecialCharacter = 0
            }
            'Middle' {
                $SpecialCharacter = [math]::Round($PasswordLength / 5)
            }
            'Strong' {
                $SpecialCharacter = [math]::Round($PasswordLength / 3)
            }
           }
           
           #Create Secure Credentials
           $SecureCredential = Get-RandomString -Length $PasswordLength -SpecialCharacter $SpecialCharacter | Convertto-SecureString -AsPlainText
        }elseif($Password -ne $null){
            #Create Secure Credentials
            $SecureCredential = $Password | Convertto-SecureString -AsPlainText
        }else{
            Throw "Either Parameter Password or GeneratedPassword has to be used"
        }
    }
    End {
        $SecureCredential | ConvertFrom-SecureString | Out-File -Path $Path
    }
}
Function Import-SecurePersistentPassword {
    <#
        .SYNOPSIS
        Creates Secure Password and stores them in external File
 
        .DESCRIPTION
        This functions creates Secure Passwords and exports the credetials into a specific path.
        Only the same user on the same machine is able to encrypt the Password, without entering a password.
 
        .PARAMETER Path
        Path to safe encrypted Password File
 
        .PARAMETER Password
        Insert Password if you're not trying to get a generated one
 
        .INPUTS
        System.String
 
        .OUTPUTS
        System.String
 
        .EXAMPLE
        New-SecurePersistentPassword -Path "C:\temp" -Password "Password"
 
        .LINK
        https://github.com/gisp497/psgisp
    #>

    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $true,
            HelpMessage = "Path to safe encrypted Password File")]
        [String]$Path
    )
    Begin {
    }
    Process {
        $SecureCredential = Get-Content -Path $Path | ConvertTo-SecureString
    }
    End {
        Return ($SecureCredential | ConvertFrom-SecureString -AsPlainText)
    }
}

#---------------------------------------------
#--------- Export Public Functions -----------
#---------------------------------------------
$PublicFunctions = @(
    'Get-Path',
    'Get-RandomString',
    'New-SecurePersistentPassword',
    'Import-SecurePersistentPassword'
)
Export-ModuleMember -Function $PublicFunctions