frameworkResources/Scripts/get_cache_server_statistics.ps1
Param( [Parameter(Mandatory)] [string]$EnvironmentName, [Parameter(Mandatory)] [string]$CacheName, [string]$Servers, [string]$CounterNames, [string]$Format = "tabular" ) $allServers = "" function GetUploadedVmNames { param( [Parameter(Mandatory)][string]$ResourceGroup ) $vms = Get-AzVM -ResourceGroupName $ResourceGroup -ErrorAction Stop | Where-Object { $_.Tags.ContainsKey("Caches") } if ([string]::IsNullOrWhiteSpace($Servers)) { $ServersList = foreach ($vm in $vms) { foreach ($nicRef in $vm.NetworkProfile.NetworkInterfaces) { $nicName = ($nicRef.Id -split "/")[-1] $nic = Get-AzNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroupName -ErrorAction Stop [PSCustomObject]@{ PrivateIps = ($nic.IpConfigurations | Select-Object -ExpandProperty PrivateIpAddress) } } } $script:allServers = ($ServersList | Select-Object -ExpandProperty PrivateIps) -join "," } return $vms | Select-Object -ExpandProperty Name } function InvokeCommandOnServerWindows { param( [string]$ResourceGroupName, [string[]]$VmNames ) $script = "Get-CacheServerStatistics -CacheName $CacheName" if (![string]::IsNullOrWhiteSpace($Servers)) { $script += " -Server `"$Servers`" " } else { $script += " -Server `"$script:allServers`" " } if (![string]::IsNullOrWhiteSpace($CounterNames)) { $script += " -CounterNames `"$CounterNames`" -DoNotShowDefaultCounters" } if (![string]::IsNullOrWhiteSpace($Format)) { $script += " -Format $Format" } $vm = $VmNames[0] $result = Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $vm -CommandId 'RunPowerShellScript' -ScriptString $script foreach ($val in $result.Value) { Write-Host $val.Message } } function InvokeCommandOnServerLinux { param( [string]$ResourceGroupName, [string[]]$VmNames ) $script = "/opt/ncache/bin/tools/get-cacheserverstatistics -cachename $CacheName" if (![string]::IsNullOrWhiteSpace($Servers)) { $script += " -server `"$Servers`" " } else { $script += " -server `"$script:allServers`" " } if (![string]::IsNullOrWhiteSpace($CounterNames)) { $script += " -counternames `"$CounterNames`" -donotshowdefaultcounters" } if (![string]::IsNullOrWhiteSpace($Format)) { $script += " -format $Format" } $vm = $VmNames[0] $result = Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $vm -CommandId 'RunShellScript' -ScriptString $script foreach ($val in $result.Value) { Write-Host $val.Message } } function InvokeCommandOnVms { param( [object]$Resource, [string]$ResourceGroupName, [string[]]$VmNames ) $os = $Resource.Tags['OsType'] if ($os -eq "Windows") { InvokeCommandOnServerWindows -ResourceGroupName $ResourceGroupName -VmNames $VmNames } else { InvokeCommandOnServerLinux -ResourceGroupName $ResourceGroupName -VmNames $VmNames } } function ExecuteCommands { $resource = Get-AzResourceGroup -ErrorAction Stop | Where-Object { $_.Tags -and $_.Tags.Contains("EnvironmentName") -and $_.Tags["EnvironmentName"] -eq $EnvironmentName } if (-not $resource) { throw "No such environment exists" } $resourceGroupName = $resource.ResourceGroupName $vmNames = GetUploadedVmNames -ResourceGroup $resourceGroupName InvokeCommandOnVms -Resource $resource -ResourceGroupName $resourceGroupName -VmNames $vmNames } try { if (-not (Get-AzContext)) { Connect-AzAccount if (Get-AzContext) { ExecuteCommands } } else { ExecuteCommands } } catch { Write-Error $($_.Exception.Message) } |