using System;
using System.Management.Automation;
using PSProxmox.Client;
using PSProxmox.Session;
namespace PSProxmox.Cmdlets
{
///
/// Removes a user from Proxmox VE.
/// The Remove-ProxmoxUser cmdlet removes a user from Proxmox VE.
///
/// Remove a user
/// Remove-ProxmoxUser -Connection $connection -UserID "john@pam"
///
///
/// Remove a user using pipeline input
/// Get-ProxmoxUser -Connection $connection -UserID "john@pam" | Remove-ProxmoxUser -Connection $connection
///
///
[Cmdlet(VerbsCommon.Remove, "ProxmoxUser", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class RemoveProxmoxUserCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The ID of the user to remove.
///
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)]
public string UserID { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Confirm removal
if (!ShouldProcess($"User {UserID}", "Remove"))
{
return;
}
// Remove the user
WriteVerbose($"Removing user {UserID}");
client.Delete($"access/users/{Uri.EscapeDataString(UserID)}");
WriteVerbose($"User {UserID} removed");
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "RemoveProxmoxUserError", ErrorCategory.OperationStopped, UserID));
}
}
}
}