LARSINUS.psm1


function Add-Prefix{
    <#
    .SYNOPSIS
    Adds a line prefix
    .PARAMETER Color
    .PARAMETER InfoType
    .DESCRIPTION
    It will add a prefix that you can use in front of your Write-Host.
    The output is a square brackets with some text (of your choice) between in a color (of your choice)
    .EXAMPLE
    PS C:\Add-Prefix -Color Red -InfoType Error
    [ERROR]
    .EXAMPLE
    PS C:\Add-Prefix -Color Magenta -InfoType Information ; Write-Host 'Hello World!'
    [INFORMATION] Hello World!
    #>

    param (
        [Parameter()]
        [validateset('Black','DarkBlue','DarkGreen','DarkCyan','DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White')]
        $Color,
        [Parameter()]
        [string]$InfoType
    )
    Write-Host '[' -NoNewline
    Write-Host -ForegroundColor $color ($infoType).ToUpper() -NoNewline
    Write-Host '] ' -noNewline
}

function Add-Space () {
    <#
    .SYNOPSIS
    Adds spaces after previous text block based on that length.
    .PARAMETER TextLength
    .PARAMETER MaxSpace
    .DESCRIPTION
    Used to make sure your columns are aligned correctly when doing Write-Host and the previous text block had different length.
    This will calculate how much space should be added at the end of the previous text block.
    .EXAMPLE
    PS C:\Add-Space -TextLength $String.Length -MaxSpace 25
    #>

    param (
        [Parameter (Mandatory=$true)]
        [int]$TextLength,
        [Parameter (Mandatory=$True)]
        [int]$MaxSpace
    )
    $SpaceToAdd = ($MaxSpace - $TextLength)
    While ($SpaceToAdd -gt 0){
        $Space = $Space + ' '
        $SpaceToAdd--
    }
    Write-Host $Space -NoNewline
}

function Open-File {
    <#
    .SYNOPSIS
    Gets a file.
    .PARAMETER Path
    .DESCRIPTION
    Opens the Windows Explorer so you can browse to a file to be used in your script.
    Can be used together with other functions/cmdlets
    .EXAMPLE
    PS C:\Open-File
    Opens Windows Explorer in the current directory you're in.
    .EXAMPLE
    PS C:\Open-File -Path C:\Users\User1\OneDrive\MyScripts
    .EXAMPLE
    PS C:\$information = Get-Content (Open-File -Path "C:fso") | ConvertFrom-Json
    #>

    param (
        [parameter()]
        $Path = 'C:fso'
    )
    [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”)
    Out-Null
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $Path
    $OpenFileDialog.filter = “All files (*.*)| *.*”
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

function Save-File {
    <#
    .SYNOPSIS
    Saves a file.
    .PARAMETER Path
    .PARAMETER FileDialogFileType
    .DESCRIPTION
    Opens the Windows Explorer so you can browse to where you want to save the file.
    Use the parameter -FileDialogFileType to specify which file type you want the user to be able to save as.
    .EXAMPLE
    PS C:\Save-File -FileDialogFileType *.*
    Opens Windows Explorer in the current directory you're in.
    .EXAMPLE
    PS C:\Save-File -Path C:\Users\User1\OneDrive\Scripts\Logs -FileDialogFileType *.log
    #>

    param (
        [parameter()]
        $Path = 'C:fso',
        [Parameter(Mandatory = $true)]
        [String]$FileDialogFileType
    )
    [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”)
    Out-Null
    $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
    $SaveFileDialog.initialDirectory = $Path
    $SaveFileDialog.filter = “All files ($FileDialogFileType )| $FileDialogFileType ”
    $result = $SaveFileDialog.ShowDialog()
    if ($result -eq 'OK'){
        Out-File -FilePath $SaveFileDialog.filename -InputObject $arrCollections
    }
}

function Enable-Sideloading {
    <#
        .SYNOPSIS
        Enables Sidelaoding of apps
        .DESCRIPTION
        ---[ MUST BE RUN WITH ELEVATED PRIVILEGES ]---
        Enables sideloading of applications in Windows 10.
        Being used to allow installations of custom/LOB .msix packaged apps.
        Since they are not published to the Microsoft Store, we need to enable sideloading.
        They are secured by using certificates.
        .EXAMPLE
        PS C:\Enable-Sideloading.ps1
        #>


        $registryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
        $Name1 = "AllowAllTrustedApps"
        $value1 = "1"
        New-ItemProperty -Path $registryPath -Name $name1 -Value $value1 -PropertyType DWORD -Force

        $Name2 = "AllowDevelopmentWithoutDevLicense"
        $value2 = "0"

        New-ItemProperty -Path $registryPath -Name $name2 -Value $value2 -PropertyType DWORD -Force
    }

    function Add-LocalGroupMember {
        <#
            .SYNOPSIS
            Adds a user to a local group
 
            .DESCRIPTION
            The script accepts two parameters. A username and a local group.
            UserName must be in the format of either:
                1) AzureAD\UserName@domain.com
                2) Domain\UserName
            Then select which group the user should be added to.
 
            .PARAMETER UserName
            .PARAMETER LocalGroup
 
            .EXAMPLE
            PS C:\Add-LocalGroupMember -UserName Domain\UserName -LocalGroup RemoteDesktopUsers
            .EXAMPLE
            PS C:\Add-LocalGroupMember -UserName AzureAD\UserName@domain.com -LocalGroup LocalAdmin
            .EXAMPLE
            PS C:\Add-LocalGroupMember -LocalGroup RemoteDesktopUsers
            Adds the currently logged on user to the local group 'Remote Desktop Users'
        #>



        #region -----------------------[ FUNCTION INITIALIZATIONS ]---------------------------------------------------------------------

        [CmdletBinding()]
        param (
            [Parameter()]
            [String]$UserName,

            [Parameter(Mandatory=$true)]
            [ValidateSet('LocalAdmin','RemoteDesktopUsers')]
            [String]$LocalGroup
        )

        $ErrorActionPreference = 'Stop'

        #endregion ------------------[ End Function Initializations ]-------------------------------------------------------------------


        #region -----------------------[ FUNCTION FUNCTIONS ]---------------------------------------------------------------------------

        function Add-GroupMember {
            Write-Log -Type Information -Message "Verifying if $UserName already belong to the group"
            if ($LocalGroupMembers -contains $UserName -eq $false) {
                Write-Log -Type Information -Message "Adding the user $UserName to the group $LocalGroupName"
                Try {
                    net localgroup $LocalGroupName /add $UserName
                }
                Catch {
                    Write-Log -Type Error -Message "Access Denied. Requires elevated privileges."
                }
            }
        }

        function Write-Log {
            param (
                [Parameter(Mandatory=$true)]
                [String]$Message,
                [Parameter(Mandatory=$true)]
                [ValidateSet('Information','Warning','Error')]
                [String]$Type
            )
            Switch ($Type){
                'Information' {
                    Write-EventLog -LogName Application -Source "Add-LocalGroupMember" -EntryType Information -EventId 7 -Message $Message
                }
                'Warning' {
                    Write-EventLog -LogName Application -Source "Add-LocalGroupMember" -EntryType Warning -EventId 14 -Message $Message
                }
                'Error' {
                    Write-EventLog -LogName Application -Source "Add-LocalGroupMember" -EntryType Error -EventId 31 -Message $Message
                }
                Default {}
            }

        }

        #endregion ------------------[ End Function Functions ]------------------------------------------------------------------------


        #region -----------------------[ FUNCTION EXECUTION ]---------------------------------------------------------------------------

        if ($UserName -eq "") {
            $domain = (Get-WmiObject win32_loggedonuser | Select-Object -ExpandProperty Antecedent).split('=')[1]
            $user = (Get-WmiObject win32_loggedonuser | Select-Object -ExpandProperty Antecedent).split('=')[2]
            $UserName = "$($domain.split('"')[1])\$($user.Split('"')[1])"
        }

        Try {
            New-EventLog -LogName Application -Source "Add-LocalGroupMember"
        }
        Catch [System.InvalidOperationException] {
            Write-Log -Type Warning -Message "Event Log Source Already Exist"
        }

        switch ($LocalGroup) {
            'LocalAdmin' {
                $LocalGroupName = 'Administrators'
                Write-Log -Type Information -Message "Collecting members of the group $LocalGroupName"
                $LocalGroupMembers = (net localgroup (Get-WmiObject win32_Group | Where-Object SID -eq 'S-1-5-32-544').Name)
                Add-GroupMember
            }
            'RemoteDesktopUsers' {
                $LocalGroupName = 'Remote Desktop Users'
                Write-Log -Type Information -Message "Collecting members of the group $LocalGroupName"
                $LocalGroupMembers = (net localgroup (Get-WmiObject win32_Group | Where-Object SID -eq 'S-1-5-32-555').Name)
                Add-GroupMember
            }
            Default {}
        }

        #endregion ------------------[ End Function Execution ]--------------------------------------------------------------------------
    }

Export-ModuleMember -Function Add-Prefix
Export-ModuleMember -Function Add-Space
Export-ModuleMember -Function Open-File
Export-ModuleMember -Function Save-File
Export-ModuleMember -Function Enable-Sideloading
Export-ModuleMember -Function Add-LocalGroupMember