framework/Resources/Scripts/install_client.ps1

Param(
    [Parameter(Mandatory)]
    [string]$Key,
    [Parameter(Mandatory)]
    [string]$FirstName,
    [Parameter(Mandatory)]
    [string]$LastName,
    [Parameter(Mandatory)]
    [string]$Company,
    [Parameter(Mandatory)]
    [string]$Email,
    [Parameter(Mandatory)]
    [string]$ServerName,
    [Parameter(Mandatory)]
    [string]$EnvironmentName,
    [string]$InstallDir = "C:\Program Files\NCache"
)

$script:ScriptWindows = $null
$script:resourceGroupName = $null
$script:scriptLinux = $null
$script:vmOS = $null
function Enable-SystemAssignedIdentity {
    param(
        [Parameter(Mandatory)][string]$VmName
    )

    Write-Host "$((Get-Date).ToString()) - Enabling system-assigned managed identity on $VmName"
    $vm = Get-AzVM -ResourceGroupName $script:resourceGroupName -Name $VmName -ErrorAction Stop 
    Update-AzVM -ResourceGroupName $script:resourceGroupName -VM $vm -IdentityType SystemAssigned -ErrorAction Stop 
}

function CreateScriptBlockWindows {
    $ScriptBlock = {
        param(
            [string]$Key,
            [string]$firstName,
            [string]$lastName,
            [string]$email,
            [string]$company
        )
        Invoke-WebRequest "https://www.alachisoft.com/downloads/ncache.ent.net.x64.msi" -UseBasicParsing
        msiexec.exe /I "ncache.ent.net.x64.msi" InstallMode=3 key=$Key USERFIRSTNAME=$FirstName USERLASTNAME=$LastName COMPANYNAME=$Company EMAILADdRESS=$Email INSTALLDIR=$InstallDir
    }
    $script:ScriptWindows = [scriptblock]::create($ScriptBlock)
}

function InvokeCommandOnServerWindows {
    
    $script = @"
New-Item -ItemType Directory -Force -Path "C:\Temp" | Out-Null
wget "https://ncachedeployments.s3.us-east-1.amazonaws.com/5.3.6-tools/InstallScript_Win_Client.ps1" -OutFile "C:\Temp\InstallScript_Win_Client.ps1"
C:\Temp\InstallScript_Win_Client.ps1 -Key $Key -Company $Company -FirstName $FirstName -LastName $LastName -Email $Email -InstallDir '$InstallDir'
"@


    Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Downloading and Installing NCache on $ServerName"
    Invoke-AzVMRunCommand -ResourceGroupName $script:resourceGroupName -VMName $ServerName -CommandId 'RunPowerShellScript' -ScriptString $script
    Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Installed NCache on server $ServerName"
}

function InvokeCommandOnServerLinux {
    $command = CreateScriptBlockLinux
    Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Downloading and Installing NCache on $ServerName"
    Invoke-AzVMRunCommand -ResourceGroupName $script:resourceGroupName -VMName $ServerName -CommandId 'RunShellScript' -ScriptString $command
    Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Installed NCache on server $ServerName"
}
function CreateScriptBlockLinux {
    return "sudo wget -O /tmp/InstallScript_Lin_Client.sh https://ncachedeployments.s3.us-east-1.amazonaws.com/5.3.6-tools/InstallScript_Lin_Client.sh;
    chmod +x /tmp/InstallScript_Lin_Client.sh;
    /tmp/InstallScript_Lin_Client.sh -FirstName $FirstName -LastName $LastName -Email $Email -Key $Key -Company $Company -SetupUrl 'https://www.alachisoft.com/downloads/ncache.ent.net.tar.gz'"

}

function GetVmOSType {
    $vm = Get-AzVM -ResourceGroupName $script:resourceGroupName -Name $ServerName -ErrorAction Stop
    $script:vmOS = $vm.StorageProfile.OsDisk.OsType
}

function AddClientTag {
    $vm = Get-AzVM -ResourceGroupName $script:resourceGroupName -Name $ServerName -ErrorAction Stop

    $tags = @{}
    $tags["InstallMode"] = "client"

    Set-AzResource -ResourceId $vm.Id -Tag $tags -Force
}

function CreateAndInvokeScript {
    GetVmOSType
    Enable-SystemAssignedIdentity -VmName $ServerName
    if ($script:vmOS -eq "Windows") {
        CreateScriptBlockWindows
        InvokeCommandOnServerWindows
    }
    else {
        InvokeCommandOnServerLinux
    }
    
}

function GetResourceGroup {
    $resource = Get-AzResourceGroup -ErrorAction Stop  | Where-Object { $_.Tags -and $_.Tags.Contains("EnvironmentName") -and $_.Tags["EnvironmentName"] -eq $EnvironmentName } 
    if (-not $resource) {
        $rg = Get-AzResourceGroup -Name $EnvironmentName -ErrorAction Stop
        if (-not $rg) {
            throw "No Environments Found"
        }
        $script:resourceGroupName = $rg.ResourceGroupName
    }
    else {
        $script:resourceGroupName = $resource.ResourceGroupName
    }
}

function ExecuteCommand {
    GetResourceGroup
    CreateAndInvokeScript
    AddClientTag
}

try {

    if (-not (Get-AzContext)) {
        Connect-AzAccount
        if (Get-AzContext) {
            ExecuteCommand
        }
    }
    else {
        ExecuteCommand
    }
}
catch {
    Write-Error $($_.Exception.Message)
}