framework/Resources/Scripts/deactivate_ncache.ps1
|
Param( [Parameter(Mandatory = $true)] [string]$Key, [Parameter(Mandatory = $true)] [string]$EnvironmentName, [Parameter(Mandatory = $false)] [switch]$OfflineDeactivate, [Parameter(Mandatory = $false)] [string[]]$Servers, [Parameter(Mandatory = $false)] [switch]$NoLogo, [string]$ScriptsFolderPath = ".\Resources\Scripts" ) . "$ScriptsFolderPath\common.ps1" $resource = CheckIfEnvironmentExits -Environment $EnvironmentName function CreateCmdLinux { param( [string]$NCachePath, [string]$Server ) $command = CreateCmdletLinux -NCacheInstallDir $NCachePath -Cmdlet "unregister-ncache" if ($Key) { $command += " -key `"$Key`"" } if ($OfflineDeactivate) { $command += " -offlinedeactivate" } if ($Server) { $command += " -server `"$Server`"" } if ($NoLogo) { $command += " -nologo" } return $command } function CreateCmdWindows { param( [string]$Server ) $command = "Unregister-NCache" if ($Key) { $command += " -Key `"$Key`"" } if ($OfflineDeactivate) { $command += " -OfflineDeactivate" } if ($Server) { $command += " -Server `"$Server`"" } if ($NoLogo) { $command += " -Nologo" } 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 AddNCacheActivationTag { param( [string]$Server ) $vm = Get-AzVM -ResourceGroupName $($resource.ResourceGroupName) -Name $Server -ErrorAction Stop $mergedTags = @{"NCacheActivated" = $false; } Update-AzTag -ResourceId $vm.Id -Tag $mergedTags -Operation Merge } 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") { AddNCacheActivationTag -Server $vmDetails.Name } View-CommandResult -Result $res } } else { $vms = GetNCacheActivatedServers -rgName $($resource.ResourceGroupName) foreach ($vm in $vms) { $res = InvokeCommand -vmDetails $vm if ($res.status -eq "success") { AddNCacheActivationTag -Server $vm.Name } View-CommandResult -Result $res } } } function ExecuteCommand { CreateCmdlet } try { if (-not (Get-AzContext)) { Connect-AzAccount if ((Get-AzContext)) { ExecuteCommand } } else { ExecuteCommand } } catch { Write-Error $($_.Exception.Message) } |