From 4bcdf372d4bb07aab295200e1c5f7334f6d00d06 Mon Sep 17 00:00:00 2001 From: GraceSolutions Date: Thu, 4 Jun 2026 17:42:34 -0400 Subject: [PATCH] Add Export-InfisicalScepMdmProfile cmdlet Writes the SyncML payload from InfisicalScepMdmProfile.ToSyncMl() to disk as UTF-8 (no BOM). Honors -WhatIf, auto-creates the target directory, and follows the project rule for -Force: if the file exists without -Force, logs a warning and returns instead of throwing. Optional -PassThru emits the resulting FileInfo. --- .../ExportInfisicalScepMdmProfileCmdlet.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/PSInfisicalAPI/Cmdlets/ExportInfisicalScepMdmProfileCmdlet.cs diff --git a/src/PSInfisicalAPI/Cmdlets/ExportInfisicalScepMdmProfileCmdlet.cs b/src/PSInfisicalAPI/Cmdlets/ExportInfisicalScepMdmProfileCmdlet.cs new file mode 100644 index 0000000..3daef68 --- /dev/null +++ b/src/PSInfisicalAPI/Cmdlets/ExportInfisicalScepMdmProfileCmdlet.cs @@ -0,0 +1,65 @@ +using System; +using System.IO; +using System.Management.Automation; +using System.Text; +using PSInfisicalAPI.Models; + +namespace PSInfisicalAPI.Cmdlets +{ + [Cmdlet(VerbsData.Export, "InfisicalScepMdmProfile", SupportsShouldProcess = true)] + [OutputType(typeof(FileInfo))] + public sealed class ExportInfisicalScepMdmProfileCmdlet : InfisicalCmdletBase + { + private const string Component = "ExportInfisicalScepMdmProfileCmdlet"; + + [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)] + [Alias("Profile", "ScepProfile")] + public InfisicalScepMdmProfile InputObject { get; set; } + + [Parameter(Mandatory = true, Position = 1)] + public string Path { get; set; } + + [Parameter] public SwitchParameter Force { get; set; } + [Parameter] public SwitchParameter PassThru { get; set; } + + protected override void ProcessRecord() + { + try + { + if (InputObject == null) { throw new InvalidOperationException("InputObject is required."); } + + string resolvedPath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(Path); + if (File.Exists(resolvedPath) && !Force.IsPresent) + { + Logger.Warning(Component, string.Concat("File '", resolvedPath, "' already exists. Pass -Force to overwrite. Skipping export.")); + return; + } + + if (!ShouldProcess(resolvedPath, "Write SyncML SCEP MDM profile")) + { + return; + } + + string directory = System.IO.Path.GetDirectoryName(resolvedPath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + Logger.Verbose(Component, string.Concat("Created directory '", directory, "'.")); + } + + string syncMl = InputObject.ToSyncMl(); + File.WriteAllText(resolvedPath, syncMl, new UTF8Encoding(false)); + + Logger.Information(Component, string.Concat("Wrote SCEP MDM profile to '", resolvedPath, "' (UniqueId=", InputObject.UniqueId, ").")); + if (PassThru.IsPresent) + { + WriteObject(new FileInfo(resolvedPath)); + } + } + catch (Exception exception) + { + ThrowTerminatingForException(Component, "ExportScepMdmProfile", exception); + } + } + } +}