MyPSFunctions.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2021 v5.8.196
     Generated on: 4/25/2024 2:14 PM
     Generated by: John@MyPSFunctions.com
     Organization: MyPSFunctions
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2021
#>



    <#
        ===========================================================================
         Created with: SAPIEN Technologies, Inc., PowerShell Studio 2021 v5.8.196
         Created on: 10/27/2023 9:00 AM
         Created by: John@MyPSFunctions.com
         Organization: MyPSFunctions
         Filename: MyPSFunctions.psm1
        -------------------------------------------------------------------------
         Module Name: MyPSFunctions
        ===========================================================================
    #>

    
    
    Function Write-Log
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true)]
            [ValidateSet('Info', 'Warning', 'Error')]
            [string]$Level,
            [Parameter(Mandatory = $false)]
            [string]$LogPath,
            [Parameter(Mandatory = $true)]
            [string]$Message
        )
        
        switch ($Level)
        {
            Warning
            {
                # Write warning log
                if ($LogPath) { Write-Output "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] WARNING: $Message" | Out-File -FilePath $LogPath -Append }
                Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -nonewline; Write-host " WARNING: " -ForegroundColor Yellow -NoNewline; Write-Host $Message
            }
            Error
            {
                # Write Error log
                if ($LogPath) { Write-Output "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] Error: $Message" | Out-File -FilePath $LogPath -Append }
                Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -nonewline; Write-host " Error: " -ForegroundColor Red -NoNewline; Write-Host $Message
            }
            default
            {
                # Write Information log
                if ($LogPath) { Write-Output "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] INFO: $Message" | Out-File -FilePath $LogPath -Append }
                Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -nonewline; Write-host " INFO: " -ForegroundColor White -NoNewline; Write-Host $Message
            }
        }
    }
    
    Function Write-CLog
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true)]
            [ValidateSet('Info', 'Warning', 'Error')]
            [string]$Level,
            [Parameter(Mandatory = $false)]
            [string]$LogPath,
            [Parameter(Mandatory = $true)]
            [string]$Message
        )
        
        switch ($Level)
        {
            Warning
            {
                # Write warning log
                if ($LogPath) { Write-Output "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] WARNING: $Message" | Out-File -FilePath $LogPath -Append }
                Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -ForegroundColor Yellow -nonewline; Write-host " WARNING: " -NoNewline; Write-Host $Message -ForegroundColor Yellow
            }
            Error
            {
                # Write Error log
                if ($LogPath) { Write-Output "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] Error: $Message" | Out-File -FilePath $LogPath -Append }
                Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -ForegroundColor Red -nonewline; Write-host " Error: " -NoNewline; Write-Host $Message -ForegroundColor Red
            }
            default
            {
                # Write Information log
                if ($LogPath) { Write-Output "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] INFO: $Message" | Out-File -FilePath $LogPath -Append }
                Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -nonewline; Write-host " INFO: " -NoNewline; Write-Host $Message
            }
        }
    }
    
    Function Create-Folder
    {
        param
        (
            [Parameter(Mandatory = $true,
                       Position = 1)]
            [string]$Path
        )
        
        If (!(Test-Path $Path))
        { New-Item $Path -Type Directory -ErrorVariable FolderCreationFailed }
        Else
        {
            Write-Host  "The folder $Path is already created" -ForegroundColor Yellow
            
        }
        
        # Check if no error occurs during the folder creation
        If ($FolderCreationFailed)
        {
            
            Write-Host  "The folder $Path failed to be created" -ForegroundColor Red -BackgroundColor Yellow
            Write-Host  "Folder creation failed with error: $FolderCreationFailed" -ForegroundColor Red -BackgroundColor Yellow
        }
    }
    
    Function Zip-Folder
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true,
                       Position = 1)]
            [String]$Folder,
            [Parameter(Mandatory = $true,
                       Position = 2)]
            [String]$ZipFile
        )
        # Test if the ZipFile is existed
        Add-Type -assembly "system.io.compression.filesystem"
        [io.compression.zipfile]::CreateFromDirectory($Folder, $ZipFile)
    }
    
    Function Clear-PSSession
    {
        $PSSessions = Get-PSSession
        $PSSessionsCount = ($PSSessions | measure).count
        If ($PSSessionsCount -gt 0)
        {
            $PSSessions | ft
            Read-Host "Are you sure you want to clear all PS Session? - Press Enter to Continue or CTRL+C to Cancel"
            Get-PSSession | Remove-PSSession -Confirm:$false
            Write-Host "All PS Sessions have been removed" -ForegroundColor Green
        }
        Else
        { Write-Host "No PS Session Found" -ForegroundColor Red }
        
    }
    
    Function Allows-BasicWinRM
    {
        ###### Basic Authentication is disabled ####
        $RegistryPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client"
        $Name = "AllowBasic"
        $Value = "1"
        if (Test-path $RegistryPath)
        {
            New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force
        }
    }
    
    Function Test-ServerSSLSupport
    {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
            [ValidateNotNullOrEmpty()]
            [string]$HostName,
            [UInt16]$Port = 443
        )
        process
        {
            $RetValue = New-Object psobject -Property @{
                Host          = $HostName
                Port          = $Port
                SSLv2          = $false
                SSLv3          = $false
                TLSv1_0          = $false
                TLSv1_1          = $false
                TLSv1_2          = $false
                KeyExhange    = $null
                HashAlgorithm = $null
            }
            "ssl2", "ssl3", "tls", "tls11", "tls12" | %{
                $TcpClient = New-Object Net.Sockets.TcpClient
                $TcpClient.Connect($RetValue.Host, $RetValue.Port)
                $SslStream = New-Object Net.Security.SslStream $TcpClient.GetStream(),
                                        $true,
                ([System.Net.Security.RemoteCertificateValidationCallback]{ $true })
                $SslStream.ReadTimeout = 15000
                $SslStream.WriteTimeout = 15000
                try
                {
                    $SslStream.AuthenticateAsClient($RetValue.Host, $null, $_, $false)
                    $RetValue.KeyExhange = $SslStream.KeyExchangeAlgorithm
                    $RetValue.HashAlgorithm = $SslStream.HashAlgorithm
                    $status = $true
                }
                catch
                {
                    $status = $false
                }
                switch ($_)
                {
                    "ssl2" { $RetValue.SSLv2 = $status }
                    "ssl3" { $RetValue.SSLv3 = $status }
                    "tls" { $RetValue.TLSv1_0 = $status }
                    "tls11" { $RetValue.TLSv1_1 = $status }
                    "tls12" { $RetValue.TLSv1_2 = $status }
                }
                # dispose objects to prevent memory leaks
                $TcpClient.Dispose()
                $SslStream.Dispose()
            }
            $RetValue
        }
    }
    
    Function Append-PrefixFileswithinFolder
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true,
                       Position = 1)]
            [String]$Folder,
            [Parameter(Mandatory = $true,
                       Position = 2)]
            [String]$Prefix
        )
        $Files = $Null
        $Files = Get-ChildItem  $Folder
        Foreach ($File in $Files)
        {
            $OldName = $Null
            $OldName = $File.versioninfo.FileName
            $NewName = $Null
            $Name = $Null
            $Name = $File.Name
            $NewName = $Prefix + $Name
            
            rename-item -NewName $NewName -path $OldName
        }
    }
    
    Function Clear-IECachedData
    {
        [CmdletBinding(ConfirmImpact = 'None')]
        param
        (
            [Parameter(Mandatory = $false,
                       HelpMessage = ' Delete Temporary Internet Files')]
            [switch]$TempIEFiles,
            [Parameter(HelpMessage = 'Delete Cookies')]
            [switch]$Cookies,
            [Parameter(HelpMessage = 'Delete History')]
            [switch]$History,
            [Parameter(HelpMessage = 'Delete Form Data')]
            [switch]$FormData,
            [Parameter(HelpMessage = 'Delete Passwords')]
            [switch]$Passwords,
            [Parameter(HelpMessage = 'Delete All')]
            [switch]$All,
            [Parameter(HelpMessage = 'Delete Files and Settings Stored by Add-Ons')]
            [switch]$AddOnSettings
        )
        if ($TempIEFiles) { RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 8 }
        if ($Cookies) { RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 2 }
        if ($History) { RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 1 }
        if ($FormData) { RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 16 }
        if ($Passwords) { RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 32 }
        if ($All) { RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 255 }
        if ($AddOnSettings) { RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 4351 }
    }
    
    Function Get-ServerPublicIP
    {
        [CmdletBinding()]
        param (
            [string]$HostFQDN
        )
        if ($HostFQDN -ne "")
        {
            $IPInfo = Invoke-Command -ComputerName $HostFQDN -ScriptBlock { Invoke-RestMethod -Uri ('http://ipinfo.io/' + (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content) }
        }
        else
        {
            $IPInfo = Invoke-RestMethod -Uri ('http://ipinfo.io/' + (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content)
        }
        $IPAddress = $IPInfo.IpAddress
        $URI = "http://ip-api.com/json/" + $IPAddress
        $API_IPInfo = Invoke-RestMethod -Method Get -Uri $URI
        $API_IPInfo
    }
    
    Function Get-MyPublicIP
    {
        [CmdletBinding()]
        param ()
        
        $IPInfo = Invoke-RestMethod -Uri ('http://ipinfo.io/' + (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content)
        $IPAddress = $IPInfo.IpAddress
        $URI = "http://ip-api.com/json/" + $IPAddress
        $API_IPInfo = Invoke-RestMethod -Method Get -Uri $URI
        $API_IPInfo
    }
    
    Function Get-IPAddressInformation
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true,
                       Position = 1)]
            [String]$IPAddress
        )
        
        #$IPAddressInfo = Invoke-RestMethod -Uri ('http://ipinfo.io/' + $IPAddress)
        Write-Log -Level INFO -message "The Information from IP Info:"
        $URI = "http://ip-api.com/json/" + $IPAddress
        $API_IPInfo = Invoke-RestMethod -Method Get -Uri $URI
        $API_IPInfo
    }
    
    Function ConvertFrom-ImmutableIdToGuid ([string]$ImmutableId)
    {
        
        [Guid]([System.Convert]::FromBase64String($ImmutableId))
    }
    
    Function ConvertTo-Gb
    {
    <#
        .EXTERNALHELP MyPSFunctions.EXO.psm1-Help.xml
    #>

        param (
            [Parameter(
                       Mandatory = $true
                       )]
            [string]$size
        )
        process
        {
            if ($size -ne $null)
            {
                $value = $size.Split(" ")
                switch ($value[1])
                {
                    "GB" { $sizeInGb = ($value[0]) }
                    "MB" { $sizeInGb = ($value[0] / 1024) }
                    "KB" { $sizeInGb = ($value[0] / 1024 / 1024) }
                }
                return [Math]::Round($sizeInGb, 2, [MidPointRounding]::AwayFromZero)
            }
        }
    }
    
    Function ConvertTo-MB
    {
    <#
        .EXTERNALHELP MyPSFunctions.EXO.psm1-Help.xml
    #>

        param (
            [Parameter(
                       Mandatory = $true
                       )]
            [string]$size
        )
        process
        {
            if ($size -ne $null)
            {
                $value = $size.Split(" ")
                switch ($value[1])
                {
                    "GB" { $sizeInMB = ($value[0] * 1024) }
                    "MB" { $sizeInMB = ($value[0]) }
                    "KB" { $sizeInMB = ($value[0] / 1024) }
                }
                return [Math]::Round($sizeInMB, 2, [MidPointRounding]::AwayFromZero)
            }
        }
    }
    
    Function Select-MyProfile
    {
        # CSV Profile (MyPSFunctionsPSProfiles.csv)
        $PSProfileFolder = $Profile.Substring(0, $Profile.LastIndexOf("\"))
        $MyPSFunctionsGlobalPSProfilesFile = $PSProfileFolder + "\MyPSFunctionsPSProfiles.csv"
        # Check if file exist
        If (!(Test-Path $MyPSFunctionsGlobalPSProfilesFile))
        {
            Try
            {
                New-Item $MyPSFunctionsGlobalPSProfilesFile -Type File
                $CreatedCSVFileStatus = "Successful"
            }
            Catch
            {
                $ErrorMessage = $Error[0].Exception.Message
                $CMDLet = $Error[0].InvocationInfo.Line
                $FailedItem = $Error[0].Exception.ItemName
                Write-Log -Level Error -Message "Failed to create CSV File : $MyPSFunctionsGlobalPSProfilesFile"
                Write-Log -Level Error -Message "Failed to run the following CMDLet: $CMDLet"
                Write-Log -Level Error -Message "Failed with Error:$ErrorMessage"
                $CreatedCSVFileStatus = "Failed"
            }
        }
        Else
        {
            #Write-Log info -message "The File $MyPSFunctionsGlobalPSProfilesFile is already created"
            $CreatedCSVFileStatus = "Exist"
        }
        
        If ($CreatedCSVFileStatus -eq "Exist")
        {
            #Write-Log warning -message "The File $MyPSFunctionsGlobalPSProfilesFile exists"
            
            Try
            {
                $MyPSFunctionsGlobalPSProfiles = $null
                $MyPSFunctionsGlobalPSProfiles = @()
                $MyPSFunctionsGlobalPSProfiles = Import-Csv $MyPSFunctionsGlobalPSProfilesFile
            }
            Catch
            {
                $MyPSFunctionsGlobalPSProfiles = $null
                $MyPSFunctionsGlobalPSProfiles = @()
            }
        }
        Else
        {
            $MyPSFunctionsGlobalPSProfiles = $null
            $MyPSFunctionsGlobalPSProfiles = @()
        }
        
        if ($MyPSFunctionsGlobalPSProfiles)
        {
            $MyPSFunctionsGlobalPSProfilesCount = ($MyPSFunctionsGlobalPSProfiles | measure).count
            If ($MyPSFunctionsGlobalPSProfilesCount -ge 2)
            {
                $MyPSFunctionsSelectedProfile = $MyPSFunctionsGlobalPSProfiles | Out-GridView -PassThru
            }
            Else
            {
                $MyPSFunctionsSelectedProfile = $MyPSFunctionsGlobalPSProfiles
            }
            
            Try
            {
                Get-Variable MyPSFunctionsProfileName -ErrorAction Stop
                Set-Variable -Name MyPSFunctionsProfileName -Value $MyPSFunctionsSelectedProfile.ProfileName -Scope Global
            }
            Catch
            { New-Variable -Name MyPSFunctionsProfileName -Value $MyPSFunctionsSelectedProfile.ProfileName -Scope Global }
            
            
            Try
            {
                Get-Variable MyPSFunctionsTenantName -ErrorAction Stop
                Set-Variable -Name MyPSFunctionsTenantName -Value $MyPSFunctionsSelectedProfile.TenantName -Scope Global
            }
            Catch
            { New-Variable -Name MyPSFunctionsTenantName -Value $MyPSFunctionsSelectedProfile.TenantName -Scope Global }
            
            
            Try
            {
                Get-Variable MyPSFunctionsTenantNameOnMicrosoft -ErrorAction Stop
                $MyPSFunctionsTenantNameOnMicrosoft = $MyPSFunctionsTenantName + ".onmicrosoft.com"
                Set-Variable -Name MyPSFunctionsTenantNameOnMicrosoft -Value $MyPSFunctionsTenantNameOnMicrosoft -Scope Global
            }
            Catch
            {
                $MyPSFunctionsTenantNameOnMicrosoft = $MyPSFunctionsTenantName + ".onmicrosoft.com"
                New-Variable -Name MyPSFunctionsTenantNameOnMicrosoft -Value $MyPSFunctionsTenantNameOnMicrosoft -Scope Global
            }
            
            
            Try
            {
                Get-Variable MyPSFunctionsSharepointAdminURL -ErrorAction Stop
                $MyPSFunctionsTenantNameSharepoinAdminURL = "https://" + $MyPSFunctionsTenantName + "-admin.sharepoint.com"
                Set-Variable -Name MyPSFunctionsSharepointAdminURL -Value $MyPSFunctionsTenantNameSharepoinAdminURL -Scope Global
            }
            Catch
            {
                $MyPSFunctionsTenantNameSharepoinAdminURL = "https://" + $MyPSFunctionsTenantName + "-admin.sharepoint.com"
                New-Variable -Name MyPSFunctionsSharepointAdminURL -Value $MyPSFunctionsTenantNameSharepoinAdminURL -Scope Global
            }
            
            Try
            {
                Get-Variable MyPSFunctionsO365Admin -ErrorAction Stop
                Set-Variable -Name MyPSFunctionsO365Admin -Value $MyPSFunctionsSelectedProfile.O365Admin -Scope Global
            }
            Catch
            { New-Variable -Name MyPSFunctionsO365Admin -Value $MyPSFunctionsSelectedProfile.O365Admin -Scope Global }
            
            
            Try
            {
                Get-Variable MyPSFunctionsEXOHybridServerName -ErrorAction Stop
                Set-Variable -Name MyPSFunctionsEXOHybridServerName -Value $MyPSFunctionsSelectedProfile.EXOHybridServerName -Scope Global
            }
            Catch
            { New-Variable -Name MyPSFunctionsEXOHybridServerName -Value $MyPSFunctionsSelectedProfile.EXOHybridServerName -Scope Global }
            
            
            Try
            {
                Get-Variable MyPSFunctionsDCServerName -ErrorAction Stop
                Set-Variable -Name MyPSFunctionsDCServerName -Value $MyPSFunctionsSelectedProfile.DCServerName -Scope Global
            }
            Catch
            { New-Variable -Name MyPSFunctionsDCServerName -Value $MyPSFunctionsSelectedProfile.DCServerName -Scope Global }
            
            
            Try
            {
                Get-Variable MyPSFunctionsOnPremiseAdmin -ErrorAction Stop
                Set-Variable -Name MyPSFunctionsOnPremiseAdmin -Value $MyPSFunctionsSelectedProfile.OnPremiseAdmin -Scope Global
            }
            Catch
            { New-Variable -Name MyPSFunctionsOnPremiseAdmin -Value $MyPSFunctionsSelectedProfile.OnPremiseAdmin -Scope Global }
            
            
            Try
            {
                Get-Variable MyPSFunctionsAzAdmin -ErrorAction Stop
                Set-Variable -Name MyPSFunctionsAzAdmin -Value $MyPSFunctionsSelectedProfile.AzAdmin -Scope Global
            }
            Catch
            { New-Variable -Name MyPSFunctionsAzAdmin -Value $MyPSFunctionsSelectedProfile.AzAdmin -Scope Global }
            
            Write-Log Warning -Message "Your MyPSFunctions Global Variables have been set"
            # Get-Variable MyPSFunctions*
            
        }
        Else
        { Write-Log Error -Message "Global Variable does not exist, please use run Initiate-MyProfile" }
        
    }
    
    Function Add-MyProfile
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true,
                       Position = 1)]
            [String]$ProfileName,
            [Parameter(Mandatory = $true,
                       Position = 2)]
            [string]$TenantName,
            [Parameter(Mandatory = $true,
                       Position = 3)]
            [String]$O365Admin,
            [Parameter(Mandatory = $true,
                       Position = 4)]
            [String]$EXOHybridServerName,
            [Parameter(Mandatory = $true,
                       Position = 5)]
            [String]$DCServerName,
            [Parameter(Mandatory = $true,
                       Position = 6)]
            [String]$OnPremiseAdmin,
            [Parameter(Mandatory = $true,
                       Position = 7)]
            [String]$AzAdmin
        )
        
        # CSV Profile (MyPSFunctionsPSProfiles.csv)
        $PSProfileFolder = $Profile.Substring(0, $Profile.LastIndexOf("\"))
        $MyPSFunctionsGlobalPSProfilesFile = $PSProfileFolder + "\MyPSFunctionsPSProfiles.csv"
        $MyPSFunctionsGlobalPSProfilesFile
        # Check if file exist
        If (!(Test-Path $MyPSFunctionsGlobalPSProfilesFile))
        {
            Try
            {
                New-Item $MyPSFunctionsGlobalPSProfilesFile -Type File
                $CreatedCSVFileStatus = "Successful"
            }
            Catch
            {
                $ErrorMessage = $Error[0].Exception.Message
                $CMDLet = $Error[0].InvocationInfo.Line
                $FailedItem = $Error[0].Exception.ItemName
                Write-Log -Level Error -Message "Failed to create CSV File : $MyPSFunctionsGlobalPSProfilesFile"
                Write-Log -Level Error -Message "Failed to run the following CMDLet: $CMDLet"
                Write-Log -Level Error -Message "Failed with Error:$ErrorMessage"
                $CreatedCSVFileStatus = "Failed"
            }
        }
        Else
        {
            #Write-Log info -message "The File $MyPSFunctionsGlobalPSProfilesFile is already created"
            $CreatedCSVFileStatus = "Exist"
        }
        
        If ($CreatedCSVFileStatus -eq "Exist")
        {
            #Write-Log warning -message "The File $MyPSFunctionsGlobalPSProfilesFile exists"
            Try
            {
                $MyPSFunctionsGlobalPSProfiles = $null
                $MyPSFunctionsGlobalPSProfiles = @()
                $MyPSFunctionsGlobalPSProfiles = Import-Csv $MyPSFunctionsGlobalPSProfilesFile
                $MyPSFunctionsGlobalPSProfiles | ft
                $ImportCSVFileStatus = "Successful"
            }
            Catch
            {
                $ErrorMessage = $Error[0].Exception.Message
                $CMDLet = $Error[0].InvocationInfo.Line
                Write-Log -Level Error -Message "Failed to Import CSV File : $MyPSFunctionsGlobalPSProfilesFile"
                Write-Log -Level Error -Message "Failed to run the following CMDLet: $CMDLet"
                Write-Log -Level Error -Message "Failed with Error:$ErrorMessage"
                $ImportCSVFileStatus = "Failed"
            }
        }
        Else
        {
            $MyPSFunctionsGlobalPSProfiles = $null
            $MyPSFunctionsGlobalPSProfiles = @()
            $ImportCSVFileStatus = "Successful"
        }
        
        # Create Global Variables
        If ($ImportCSVFileStatus -eq "Successful")
        {
            # Set Update Variable MyPSFunctionsProfiles
            $MyPSFunctionsGlobalPSProfiles += New-object PSobject -Property ([Ordered] @{
                    ProfileName            = $ProfileName;
                    TenantName            = $TenantName;
                    O365Admin            = $O365Admin;
                    EXOHybridServerName = $EXOHybridServerName;
                    DCServerName        = $DCServerName;
                    OnPremiseAdmin        = $OnPremiseAdmin;
                    AzAdmin                = $AzAdmin;
                })
            
            # Set Export MyPSFunctionsProfiles
            Try
            {
        
                $MyPSFunctionsGlobalPSProfiles | Export-Csv $MyPSFunctionsGlobalPSProfilesFile -NoTypeInformation -Encoding UTF8
                Write-Log info -Message "Successfully export MyPSFunctionsPSProfiles.csv"
            }
            Catch
            {
                $ErrorMessage = $Error[0].Exception.Message
                Write-Log Error -Message "Failed to export MyPSFunctionsPSProfiles.csv with Error: $ErrorMessage"
            }
            Select-MyProfile
        }
        
        
    }
    
    Function Create-MyProfile
    {
        [CmdletBinding()]
        param ()
        
        If (!(Test-Path $PROFILE))
        {
            New-Item -Type file -Force $profile
            Write-Log Warning -Message "PSProfile has been created"
        }
        Else
        { Write-Log Warning -Message "PSProfile already created" }
        
    }
    
    Function Set-MyProfile
    {
        [CmdletBinding()]
        param ()
        
        If (!(Select-String -Path $PROFILE -Pattern 'MyPSFunctionsProfile'))
        {
            
            Add-Content $PROFILE "`################## MyPSFunctionsProfile ##################"
            Add-Content $PROFILE "`nCreate-Folder -Path 'C:\Scripts'"
            Add-Content $PROFILE "`nSet-Location 'C:\scripts'"
            Add-Content $PROFILE "`nEnable-Transcript -TranscriptFolder 'C:\Powershell_Transcripts\'"
            Add-Content $PROFILE "`nInitiate-MyProfile"
            Add-Content $PROFILE "`################## MyPSFunctionsProfile ##################"
            Write-Log Warning -Message "PS Profile has been set"
        }
        Else { Write-Log info -Message "PS Profile is already set" }
    }
    
    Function Enable-Transcript
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true,
                       Position = 1)]
            [String]$TranscriptFolder
        )
        Create-Folder -Path $TranscriptFolder
        $Transcript_Date = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
        $Transcript_File = $TranscriptFolder + "Transcript_" + $Transcript_Date + ".txt"
        Start-Transcript -Path $Transcript_File
    }
    
    Function Initiate-MyProfile
    {
        [CmdletBinding()]
        param ()
        
        # CSV Profile (MyPSFunctionsPSProfiles.csv)
        $PSProfileFolder = $Profile.Substring(0, $Profile.LastIndexOf("\"))
        $MyPSFunctionsGlobalPSProfilesFile = $PSProfileFolder + "\MyPSFunctionsPSProfiles.csv"
        
        #Import CSV
        
        If (Test-Path $MyPSFunctionsGlobalPSProfilesFile)
        {
            Try
            {
                $MyPSFunctionsGlobalPSProfilesTemp = Import-Csv $MyPSFunctionsGlobalPSProfilesFile
                $ImportCSVFileStatus = "Successful"
                
            }
            Catch
            {
                $ErrorMessage = $Error[0].Exception.Message
                $CMDLet = $Error[0].InvocationInfo.Line
                Write-Log -Level Error -Message "Failed to Import CSV File : $MyPSFunctionsGlobalPSProfilesFile"
                Write-Log -Level Error -Message "Failed to run the following CMDLet: $CMDLet"
                Write-Log -Level Error -Message "Failed with Error:$ErrorMessage"
                $ImportCSVFileStatus = "Failed"
            }
            
        }
        Else
        {
            Write-Log Error -message "The File $MyPSFunctionsGlobalPSProfilesFile does not exist"
            $ImportCSVFileStatus = "NotExist"
        }
        
        If ($ImportCSVFileStatus -eq "Successful")
        {
            Write-Log -Level info -Message "$MyPSFunctionsGlobalPSProfilesFile exists"
    
        }
        Else
        {
            Add-MyProfile
        }
        # Update PS Profile file
        If (Test-Path $Profile)
        {
            Set-MyProfile
        }
        Else
        {
            Create-MyProfile
            Set-MyProfile
        }
        Select-MyProfile
    }