using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Security;
using PSProxmox.Client;
using PSProxmox.Models;
using PSProxmox.Session;
using PSProxmox.Utilities;
namespace PSProxmox.Cmdlets
{
///
/// Creates a new user in Proxmox VE.
/// The New-ProxmoxUser cmdlet creates a new user in Proxmox VE.
///
/// Create a new user
/// $user = New-ProxmoxUser -Connection $connection -Username "john" -Realm "pam" -Password $securePassword -FirstName "John" -LastName "Doe" -Email "john.doe@example.com"
///
///
[Cmdlet(VerbsCommon.New, "ProxmoxUser")]
[OutputType(typeof(ProxmoxUser))]
public class NewProxmoxUserCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The username.
///
[Parameter(Mandatory = true)]
public string Username { get; set; }
///
/// The realm.
///
[Parameter(Mandatory = true)]
public string Realm { get; set; }
///
/// The password.
///
[Parameter(Mandatory = true)]
public SecureString Password { get; set; }
///
/// The user's first name.
///
[Parameter(Mandatory = false)]
public string FirstName { get; set; }
///
/// The user's last name.
///
[Parameter(Mandatory = false)]
public string LastName { get; set; }
///
/// The user's email address.
///
[Parameter(Mandatory = false)]
public string Email { get; set; }
///
/// The user's comment.
///
[Parameter(Mandatory = false)]
public string Comment { get; set; }
///
/// The user's expiration date.
///
[Parameter(Mandatory = false)]
public DateTime? Expire { get; set; }
///
/// The user's groups.
///
[Parameter(Mandatory = false)]
public string[] Groups { get; set; }
///
/// Whether the user is enabled.
///
[Parameter(Mandatory = false)]
public SwitchParameter Enabled { get; set; } = true;
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Create the user
var parameters = new Dictionary
{
["userid"] = $"{Username}@{Realm}"
};
// Convert SecureString to plain text (only for sending to API)
string plainPassword = new System.Net.NetworkCredential(string.Empty, Password).Password;
parameters["password"] = plainPassword;
if (!string.IsNullOrEmpty(FirstName))
{
parameters["firstname"] = FirstName;
}
if (!string.IsNullOrEmpty(LastName))
{
parameters["lastname"] = LastName;
}
if (!string.IsNullOrEmpty(Email))
{
parameters["email"] = Email;
}
if (!string.IsNullOrEmpty(Comment))
{
parameters["comment"] = Comment;
}
if (Expire.HasValue)
{
parameters["expire"] = ((DateTimeOffset)Expire.Value).ToUnixTimeSeconds().ToString();
}
if (Groups != null && Groups.Length > 0)
{
parameters["groups"] = string.Join(",", Groups);
}
parameters["enable"] = Enabled.IsPresent ? "1" : "0";
// Create the user
client.Post("access/users", parameters);
// Get the created user
string userResponse = client.Get($"access/users/{Uri.EscapeDataString($"{Username}@{Realm}")}");
var user = JsonUtility.DeserializeResponse(userResponse);
WriteObject(user);
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "NewProxmoxUserError", ErrorCategory.OperationStopped, Connection));
}
}
}
}