core/Resources/Scripts/activate_ncache.ps1
|
Param( [Parameter(Mandatory)] [string]$Key, [Parameter(Mandatory)] [string[]]$Servers, [Parameter(Mandatory)] [string]$EnvironmentName, [int]$Clients, [string]$Address, [string]$City, [string]$Comapny, [string]$Country, [string]$Email, [string]$FirstName, [string]$LastName, [string]$Phone, [bool]$Reactivate, [string]$State, [string]$ZipCode, [string]$AuthCode, [long]$Port, [switch]$OfflineActivate, [string]$KeyType, [switch]$NoLogo, [string]$RegisterAs, [string]$LicenseDuration, [switch]$Recurring, [string]$ScriptsFolderPath = ".\Resources\Scripts" ) . "$ScriptsFolderPath\common.ps1" $resource = CheckIfEnvironmentExits -Environment $EnvironmentName function CreateCmdLinux { param( [string]$NCachePath, [string]$Server ) $command = CreateCmdletLinux -NCacheInstallDir $NCachePath -Cmdlet "register-ncache -key $Key -email $Email -firstname $FirstName -lastname $LastName" if ($OfflineActivate) { $command += " -offlineactivate" } if ($Server) { $command += " -server $Server" } if ($NoLogo) { $command += " -nologo" } if ($RegisterAs) { $command += " -registeras $RegisterAs" } if ($LicenseDuration -and $KeyType -eq "License") { $command += " -licenseduration $LicenseDuration" } if ($Recurring) { $command += " -recurring" } if ($Address) { $command += " -address `"$Address`"" } if ($City) { $command += " -city `"$City`"" } if ($Company) { $command += " -company $Company" } if ($Country) { $command += " -country $Country" } if ($Phone) { $command += " -phone $Phone" } if ($State) { $command += " -state $State" } if ($ZipCode) { $command += " -zipcode $ZipCode" } if ($AuthCode) { $command += " -authcode $AuthCode" } if ($KeyType) { $command += " -keytype $KeyType" } if ($Clients) { $command += " -clients $Clients" } if ($Reactivate) { $command += " -reactivate" } if ($Port) { $command += " -port $Port" } if ($KeyType -eq "License") { $command += " -environment $EnvironmentName" } return $command } function CreateCmdWindows { param( [string]$Server ) $command = "Register-NCache -key $Key -Email $Email -FirstName $FirstName -LastName $LastName " if ($OfflineActivate) { $command += " -OfflineActivate" } if ($Server) { $command += " -Server $Server" } if ($NoLogo) { $command += " -NoLogo" } if ($RegisterAs) { $command += " -RegisterAs $RegisterAs" } if ($LicenseDuration -and $KeyType -eq "License") { $command += " -LicenseDuration $LicenseDuration" } if ($Recurring) { $command += " -Recurring" } if ($Address) { $command += " -Address `"$Address`"" } if ($City) { $command += " -City `"$City`"" } if ($Company) { $command += " -Company `"$Company`"" } if ($Country) { $command += " -Country `"$Country`"" } if ($Phone) { $command += " -Phone `"$Phone`"" } if ($State) { $command += " -State `"$State`"" } if ($ZipCode) { $command += " -ZipCode `"$ZipCode`"" } if ($AuthCode) { $command += " -AuthCode `"$AuthCode`"" } if ($KeyType) { $command += " -KeyType `"$KeyType`"" } if ($Clients) { $command += " -Clients $Clients" } if ($Reactivate) { $command += " -Reactivate" } if ($Port) { $command += " -Port $Port" } if ($KeyType -eq "License") { $command += " -Environment $EnvironmentName" } return $command } function InvokeCommand { param( [pscustomobject]$vmDetails ) $result = $null $cmdlet = "" if ($resource.Tags["OsType"] -eq "Linux") { $NCachePath = Get-LinuxNCacheInstallDir -VMName $($vmDetails.Name) -ResourceGroupName $($resource.ResourceGroupName) $cmdlet = CreateCmdLinux -NCachePath $NCachePath -Server $($vmDetails.Name) } else { $cmdlet = CreateCmdWindows -Server $($vmDetails.Name) } $result = InvokeCommandOnVms -ResourceGroupName $($resource.ResourceGroupName) -Server $($vmDetails.Name) -Command $cmdlet -OS $vmDetails.OsType return $result } function AddLicenseKeyTag { if ($KeyType -eq "License") { $mergedTags = @{"LicenseKey" = $Key; "LicenseDuration" = $LicenseDuration } Update-AzTag -ResourceId $($resource.ResourceId) -Tag $mergedTags -Operation Merge | Out-Null } elseif ($KeyType -eq "Extension") { $mergedTags = @{"ExtensionKey" = $Key; } Update-AzTag -ResourceId $($resource.ResourceId) -Tag $mergedTags -Operation Merge | Out-Null } } function AddNCacheActivationTagServer { param( [string]$Server ) $vm = Get-AzVM -ResourceGroupName $($resource.ResourceGroupName) -Name $Server -ErrorAction Stop $mergedTags = @{"NCacheActivated" = $true; } Update-AzTag -ResourceId $vm.Id -Tag $mergedTags -Operation Merge | Out-Null } function CreateCmdlet { if ($Servers.Count -gt 0) { foreach ($server in $Servers) { $vmDetails = GetVmDetailsFromPrivateIp -rgName $($resource.ResourceGroupName) -PrivateIp $server $res = InvokeCommand -vmDetails $vmDetails if ($res.status -eq "success" -and $KeyType -eq "License") { AddNCacheActivationTagServer -Server $vmDetails.Name } View-CommandResult -Result $res } } else { $vms = GetNCacheServers -rgName $($resource.ResourceGroupName) foreach ($vm in $vms) { $res = InvokeCommand -vmDetails $vm if ($res.status -eq "success" -and $KeyType -eq "License") { AddNCacheActivationTagServer -Server $vm.Name } View-CommandResult -Result $res } } } function ExecuteCommand { CreateCmdlet AddLicenseKeyTag } try { ShowExecutingCommand if (-not (Get-AzContext)) { NoContext Connect-AzAccount if ((Get-AzContext)) { ExecuteCommand } } else { ExecuteCommand } } catch { Write-Error $($_.Exception.Message) } |