gisputility.psm1

function get-gispadminprivileges {
    <#
        .SYNOPSIS
        Get admin privileges
 
        .DESCRIPTION
        This Function checks if admin privelges are commited.
        If not, it tries to get admin privilges on its own.
 
        ------------------------------------------------------------------------------
        _____/\\\\\\\\\\\\__/\\\\\\\\\\\_____/\\\\\\\\\\\____/\\\\\\\\\\\\\___________
        ____/\\\//////////__\/////\\\///____/\\\/////////\\\_\/\\\/////////\\\________
        ____/\\\_________________\/\\\______\//\\\______\///__\/\\\_______\/\\\_______
        ____\/\\\____/\\\\\\\_____\/\\\_______\////\\\_________\/\\\\\\\\\\\\\/_______
        _____\/\\\___\/////\\\_____\/\\\__________\////\\\______\/\\\/////////________
        ______\/\\\_______\/\\\_____\/\\\_____________\////\\\___\/\\\________________
        _______\/\\\_______\/\\\_____\/\\\______/\\\______\//\\\__\/\\\_______________
        ________\//\\\\\\\\\\\\/___/\\\\\\\\\\\_\///\\\\\\\\\\\/___\/\\\______________
        __________\////////////____\///////////____\///////////_____\///______________
        ------------------------------------------------------------------------------
        date: 16.8.2022
        ------------------------------------------------------------------------------
        .EXAMPLE
        get-gispadminprivileges
 
        .LINK
        https://github.com/gisp497/gisputility
    #>

    [CmdletBinding()]
    param (
    )
    Begin {
        Write-Verbose "Get current user context"
        $CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    }
    Process {
        Write-Verbose "Check user is running the script is member of Administrator Group"
        if($CurrentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
        {
            Write-Verbose "Admin privileges are already commited."
        }else{
            try {
                  [string]$arguments = '-file ' + $PSCmdlet.MyInvocation.PSCommandPath
                  Start-Process powershell -Verb runAs -ArgumentList $arguments -ErrorAction Stop
                  Break
            }
            catch {
                Throw "Can't open powershell with admin privileges! Error: $_"
            }
        }
    }
    End {
    }
}
function install-gispmodule {
    <#
        .SYNOPSIS
        This function imports the module. If the module isen't installed, it installs the module and imports it then.
 
        .DESCRIPTION
        This function imports the module. If the module isen't installed, it installs the module and imports it then.
        In order to install the module you need to know if the module is installed by module or windows feature.
 
        ------------------------------------------------------------------------------
        _____/\\\\\\\\\\\\__/\\\\\\\\\\\_____/\\\\\\\\\\\____/\\\\\\\\\\\\\___________
        ____/\\\//////////__\/////\\\///____/\\\/////////\\\_\/\\\/////////\\\________
        ____/\\\_________________\/\\\______\//\\\______\///__\/\\\_______\/\\\_______
        ____\/\\\____/\\\\\\\_____\/\\\_______\////\\\_________\/\\\\\\\\\\\\\/_______
        _____\/\\\___\/////\\\_____\/\\\__________\////\\\______\/\\\/////////________
        ______\/\\\_______\/\\\_____\/\\\_____________\////\\\___\/\\\________________
        _______\/\\\_______\/\\\_____\/\\\______/\\\______\//\\\__\/\\\_______________
        ________\//\\\\\\\\\\\\/___/\\\\\\\\\\\_\///\\\\\\\\\\\/___\/\\\______________
        __________\////////////____\///////////____\///////////_____\///______________
        ------------------------------------------------------------------------------
        date: 25.08.2022
        ------------------------------------------------------------------------------
        .PARAMETER module
        Name of the module to be installed.
 
        .PARAMETER installfeature
        Name of the windows feature to be installed.
 
        .INPUTS
        system.string[]
 
        .OUTPUTS
        none
 
        .EXAMPLE
        install-gispmodule activedirectory -installfeature "RSAT-AD-PowerShell"
 
        .LINK
        https://github.com/gisp497/gisputility
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Position=0,
            Mandatory = $true,
            ValueFromPipeline = $false,
            HelpMessage = "Name of the module to be installed.")]
        [string]$module,
        [Parameter(Position=1,
            Mandatory = $false,
            ValueFromPipeline = $false,
            HelpMessage = "Name of the windows feature to be installed.")]
        $installfeature
    )
    Begin {
        Write-Verbose "Get Admin Privileges"
        get-gispadminprivileges
    }
    Process {
        Write-Verbose "Check If module is already installed."
        if (Get-Module -ListAvailable $module) {
            Write-Verbose "Update Module"
            Update-Module -Name $module
        }else{
            try {
                Write-Verbose "Install module"
                if ($null -ne $installfeature) {
                    if (((Get-ComputerInfo).osname) -like "*server*") {
                        $null = Install-WindowsFeature -Name $installfeature -ErrorAction Stop
                    }else{
                        $null = Enable-WindowsOptionalFeature -Online -FeatureName $installfeature -ErrorAction Stop
                    }
                }else{
                    $null = Install-Module -Name $module -Force -ErrorAction Stop
                }
            }
            catch {
                Throw "Cant install $module module. Error: $_"
            }
        }
        try {
            Write-Verbose "Import Module"
            $null = Import-Module $module -ErrorAction Stop    
        }
        catch {
            Throw "Cant import $module module. Error: $_"
        }
    }
    End {
    }
}
function get-gisprandomcharacter {
    <#
        .SYNOPSIS
        Creates random strings
 
        .DESCRIPTION
        This functions creates random strings. You can choose characters and the length.
 
        ------------------------------------------------------------------------------
        _____/\\\\\\\\\\\\__/\\\\\\\\\\\_____/\\\\\\\\\\\____/\\\\\\\\\\\\\___________
        ____/\\\//////////__\/////\\\///____/\\\/////////\\\_\/\\\/////////\\\________
        ____/\\\_________________\/\\\______\//\\\______\///__\/\\\_______\/\\\_______
        ____\/\\\____/\\\\\\\_____\/\\\_______\////\\\_________\/\\\\\\\\\\\\\/_______
        _____\/\\\___\/////\\\_____\/\\\__________\////\\\______\/\\\/////////________
        ______\/\\\_______\/\\\_____\/\\\_____________\////\\\___\/\\\________________
        _______\/\\\_______\/\\\_____\/\\\______/\\\______\//\\\__\/\\\_______________
        ________\//\\\\\\\\\\\\/___/\\\\\\\\\\\_\///\\\\\\\\\\\/___\/\\\______________
        __________\////////////____\///////////____\///////////_____\///______________
        ------------------------------------------------------------------------------
        date: 31.08.2022
        ------------------------------------------------------------------------------
        .PARAMETER length
        Determined the length of the string.
 
        .PARAMETER character
        Determined possible characters of the string.
 
        .INPUTS
        system.int[]
        system.string[]
 
        .OUTPUTS
        system.string[]
 
        .EXAMPLE
        get-gisprandomcharacter -length 12 -character "abcsdfjlk2344"
 
        .LINK
        https://github.com/gisp497/gisputility
    #>

    [CmdletBinding()]
    param (
        [Parameter(Position=0,
            Mandatory = $false,
            ValueFromPipeline = $false,
            HelpMessage = "help message")]
        [int]$length = 24,
        [Parameter(Position=1,
        Mandatory = $false,
        ValueFromPipeline = $false,
        HelpMessage = "help message")]
        [string]$character = 'ABCDEFGHKLMNOPRSTUVWXYZabcdefghiklmnoprstuvwxyz1234567890!"$%&/()=?@#*+'
    )
    Begin {
    }
    Process {
        $random = 1..$length | ForEach-Object { Get-Random -Maximum $character.length }
        $private:ofs=""
    }
    End {
        return [String]$character[$random]
    }
}