frameworkResources/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, [string]$SetupUrl, [string[]]$SetupArrays, [string]$ScriptsFolderPath = ".\Resources\Scripts" ) . "$ScriptsFolderPath\log_monitoring_common.ps1" . "$ScriptsFolderPath\common.ps1" $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 VM '$VmName' for secure Azure access" $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 "$SetupUrl" -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' -SetupUrl $SetupUrl "@ 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 $SetupUrl -InstallNCacheDir $InstallDir" } 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 if (![string]::IsNullOrWhiteSpace($SetupUrl) -and $SetupUrl -notmatch '\.msi(\?|$)') { throw "Please provide a valid url" } else { if ([string]::IsNullOrWhiteSpace($SetupUrl)) { $SetupUrl = $SetupArrays[0] } if ([string]::IsNullOrWhiteSpace($InstallDir)) { $InstallDir = "C:\\Program Files\\NCache" } InvokeCommandOnServerWindows } } else { if (![string]::IsNullOrWhiteSpace($SetupUrl) -and $SetupUrl -notmatch '\.tar\.gz(\?|$)') { throw "Please provide a valid url" } else { if ([string]::IsNullOrWhiteSpace($SetupUrl)) { $SetupUrl = $SetupArrays[1] } if ([string]::IsNullOrWhiteSpace($InstallDir)) { $InstallDir = "/opt" } 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 EnableMonitoringOnVms { # check if resource group has EnableDetailedMonitoring tag set to true $resource = Get-AzResourceGroup -Name $script:resourceGroupName -ErrorAction Stop if (-not $resource -or -not $resource.Tags -or -not $resource.Tags.ContainsKey("EnableDetailedMonitoring") -or -not [System.Convert]::ToBoolean($resource.Tags["EnableDetailedMonitoring"])) { Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Detailed monitoring is not enabled for environment $($script:resourceGroupName). Skipping monitoring setup." return } # check if workspace, DCE and DCR exists in the resource group $workspace = Get-AzOperationalInsightsWorkspace -ResourceGroupName $script:resourceGroupName -Name "$script:resourceGroupName-Workspace" -ErrorAction SilentlyContinue if (-not $workspace) { Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - No Log Analytics workspace found in resource group $($script:resourceGroupName). Skipping monitoring setup." return } $dce = Get-AzDataCollectionEndpoint -ResourceGroupName $script:resourceGroupName -Name "$script:resourceGroupName-DCE" -ErrorAction SilentlyContinue if (-not $dce) { Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - No Data Collection Endpoint found in resource group $($script:resourceGroupName). Skipping monitoring setup." return } $dcr = Get-AzDataCollectionRule -ResourceGroupName $script:resourceGroupName -Name "$script:resourceGroupName-DCR" -ErrorAction SilentlyContinue if (-not $dcr) { Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - No Data Collection Rule found in resource group $($script:resourceGroupName). Skipping monitoring setup." return } Install-NcAzMonitoringOnVM -ResourceGroupName $script:resourceGroupName -VmName $ServerName } function ExecuteCommand { GetResourceGroup CreateAndInvokeScript AddClientTag EnableMonitoringOnVms } try { ShowExecutingCommand $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() if (-not (Get-AzContext)) { NoContext Connect-AzAccount if (Get-AzContext) { ExecuteCommand } } else { ExecuteCommand } $stopwatch.Stop() Write-Host "Execution Time: $($stopwatch.Elapsed.ToString())" } catch { Write-Error $($_.Exception.Message) } |