framework/Resources/Scripts/StartupScriptWin_v6.ps1
Param( [Parameter()] [string]$StagingCloudURL, [Parameter()] [string]$StagingActivateJsonURL, [Parameter()] [string]$StubDLLUpdatedURL, [Parameter()] [string]$EnableHttps, [Parameter(Mandatory = $true)] [string]$InstallType, [Parameter(Mandatory = $true)] [string]$Key, [Parameter(Mandatory = $true)] [string]$FirstName, [Parameter(Mandatory = $true)] [string]$LastName, [Parameter(Mandatory = $true)] [string]$Email, [string[]]$PrivateIps, [Parameter(Mandatory = $true)] [string]$Company, [Parameter(Mandatory = $true)] [string]$Username, [Parameter(Mandatory = $true)] [string]$Password, [Parameter(Mandatory = $false)] [string]$Environment, [int]$IsEvalKey, [string]$LicenseDuration ) function CreateSSLCertificate { if ($EnableHttps.Equals("True")) { $cert = New-SelfSignedCertificate -KeyAlgorithm RSA -CertStoreLocation "Cert:\LocalMachine\My" -Subject "localhost" -FriendlyName "MyCertificate" -TextExtension @("2.5.29.17={critical}{text}DNS=localhost") $pwd = ConvertTo-SecureString -String 'password1234' -Force -AsPlainText; $path = 'Cert:\LocalMachine\My\' + $cert.thumbprint Export-PfxCertificate -cert $path -FilePath 'C:\\MyCertificate.pfx' -Password $pwd Import-PfxCertificate -FilePath 'C:\\MyCertificate.pfx' -CertStoreLocation 'Cert:\LocalMachine\Root' -Password $pwd $kestrelSettings = '{"Kestrel":{"EndPoints":{"Http":{"Url":"http://0.0.0.0:8251"},"HttpsDefaultCert":{"Url":"https://0.0.0.0:8252"}},"Certificates":{"Default":{"Path":"C:\\MyCertificate.pfx","Password":"password1234"}}}}' $kestrelSettings | Out-File "C:\Program Files\NCache\bin\tools\web\config.json" } } 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 RegisterNCache { if ($Key.Equals("NotSpecified")) { $Key = "" } if ($Key -ne "") { $EVAL_SUCCESS = "NCache has been successfully registered for FREE evaluation on server" $EXT_SUCCESS = "NCache evaluation period has been extended" $TOTAL_RETRIES = 10 $RETRY_DELAY = 30 $retries = 0 while ($retries -lt $TOTAL_RETRIES) { $commandType = "Register-NCache" $envParam = "" if ($IsEvalKey -eq 1) { $commandType = "Register-NCacheEvaluation" } else { $envParam = " -Environment $Environment -LicenseDuration $LicenseDuration" } $NActivateExpression = $commandType + ' -Key ' + $Key + ' -FirstName ' + $FirstName + ' -LastName ' + $LastName + ' -Email ' + $Email + ' -Company ' + $Company + $envParam try { $response = Invoke-Expression -Command $NActivateExpression $response >> C:\NCache-Init-Status.txt if (-not [string]::IsNullOrEmpty($error)) { $error >> C:\NCache-Init-Status.txt Start-Sleep -seconds $RETRY_DELAY $retries++; $error.clear(); } else { break; } } catch { $_.Exception.Message >> C:\NCache-Init-Status.txt Start-Sleep -seconds $RETRY_DELAY $retries++; } } } } 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 SetRegistryValues { try { Set-ItemProperty -Path HKLM:\\SOFTWARE\\Alachisoft\\NCache -Name InstallType -Value $InstallType } catch { $_.Exception.Message >> C:\NCache-Init-Status.txt } } function PlaceUpdatedStubDLL { try { if ($StubDLLUpdatedURL -ne "") { Invoke-WebRequest -Uri $StubDLLUpdatedURL -OutFile "C:\Program Files\NCache\bin\NActivate\Alachisoft.NCache.StubDll.dll" } } catch { $_.Exception.Message >> C:\NCache-Init-Status.txt } } function PlaceActivateJson { try { if ($StagingActivateJsonURL -ne "") { Invoke-WebRequest -Uri $StagingActivateJsonURL -OutFile "C:/Program Files/NCache/bin/NActivate/activate.json" } } catch { $_.Exception.Message >> C:\NCache-Init-Status.txt } } function CreateNewUser { if (-not ([string]::IsNullOrWhiteSpace($Username) -or [string]::IsNullOrWhiteSpace($Password))) { $user = New-LocalUser -AccountNeverExpires:$true -Password ( ConvertTo-SecureString -AsPlainText -Force $Password) -Name $Username -FullName $Username Add-LocalGroupMember -Group "Administrators" -Member $Username Add-LocalGroupMember -Group "Remote Desktop Users" -Member $Username } } 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 RunServiceUnderNewUser { Invoke-WebRequest -Uri "https://ncachedeployments.s3.us-east-1.amazonaws.com/5.3.6-tools/ntrights.exe" -OutFile "C:/ntrights.exe" Unblock-File -Path "C:/ntrights.exe" $ntrightsPath = "C:\ntrights.exe" if ((Test-Path $ntrightsPath)) { # --- Step 1: Grant 'Log on as a service' right --- & $ntrightsPath -u "$env:COMPUTERNAME\$Username" +r SeServiceLogonRight # --- Step 2: Assign service account --- $service = Get-WmiObject -Class Win32_Service -Filter "Name='NCacheSvc'" $result = $service.Change( $null, $null, $null, $null, $null, $null, ".\$Username", $Password, $null, $null, $null ) } } if (!(Test-Path C:\NCache-Init-Status.txt)) { $STARTUP_DELAY = 30 Start-Sleep -seconds $STARTUP_DELAY CreateNewUser SetRegistryValues PlaceActivateJson CreateSSLCertificate RegisterNCache UpdatePrivateIpInsideConfig RunServiceUnderNewUser RestartNCacheService } |