httools.psm1

function ConvertFrom-HTSerializedJson
{
    [cmdletbinding()]
    Param
    (
        [Parameter(Mandatory = $true,Position=1)]
        [string]$File
    )
    Begin
    {
        $File = Get-Item -LiteralPath $File
    }

    Process
    {
        Add-Type -Assembly System.Web.Extensions
        $text = [IO.File]::ReadAllText($File)
        $parser = New-Object Web.Script.Serialization.JavaScriptSerializer
        $parser.MaxJsonLength = $text.length
        $parser.DeserializeObject($text)
    }
}

function ConvertTo-HTSerializedJson
{
    [cmdletbinding()]
    Param
    (
        [Parameter(Mandatory = $true,Position=1)]
        [System.Object]$Object,
        [Parameter(Mandatory = $true,Position=2)]
        [string]$File
    )
    Process
    {
        ConvertTo-Json $Object | Out-File -LiteralPath $File -Encoding utf8
    }
}

function Invoke-HTPlatformMessage
{
    [CmdletBinding()]
    param (
        [string]$RawMessage,
        [string]$ToDirectComponent,
        [int]$Count=1
    )
    begin {
        if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
        {
        $certCallback = '
            using System;
            using System.Net;
            using System.Net.Security;
            using System.Security.Cryptography.X509Certificates;
            public class ServerCertificateValidationCallback
            {
                public static void Ignore()
                {
                    if(ServicePointManager.ServerCertificateValidationCallback ==null)
                    {
                        ServicePointManager.ServerCertificateValidationCallback +=
                            delegate
                            (
                                Object obj,
                                X509Certificate certificate,
                                X509Chain chain,
                                SslPolicyErrors errors
                            )
                            {
                                return true;
                            };
                    }
                }
            }
        '

        Add-Type $certCallback
         }
        [ServerCertificateValidationCallback]::Ignore()
    }
    process {
        $posturi = 'https://localhost:8001'

        if($ToDirectComponent -ne '')
        {
            $posturi = "https://localhost:8003/$ToDirectComponent/1"
        }

        $param = @{
            'Uri' = $posturi;
            'Method' = 'POST';
            'Headers' = @{'Content-Type'='application/json'};
            'Body' = $RawMessage
            }

            1..$Count | ForEach-Object {
                $result = Invoke-WebRequest @param
                Write-Information "Response:`n $($result.RawContent)"
            }

            Write-Information "Post URL: $posturi"  -ForegroundColor Yellow
            Write-Information "Count: $Count" -ForegroundColor Yellow
    }
}

function New-HTTempFolder
{
    [CmdletBinding()]
    param
    (
        [parameter(Position = 0)]
        [string]$FolderName
    )
    process
    {
        if ($FolderName -eq '')
        {
            return
        }
        $root_folder = Join-Path $env:TEMP 'HTToolsTemp'

        if (-not (Test-Path $root_folder))
        {
            New-Item $root_folder -ItemType Directory | Out-Null
        }

        $root_folder = Resolve-Path $root_folder

        $folder = Join-Path $root_folder $FolderName

        if (-not (Test-Path $folder))
        {
            $ret = New-Item $folder -ItemType Directory
        }
        else
        {
            $ret = Get-Item $folder
        }
        return $ret
    }
}

function Remove-HTTempFolder
{
    [CmdletBinding()]
    param
    (
        [parameter(Position = 0)]
        [string]$FolderName,
        [switch]$FlushTemp
    )
    process
    {
        $root_folder = Join-Path $env:TEMP 'HTToolsTemp'

        if (-not (Test-Path $root_folder))
        {
            Write-Error "Root Temp Folder not initialized..,"
            return
        }

        if ($FlushTemp)
        {
            Remove-Item $root_folder -Confirm:$false -Recurse

            Write-Error "All data in temp folder deleted..."
            return
        }
        if ($FolderName -eq '')
        {
            return
        }

        $FolderName = Join-Path $root_folder $FolderName
        if (-not (Test-Path $FolderName))
        {
            Write-Error "$FolderName not found... "
            return
        }
        Remove-Item $FolderName -Recurse -Confirm:$false
    }

}

function Write-HTElasticDocument
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Function is just for debugging purposes")]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]$ElasticUri,

        [Parameter(Mandatory=$true)]
        [string]$Index,

        [Parameter(Mandatory=$true)]
        [string]$Type,

        [Parameter(Mandatory=$true)]
        [string]$Document,

        [Parameter(Mandatory=$true)]
        [string]$ElasticUser,

        [Parameter(Mandatory=$true)]
        [string]$ElasticUserp
    )

    process {
        $es_username = $ElasticUser
        $es_password = $ElasticUserp
        $es_uri =  "$ElasticUri/$Index/$Type"

        $es_securecred = ConvertTo-SecureString $es_password -AsPlainText -Force

        $es_creds = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $es_username, $es_securecred

        
        $param = @{
            'Uri' = $es_uri;
            'Method' = 'POST';
            'Credential' = $es_creds;
            'Body' = $Document
            }

        $Result = Invoke-WebRequest @param

        Write-Output $Result.Content

    }
}

function Get-HTLicenseDays
{
    [CmdletBinding()]
    param (
        [Parameter(Position=0, Mandatory=$true)]
        [string]
        $EndDate
    )
    process {
        
        try {
            $end = [System.DateTime]::ParseExact($EndDate, 'dd/MM/yyyy', [System.Globalization.CultureInfo]::InvariantCulture)
            Write-Host "End date is = $end"
        }
        catch {
            Write-Host "Cannot Parse date $EndDate - with parser dd/mm/yyyy"
        }

        $timespan = $end - $(Get-Date)
        Write-Output $timespan.Days
    }
}

function Show-HTComponentStoreInfo
{
    [CmdletBinding()]
    param (
        
    )
    process {
        Invoke-Expression -Command 'Dism.exe /Online /Cleanup-Image /AnalyzeComponentStore'
    }
}

function Start-HTComponentCleanup 
{
    [CmdletBinding()]
    param (
        
    )
    process {
        Invoke-Expression -Command 'Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase'
    }

}

function Convertfrom-HTJsonString {
    [CmdletBinding()]
    param (
        # String input of JSON
        [Parameter(Mandatory=$true)]
        [string]
        $JsonString
    )
    process {
        $hashtbl = [HTdotnet.Tools.Prototype]::DeserializeObject($JsonString)
        return [pscustomobject]$hashtbl
    }
}