frameworkResources/Scripts/InstallScript_Win_Server.ps1

Param(
    [Parameter(Mandatory)]
    [string]$Key,
    [Parameter(Mandatory)]
    [string]$UserName,
    [Parameter(Mandatory)]
    [string]$Company,
    [Parameter(Mandatory)]
    [string]$FirstName,
    [Parameter(Mandatory)]
    [string]$LastName,
    [Parameter(Mandatory)]
    [string]$Password,
    [Parameter(Mandatory)]
    [string]$Email,
    [Parameter(Mandatory)]
    [string]$SetupUrl,
    [string[]]$PrivateIps,
    [string]$LicenseKey,
    [bool]$EnableCacheServerPublicIp,
    [string]$Environment,
    [string]$LicenseDuration
)


function DownloadNcache {
    Start-BitsTransfer -Source $SetupUrl -Destination "C:\Temp\ncache.ent.net.x64.msi"
}

function installNCache {
    $msiPath = "C:\Temp\ncache.ent.net.x64.msi"
    $logPath = "C:\Temp\ncache_install.log"

    $arguments = @(
        "/i `"$msiPath`""
        "/qn"                                 
        "/l*v `"$logPath`""                   
        "USERNAME=$UserName"
        "INSTALLMODE=0"
        "COMPANYNAME=$Company"
        "USERFIRSTNAME=$FirstName"
        "USERLASTNAME=$LastName"
        "EMAILADDRESS=$Email"
        "PASSWORD=$Password"
        "KEY=$Key"
        "INSTALLDIR=`"C:\Program Files\NCache`""
    ) -join " "  
    Write-Host $arguments
    Start-Process msiexec.exe -ArgumentList $arguments -Wait -NoNewWindow


}

function SetSecurityRules {
    New-NetFirewallRule -DisplayName "NCache-In" -Direction Inbound -Action Allow `
        -Protocol TCP -LocalPort 9800, 8250-8260, 7800-7900, 8300-8400, 9900, 10000-11000
    New-NetFirewallRule -DisplayName "NCache-Out" -Direction Outbound -Action Allow `
        -Protocol TCP -LocalPort 7800-7900, 10000-11000
}

function RestartNCacheService {
    taskkill /IM Alachisoft.NCache.Service.exe /F;
    taskkill /IM Alachisoft.NCache.WebManager.exe /F;
    Start-Sleep -seconds 3
    
    $ncserviceState = Get-Service -Name NCacheSvc
    Invoke-Expression -Command 'Restart-Service NCacheSvc' | Out-Null
    $ncserviceState = Get-Service -Name NCacheSvc
    $ncserviceState.Status >> C:\NCache-Init-Status.txt
}

function UpdatePrivateIpInsideConfig {
    $ConfigPath = "C:\Program Files\NCache\config\config.ncconf"  

    $myPrivateIp = Get-NetIPAddress | Select-Object -ExpandPropert IPAddress | Select-Object -index 2

    $config = Get-Content -Path $ConfigPath

    foreach ($ip in $PrivateIps) {
        if ($ip -ne $myPrivateIp) {
            $line = " <server-node ip=""$ip"" active-mirror-node=""False""/>"
            $config = $config -replace '(?=</servers>)', "$line`r`n"
            Write-Output $ip
        }
    }

    Set-Content -Path $ConfigPath -Value $config
}

function RestartWindowsAgent {
    Restart-Service -Name "WindowsAzureGuestAgent" -Force
}

function InstallAzureMonitoring {
    # ================================
    # Variables
    # ================================
    $TELEGRAF_CONFIG = "C:\Program Files\InfluxData\telegraf\telegraf.conf"
    $NAMESPACE_PREFIX = "ncache-metrics"

    # ================================
    # Install Telegraf (if not installed)
    # ================================
    Start-BitsTransfer `
        -Source "https://dl.influxdata.com/telegraf/releases/telegraf-1.36.1_windows_amd64.zip" `
        -Destination "C:\Temp\telegraf-1.36.1_windows_amd64.zip"

    Expand-Archive "C:\Temp\telegraf-1.36.1_windows_amd64.zip" `
        -DestinationPath 'C:\Program Files\InfluxData\telegraf\'

    Set-Location "C:\Program Files\InfluxData\telegraf"

    Move-Item .\telegraf-1.36.1\telegraf.* .

    # ================================
    # Fetch Azure Metadata
    # ================================
    $headers = @{ "Metadata" = "true" }
    $azMetadata = Invoke-RestMethod -Headers $headers -Uri "http://169.254.169.254/metadata/instance?api-version=2021-02-01"

    $RESOURCE_ID = $azMetadata.compute.resourceId
    $REGION = $azMetadata.compute.location

    if ([string]::IsNullOrEmpty($RESOURCE_ID) -or [string]::IsNullOrEmpty($REGION)) {
        exit 1
    }

    # ================================
    # Configure Telegraf (PerfMon)
    # ================================
    @"
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = "0s"

[[inputs.win_perf_counters]]
  [[inputs.win_perf_counters.object]]
    ObjectName = "NCache"
    Instances = ["*"]
    Counters = ["# Clients", "Additions/sec", "Cache Size", "Count", "Deletes/sec", "Evictions/sec", "Expirations/sec", "Fetches/sec", "Queries/sec", "Requests/sec", "Updates/sec", "Query Index Size", "Expiration Index Size", "Eviction Index Size", "Message Store Size", "Messages Count", "Average us/add", "Average us/fetch", "Average us/remove", "Average us/insert", "Average us/cache operation"]
    Measurement = "*"
    IncludeTotal = true

  [[inputs.win_perf_counters.object]]
    ObjectName = "Processor Information"
    Instances = ["*"]
    Counters = ["% Processor Time"]
    Measurement = "win_cpu"
    IncludeTotal = false

  [[inputs.win_perf_counters.object]]
    ObjectName = "Memory"
    Counters = ["% Committed Bytes In Use"]
    Instances = ["------"]
    Measurement = "win_mem"
    IncludeTotal = false

[[outputs.azure_monitor]]
  region = "$REGION"
  namespace_prefix = "$NAMESPACE_PREFIX"
  resource_id = "$RESOURCE_ID"
  timeout = "20s"
"@
 | Set-Content -Path $TELEGRAF_CONFIG -Encoding ascii

    # ================================
    # Enable & Start Telegraf
    # ================================
    & "C:\Program Files\InfluxData\telegraf\telegraf.exe" --service install --config $TELEGRAF_CONFIG
    & "C:\Program Files\InfluxData\telegraf\telegraf.exe" --config $TELEGRAF_CONFIG --test
    & "C:\Program Files\InfluxData\telegraf\telegraf.exe" --service start    
}

function ActivateLicense {
    if (![string]::IsNullOrWhiteSpace($LicenseKey)) {
        $NActivateExpression = "Register-NCache -Key $LicenseKey -FirstName $FirstName -LastName $LastName -Email $Email -Company $Company -Environment $Environment -LicenseDuration $LicenseDuration";
        Invoke-Expression -Command $NActivateExpression
    }
}

function GetPrivateIp {
    return Get-NetIPAddress | Select-Object -ExpandPropert IPAddress | Select-Object -index 2
}

function GetPublicIp {

    try {
        return  (Invoke-WebRequest "ifconfig.me/ip" -UseBasicParsing).Content
    }
    catch {
        $_.Exception.Message >> C:\NCache-Init-Status.txt
    }
}

function ImportNCacheModules {
    Invoke-Expression -Command "import-module 'C:\Program Files\NCache\bin\tools\ncacheps'"
}

function SetCCaheServerPublicIp {
    if ($EnableCacheServerPublicIp) {
        $privateIp = GetPrivateIp
        $publicIp = GetPublicIp
        $NActivateExpression = "Set-CacheServerPublicIP -Server $privateIp -PublicIP $publicIp"
        Invoke-Expression -Command $NActivateExpression
    }
}

function DeleteTempDirectory{
    Remove-Item "C:\Temp" -r -force
}

function ExecuteCommands {
    DownloadNcache
    installNCache
    SetSecurityRules
    InstallAzureMonitoring
    UpdatePrivateIpInsideConfig
    ImportNCacheModules
    ActivateLicense
    SetCCaheServerPublicIp
    RestartNCacheService
    RestartWindowsAgent
    DeleteTempDirectory
}

ExecuteCommands