2024-08-05 18:02:01 -05:00
using LANCommander.SDK ;
using LANCommander.SDK.Helpers ;
using System.Management.Automation ;
namespace LANCommander.SDK.PowerShell.Cmdlets
{
[Cmdlet(VerbsData.ConvertTo, "StringBytes")]
[OutputType(typeof(byte[] ) ) ]
2026-01-24 16:37:07 -06:00
public class ConvertToStringBytesCmdlet : Cmdlet
2024-08-05 18:02:01 -05:00
{
2026-05-08 02:02:33 -05:00
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The string to convert to a byte array.")]
2024-08-05 18:02:01 -05:00
public string Input { get ; set ; }
2026-05-08 02:02:33 -05:00
[Parameter(HelpMessage = "Encode as UTF-16 instead of ASCII. Use with -BigEndian for big-endian UTF-16.")]
2024-08-05 18:02:01 -05:00
public bool Utf16 { get ; set ; } = false ;
2026-05-08 02:02:33 -05:00
[Parameter(HelpMessage = "Use big-endian byte order for UTF-16 encoding. Only applies when -Utf16 is set.")]
2024-08-05 18:02:01 -05:00
public bool BigEndian { get ; set ; } = false ;
2026-05-08 02:02:33 -05:00
[Parameter(HelpMessage = "Maximum number of characters to include. The string will be truncated if longer. Set to 0 for no limit.")]
2024-08-05 18:02:01 -05:00
public int MaxLength { get ; set ; } = 0 ;
2026-05-08 02:02:33 -05:00
[Parameter(HelpMessage = "Minimum number of characters in the output. The string will be right-padded with null characters if shorter. Set to 0 for no padding.")]
2024-08-05 18:02:01 -05:00
public int MinLength { get ; set ; } = 0 ;
protected override void ProcessRecord ( )
{
byte [ ] output ;
if ( MaxLength > 0 & & Input . Length > MaxLength )
Input = Input . Substring ( 0 , MaxLength ) ;
if ( MinLength > 0 & & MinLength < MaxLength )
Input = Input . PadRight ( MinLength , '\0' ) ;
else if ( MinLength > 0 )
Input = Input . PadRight ( MaxLength , '\0' ) ;
if ( Utf16 & & BigEndian )
output = System . Text . Encoding . BigEndianUnicode . GetBytes ( Input ) ;
else if ( Utf16 )
output = System . Text . Encoding . Unicode . GetBytes ( Input ) ;
else
output = System . Text . Encoding . ASCII . GetBytes ( Input ) ;
WriteObject ( output ) ;
}
}
}