Close all active Frame sessions

By David Horvath

April 25, 2022 | min

The Nutanix® Frame® Desktop-as-a- Service (DaaS) solution that allows administrators to create pools of non-persistent desktops and applications for use by internal and external users. When used on one of the public cloud infrastructures – AWS®, Azure® or GCP® clouds –  this service can scale out very quickly and increase an organization's capacity to support a variety of use cases

One use case provides applications for training or tradeshow/conference demonstration purposes on a temporary basis.This use case could become important to close all the active sessions on a Frame account to allow deprovisioning of the cloud resources. This blog will step you through how the Frame admin API can be used to accomplish this objective via a PowerShell script.

Concept of operations

The concept of operations is pretty simple:

  1. Get the pool IDs of all the Frame production pools for the account
  2. Get a list of active sessions
  3. Close an active sessions if it is in a production pool

Prerequisites

For this script to operate, you will need the following values:

  • Client ID and Client Secret: These are the credentials used to call the Frame admin API. They can be obtained by following the Frame documentation "How to Provision API Credentials". The permissions that you grant to the credentials will need to have administrative access to the Frame account that you are working with.
  • Account ID: The account ID is the Universally Unique Identifier (UUID) of the Frame account in which you will update the elasticity settings. You can find that value by going to the Frame admin UI and choosing “Update” on the account in which you plan to change the elasticity parameters. 

Choose “Update” after clicking on the kabob on the far right

In your browser’s location bar you will see something like:

https://frame.nutanix.com/frame/account/1f86e290-8cd2-4950-9c5a-9d3f7ed332e7/basic-info

The information between “account” and “basic-info” (in this example, 1f86e290-8cd2-4950-9c5a-9d3f7ed332e7) is the Account ID.

Some REST basics

With the Frame admin API, we use REST calls to interact with the Frame control plane. Some of the calls are queries or requests for information and they use the HTTPS GET request to gather that information. Some of the REST calls ask Frame to perform something known as “mutations.” These calls use the HTTPS POST or DELETE request since they are intended to change something within the Frame control plane.

In PowerShell, the method type of request is sent as an argument in the HTTPS call:

$response = Invoke-RestMethod -Method Get -Uri $api -Headers $headers


To perform proper error handling, the developed script has two different functions:

  1. The Get-FrameAPICall function implements an HTTPS “GET” request and is used to gather information about the pools and sessions
  2. The Delete-FrameAPICall function implements an HTTPS “DELETE” request and is used to close the sessions

The script

After defining those functions, the main part of the script is pretty self explanatory. First, gather all the production pool IDs and put them in a list called $pools.

$req_string = "https://api.console.nutanix.com/v1/accounts/" + $acct_id + "/pools"
$res = Get-FrameAPICall -client_id $clnt_id -client_secret $clnt_secret -api $req_string
$pools=@()

foreach ($j in $res)
{
	if ($j.kind -eq "production")
	{
		$pools += $j.external_id
	}
}


Then you get a list of the active sessions.

$req_string = "https://api.console.nutanix.com/v1/accounts/" + $acct_id + "/active_sessions"
$res = Get-FrameAPICall -client_id $clnt_id -client_secret $clnt_secret -api $req_string


Now you loop through all active sessions and if the pool_id value matches an id in $pools, you close the session using Delete-FrameAPICall.

foreach ($i in $res)
{
    # Check for each production pool_id
	foreach ($j in $pools)
	{
		if (($i.id -ne $null) -and ($i.pool_id -eq $j))
		{
			Write-Host "Closing "$i.id
			$req_string = "https://api.console.nutanix.com/v1/accounts/" + $acct_id + "/sessions/" + $i.id
			$res = Delete-FrameAPICall -client_id $clnt_id -client_secret $clnt_secret -api $req_string
		}
	}
}


The sessions will close immediately and the user will get an administrative close dialog.

Session close dialog

Conclusion

This relatively simple script shows the power of the Frame admin API to query the Frame control plane for account information and use REST mutations to instruct the platform to perform an action within the Frame account. For a full list of the account endpoints, check out the Frame documentation found here.

David Horvath is a senior solutions architect with Nutanix Frame. He has been a part of the Frame team for almost five years and prior to that spent 20 years consulting on various information technology projects with the U.S. intelligence community.

© 2022 Nutanix, Inc.  All rights reserved. Nutanix, the Nutanix logo and all Nutanix product, feature and service names mentioned herein are registered trademarks or trademarks of Nutanix, Inc. in the United States and other countries. Other brand names mentioned herein are for identification purposes only and may be the trademarks of their respective holder(s). This post may contain links to external websites that are not part of Nutanix.com. Nutanix does not control these sites and disclaims all responsibility for the content or accuracy of any external site. Our decision to link to an external site should not be considered an endorsement of any content on such a site. Certain information contained in this post may relate to or be based on studies, publications, surveys and other data obtained from third-party sources and our own internal estimates and research. While we believe these third-party studies, publications, surveys and other data are reliable as of the date of this post, they have not been independently verified, and we make no representation as to the adequacy, fairness, accuracy, or completeness of any information obtained from third-party sources.

This post may contain express and implied forward-looking statements, which are not historical facts and are instead based on our current expectations, estimates and beliefs. The accuracy of such statements involves risks and uncertainties and depends upon future events, including those that may be beyond our control, and actual results may differ materially and adversely from those anticipated or implied by such statements. Any forward-looking statements included herein speak only as of the date hereof and, except as required by law, we assume no obligation to update or otherwise revise any of such forward-looking statements to reflect subsequent events or circumstances.