using System; using System.Collections.Generic; using System.Management.Automation; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PSProxmox.Client; using PSProxmox.Models; using PSProxmox.Session; using PSProxmox.Utilities; namespace PSProxmox.Cmdlets { /// /// Gets roles from Proxmox VE. /// The Get-ProxmoxRole cmdlet retrieves roles from Proxmox VE. /// /// Get all roles /// $roles = Get-ProxmoxRole -Connection $connection /// /// /// Get a specific role by ID /// $role = Get-ProxmoxRole -Connection $connection -RoleID "Administrator" /// /// [Cmdlet(VerbsCommon.Get, "ProxmoxRole")] [OutputType(typeof(ProxmoxRole), typeof(string))] public class GetProxmoxRoleCmdlet : PSCmdlet { /// /// The connection to the Proxmox VE server. /// [Parameter(Mandatory = true, Position = 0)] public ProxmoxConnection Connection { get; set; } /// /// The ID of the role to retrieve. /// [Parameter(Mandatory = false)] public string RoleID { get; set; } /// /// Whether to return the raw JSON response. /// [Parameter(Mandatory = false)] public SwitchParameter RawJson { get; set; } /// /// Processes the cmdlet. /// protected override void ProcessRecord() { try { var client = new ProxmoxApiClient(Connection, this); string response; if (string.IsNullOrEmpty(RoleID)) { // Get all roles response = client.Get("access/roles"); var rolesData = JsonUtility.DeserializeResponse(response); var roles = new List(); foreach (var roleObj in rolesData) { var role = roleObj.ToObject(); roles.Add(role); } if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(roles, true); } } else { // Get a specific role response = client.Get($"access/roles/{Uri.EscapeDataString(RoleID)}"); var roleData = JsonUtility.DeserializeResponse(response); var role = roleData.ToObject(); if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(role); } } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "GetProxmoxRoleError", ErrorCategory.OperationStopped, Connection)); } } } }