importfromURL.psm1

# --------------------------------------------------------------------------------------------------
# This function allows for a module or script to be imnported into the current PowerShell scope
# without storing it on the local computer
# Original script by Alec McCutcheon : https://github.com/AlecMcCutcheon/ImportFromURL
# --------------------------------------------------------------------------------------------------

# This imports a powershell module or script from a URL
function ImportFromURL {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory = $true,ValueFromPipeLine = $true)]
        [string]$URL,
        [Parameter(Mandatory = $false,ValueFromPipeLine = $true)]
        [pscredential]$Credential,
        [Parameter(Mandatory = $false)]
        [switch]$DLL,
        [Parameter(Mandatory = $false)]
        [switch]$Ps1,
        [Parameter(Mandatory = $false)]
        [switch]$Psm1
    )

    process {
        if (($URL -as [System.URI]).IsAbsoluteUri -eq $False) {
            Write-Verbose "URL is Missing http:// or https://"
            break
        }
        if ($URL -match ".DLL" -or $DLL -eq $true) {
            try{
                Import-Module ([System.Reflection.Assembly]::Load((Invoke-WebRequest -UseBasicParsing -Uri $URL).content)) -ErrorAction SilentlyContinue > $null
            }catch{
                Write-Verbose "Import-Module Failed to Import DLL, Make sure the Url is a direct link to the file."
            }
            break
        }
        if ($URL -match ".psm1" -or $Psm1 -eq $true) {
            try{
                # Get module name without extension
                $ModuleFileNameComponents = (Split-Path $URL -Leaf).Split(".")
                $ModuleName = (Split-Path $URL -Leaf).Replace(".$($ModuleFileNameComponents[$ModuleFileNameComponents.count-1])","")
                if($Credential) {
                    $ImportedCode = Invoke-WebRequest -Uri $URL -UseBasicParsing -Credential $Credential
                } else {
                    $ImportedCode = Invoke-WebRequest -Uri $URL -UseBasicParsing
                }
                New-Module -Name "$($ModuleName)" -ScriptBlock {$ImportedCode} -ErrorAction SilentlyContinue > $null
            }catch{
                Write-Verbose "Import-Module Failed to Import Psm1 Module, Make sure the Url is a direct link to the raw code"
            }
            break
        }
        if ($URL -match ".ps1" -or $Ps1 -eq $true) {
            try{
                if($Credential) {
                    $ImportedCode = Invoke-WebRequest -Uri $URL -UseBasicParsing -Credential $Credential
                } else {
                    $ImportedCode = Invoke-WebRequest -Uri $URL -UseBasicParsing
                }
                Invoke-Command -ScriptBlock { $ImportedCode }
                Write-Verbose "Attempting to invoke the Ps1 script (There is no error handling for .Ps1 scripts)"
                Write-Verbose "Windows Bug Tip: If a script prompts for UAC and then force closes the Session, Try running the session as admin"
            } catch {
                Write-Verbose "Importing script failed, Make sure the Url is a direct link to the raw code"
            }
            break
        }
        Write-Verbose "The URL does not conatin any of file extensions: .DLL, .Ps1, or Psm1, or is Invailed."
        Write-Verbose "Please add the -DLL, -Ps1 or -Psm1 parameter to let the function know what file type it is."
    }
}

# --------------------------------------------------------------------------------------------------