namespace PS.IPAM.Cmdlets; using System; using System.Management.Automation; using System.Threading.Tasks; using PS.IPAM; using PS.IPAM.Helpers; [Cmdlet(VerbsCommon.New, "Session", DefaultParameterSetName = "Credentials")] public class NewSessionCmdlet : PSCmdlet { [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)] [ValidateNotNullOrEmpty] [ValidatePattern("^https?://")] public string URL { get; set; } = string.Empty; [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 1)] [ValidateNotNullOrEmpty] public string AppID { get; set; } = string.Empty; [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 2, ParameterSetName = "Credentials")] [ValidateNotNullOrEmpty] public PSCredential? Credentials { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 2, ParameterSetName = "Token")] [ValidateNotNullOrEmpty] public string? Token { get; set; } [Parameter( Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 3)] public SwitchParameter IgnoreSSL { get; set; } protected override void ProcessRecord() { try { Session session; if (ParameterSetName == "Credentials" && Credentials != null) { session = SessionManager.CreateSessionWithCredentials( URL, AppID, Credentials, IgnoreSSL.IsPresent ).GetAwaiter().GetResult(); } else if (ParameterSetName == "Token" && Token != null) { session = SessionManager.CreateSessionWithToken(URL, AppID, Token); } else { throw new ArgumentException("Invalid parameter set"); } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "NewSessionError", ErrorCategory.InvalidOperation, null)); } } }