This blog is mainly about how to retrieve the CSES configuration via PowerShell and REST API. It will cover the following sections:
No matter PowerShell command or Rest API request will be used to get the information, the PowerShell Azure Az module is necessary. For the installation details, please refer to this document.
To use Get-AzCloudService (Az.CloudService) | Microsoft Learn to get the CSES configuration data, we can follow these steps:
The used commands will be:
Connect-AzAccount
$cses = Get-AzCloudService -ResourceGroupName “xxx” -CloudServiceName “xxx”
[xml]$xml = $cses.Configuration
Example of PowerShell command
To use Cloud Services - Get - REST API (Azure Compute) | Microsoft Learn to get the CSES configuration data by sending out REST API call in PowerShell, we can follow these steps:
The used commands will be:
Connect-AzAccount
$csesapi = (Invoke-AzRestMethod -Path "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Compute/cloudServices/{CSES-resource-name}?api-version=2021-03-01").Content | convertfrom-json
[xml]$xml = $csesapi.properties.configuration
Example of REST API request
No matter PowerShell command or REST API is used, with above instruction, the $xml from both ways will be the same.
Basically, this is how my example CSES configuration data looks like:
Example of configuration data of CSES
The $xml is the whole configuration file in XML format. In order to get the data, such as osFamily, osVersion or VirtualNetworkSite, following the construction of this XML file to add related name to identify the data of which level is needed will be enough.
For example, for osVersion/osFamily, the path to it will be ServiceConfiguration -> osVersion/osFamily. So the expression to use in PowerShell to get the data will be:
$xml.ServiceConfiguration.osVersion / $xml.ServiceConfiguration.osFamily
Example expression for osFamily/osVersion
And for the VirtualNetworkSite, the path to it will be ServiceConfiguration -> NetworkConfiguration -> VirtualNetworkSite -> name. So the expression to use in PowerShell to get the data will be:
$xml.ServiceConfiguration.NetworkConfiguration.VirtualNetworkSite.name
Example expression of Virtual Network name
Note: It’s possible that we have more than one role in our configuration, the expression to locate a role which is not the first role will be $xml.ServiceConfiguration.Role[1]. The number “1” here means the second role in the configuration data because this counter starts from 0. If there are two roles in same configuration data, the expression $xml.ServiceConfiguration.Role[1].Instances.count will be able to return how many instances there are for the second role.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.