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 users from Proxmox VE. /// The Get-ProxmoxUser cmdlet retrieves users from Proxmox VE. /// /// Get all users /// $users = Get-ProxmoxUser -Connection $connection /// /// /// Get a specific user by ID /// $user = Get-ProxmoxUser -Connection $connection -UserID "root@pam" /// /// [Cmdlet(VerbsCommon.Get, "ProxmoxUser")] [OutputType(typeof(ProxmoxUser), typeof(string))] public class GetProxmoxUserCmdlet : PSCmdlet { /// /// The connection to the Proxmox VE server. /// [Parameter(Mandatory = true, Position = 0)] public ProxmoxConnection Connection { get; set; } /// /// The ID of the user to retrieve. /// [Parameter(Mandatory = false)] public string UserID { 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(UserID)) { // Get all users response = client.Get("access/users"); var usersData = JsonUtility.DeserializeResponse(response); var users = new List(); foreach (var userObj in usersData) { var user = userObj.ToObject(); users.Add(user); } if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(users, true); } } else { // Get a specific user response = client.Get($"access/users/{Uri.EscapeDataString(UserID)}"); var userData = JsonUtility.DeserializeResponse(response); var user = userData.ToObject(); if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(user); } } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "GetProxmoxUserError", ErrorCategory.OperationStopped, Connection)); } } } }