Ledningssystem.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.160
     Generated on: 2020-01-09 12:47
     Generated by: fft
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2019
#>



    <#
        ===========================================================================
         Created by: Firas Farzat
         Organization: Interlan
         Filename: Ledningssystem.psm1
        -------------------------------------------------------------------------
         Module Name: Ledningssystem
        ===========================================================================
    #>

    
    function Set-Connection
    {
        
        Param (
            [Parameter(Mandatory = $true,
                       HelpMessage = "Enter office Admin Email.")]
            [String]$UserName,
            [Parameter(Mandatory = $true,
                       HelpMessage = "Enter office Admin Password.")]
            [String]$Password,
            [Parameter(Mandatory = $False)]
            [ValidateSet("AzureAD", "SPOService", "PnPOnline", "PnPOnlineAdmin", "MsolService")]
            [String[]]$Connect,
            [Parameter(Mandatory = $False)]
            [String]$ConnectName,
            [Parameter(Mandatory = $False)]
            [String]$SiteName)
        Process
        {
            $Text = ($Password + ";" + $userName + ";" + $Connect + ";" + $SiteName)
            $Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text)
            $EncodedText = [Convert]::ToBase64String($Bytes)
            $EncodedText | Out-File "$env:SystemDrive$env:HOMEPATH\$ConnectName.ssl" -Force
        } #End Process
    }
    
    function Remove-Connection
    {
        
        Param (
            [String]$ConnectName)
        Process
        {
            if (Test-Path "$env:SystemDrive$env:HOMEPATH\$ConnectName.ssl")
            {
                
                Remove-Item -Path "$env:SystemDrive$env:HOMEPATH\$ConnectName.ssl" -Force
            }
        } #End Process
    }
    
    function Import-Connection
    {
        Param ([String]$ConnectName)
        Process
        {
            IF (Test-Path "$env:SystemDrive$env:HOMEPATH\$ConnectName.ssl")
            {
                $EncodedText = Get-Content -Path "$env:SystemDrive$env:HOMEPATH\$ConnectName.ssl"
                $Content = ([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedText)) -split ';')
                $Password = $Content[0]
                $UserName = $Content[1]
                $Connect = $Content[2]
                $SiteName = $Content[3]
                
                $Data = @{
                    "Password" = $Password;
                    "UserName" = $UserName;
                    "Connect"  = $Connect;
                    "SiteName" = $SiteName;
                }
                
                Write-Output $Data
            }else{ Write-Host "The connection could not be found" }
        } #End Process
    }
    
    function New-Connection{
    Param (
        [Parameter(Mandatory = $true,
                   HelpMessage = "Enter office Admin Email.")]
        [String]$UserName,
        [Parameter(Mandatory = $true,
                   HelpMessage = "Enter office Admin Password.")]
        [String]$Password,
        [Parameter(
                   HelpMessage = "Enter office Admin Connect.")]
        [ValidateSet("AzureAD", "SPOService", "PnPOnline", "PnPOnlineAdmin", "MsolService")]
        [String[]]$Connect,
        [Parameter(Mandatory = $False)]
        [String]$SiteName)
    
    
    BEGIN
    {
        $Tenant = ($UserName -split '@')[1].Replace('.onmicrosoft.com', '')
        $encpassword = convertto-securestring -String $Password -AsPlainText -Force
        $Credential = New-Object -typename System.Management.Automation.PSCredential($UserName, $encpassword)
    }
    
    PROCESS
    {
        try
        {
            Switch ($Connect)
            {
                "AzureAD" { Connect-AzureAD -Credential $Credential | Out-Null; Write-host "AzureAD Connected" -ForegroundColor Green}
                "SPOService" { Connect-SPOService -Url "https://$Tenant-admin.sharepoint.com" -Credential $Credential | Out-Null; Write-host "SPOService Connected" -ForegroundColor Green }
                "MsolService" { Connect-MsolService -Credential $Credential | Out-Null; Write-host "MsolService Connected" -ForegroundColor Green}
                "PnPOnline"{ IF (!$SiteName) { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com" -Credentials $Credential }
                    else { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com/sites/$SiteName/" -Credentials $Credential }; Write-Host "PnPOnline Connected" -ForegroundColor Green }
                "PnPOnlineAdmin"{ IF (!$SiteName) { Connect-PnPOnline -Url "https://$Tenant-admin.sharepoint.com" -Credentials $Credential; Write-Host "PnPOnline Connected" -ForegroundColor Green }
                    else { Connect-PnPOnline -Url "https://$Tenant-admin.sharepoint.com/sites/$SiteName/" -Credentials $Credential }; Write-Host "PnPOnlineAdmin Connected" -ForegroundColor Green }
                    default
                    {
                        IF (!$SiteName) { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com" -Credentials $Credential; Write-Host "PnPOnline Connected" -ForegroundColor Green}
                        else { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com/sites/$SiteName/" -Credentials $Credential }; Write-Host "PnPOnline Connected" -ForegroundColor Green }
            }
        }
        catch { Out-Null | Write-host Connect Error -ForegroundColor Red }
    } #End Process
    }
    
    function Get-Connection
    {
        Param (
            [String]$ConnectName,
            [String]$SiteName,
            [ValidateSet("AzureAD", "SPOService", "PnPOnline", "PnPOnlineAdmin", "MsolService")]
            [String[]]$Connect
        )
        
        IF (Test-Path "$env:SystemDrive$env:HOMEPATH\$ConnectName.ssl")
        {
            $Content = Import-Connect $ConnectName
            $Tenant = ($Content.UserName -split '@')[1].Replace('.onmicrosoft.com', '')
            $encpassword = convertto-securestring -String $Content.Password -AsPlainText -Force
            $Credential = New-Object -typename System.Management.Automation.PSCredential($Content.UserName, $encpassword)
            $GetConnect = $Connect
            $GetSiteName = $SiteName
            IF (!$GetConnect) { $GetConnect = $Content.Connect }
            IF (!$GetSiteName) { $GetSiteName = $Content.SiteName }
            
            try
            {
                Switch ($GetConnect)
                {
                    "AzureAD" { Connect-AzureAD -Credential $Credential | Out-Null; Write-Host "AzureAD Connected" -ForegroundColor Green }
                    "SPOService" { Connect-SPOService -Url "https://$Tenant-admin.sharepoint.com" -Credential $Credential | Out-Null; Write-Host "SPOService Connected" -ForegroundColor Green }
                    "MsolService" { Connect-MsolService -Credential $Credential | Out-Null; Write-Output "MsolService Connected" -ForegroundColor Green }
                    "PnPOnline"{ IF (!$GetSiteName) { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com" -Credentials $Credential }
                        else { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com/sites/$GetSiteName/" -Credentials $Credential }; Write-Host "PnPOnline Connected" -ForegroundColor Green }
                    "PnPOnlineAdmin"{ IF (!$GetSiteName) { Connect-PnPOnline -Url "https://$Tenant-admin.sharepoint.com" -Credentials $Credential; Write-Host "PnPOnline Connected" -ForegroundColor Green }
                        else { Connect-PnPOnline -Url "https://$Tenant-admin.sharepoint.com/sites/$GetSiteName/" -Credentials $Credential }; Write-Host "PnPOnlineAdmin Connected" -ForegroundColor Green }
                    default { IF (!$GetSiteName) { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com" -Credentials $Credential; Write-Host "PnPOnline Connected" -ForegroundColor Green}
                        else { Connect-PnPOnline -Url "https://$Tenant.sharepoint.com/sites/$GetSiteName/" -Credentials $Credential }; Write-Host "PnPOnline Connected" -ForegroundColor Green }
                }
            }
            catch { Write-Host "Connect Error " -ForegroundColor Red }
            
        } #End IF
        else { Write-Host "Not found the connection" -ForegroundColor Red }
    }
    
    function Search-Connection
    {
        [CmdletBinding(ConfirmImpact = 'Medium',
                       PositionalBinding = $false,
                       SupportsPaging = $false,
                       SupportsShouldProcess = $false)]
        param ()
        
        #TODO: Place script here
    }