-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
VALDINGER
committed
Jan 9, 2019
0 parents
commit 6897382
Showing
30 changed files
with
4,378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Management.Automation; | ||
using System; | ||
|
||
namespace Base64 | ||
{ | ||
[Cmdlet(VerbsData.ConvertTo, "Base64String")] | ||
[OutputType(typeof(string))] | ||
public class ConvertToBase64Command : Cmdlet | ||
{ | ||
// Declare the parameters for the cmdlet. | ||
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)] | ||
[Alias("InputString","String","Text")] | ||
public string InputObject { get; set; } | ||
|
||
|
||
[Parameter] | ||
[ArgumentCompleter(typeof(EncodingCompleter))] | ||
[EncodingTransform] | ||
public System.Text.Encoding Encoding { get; set; } = EncodingTransformAttribute.StandardEncoding["UTF8"]; | ||
|
||
|
||
//This is the "Process {}" block of a Powershell script | ||
protected override void ProcessRecord() | ||
{ | ||
//WriteObject("Hello " + name + "!"); | ||
|
||
byte[] bytes = Encoding.GetBytes(InputObject); | ||
WriteObject(Convert.ToBase64String(bytes), true); | ||
|
||
} | ||
} //class | ||
|
||
[Cmdlet(VerbsData.ConvertFrom, "Base64String")] | ||
[OutputType(typeof(string))] | ||
public class ConvertFromBase64Command : Cmdlet | ||
{ | ||
// Declare the parameters for the cmdlet. | ||
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)] | ||
[Alias("InputString","String","Text")] | ||
public string InputObject { get; set; } | ||
|
||
|
||
[Parameter] | ||
[ArgumentCompleter(typeof(EncodingCompleter))] | ||
[EncodingTransform] | ||
public System.Text.Encoding Encoding { get; set; } = EncodingTransformAttribute.StandardEncoding["UTF8"]; | ||
|
||
|
||
//This is the "Process {}" block of a Powershell script | ||
protected override void ProcessRecord() | ||
{ | ||
//WriteObject("Hello " + name + "!"); | ||
// $Encoding.GetString([System.Convert]::FromBase64String($Base64String)) | ||
byte[] bytes = Convert.FromBase64String(InputObject); | ||
WriteObject(Encoding.GetString(bytes), true); | ||
} | ||
} //class | ||
} //namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.Management.Automation; | ||
using System.Management.Automation.Language; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System; | ||
|
||
namespace Base64 | ||
{ | ||
class EncodingTransformAttribute : ArgumentTransformationAttribute | ||
{ | ||
public EncodingTransformAttribute() { } | ||
internal static Dictionary<string, Encoding> StandardEncoding = new Dictionary<string, Encoding>() | ||
{ | ||
{ "ASCII", Encoding.ASCII }, | ||
{ "UnicodeBE", Encoding.BigEndianUnicode }, | ||
{ "Unicode", Encoding.Unicode }, | ||
{ "UTF32", Encoding.UTF32 }, | ||
{ "UTF7", Encoding.UTF7 }, | ||
{ "UTF8", new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) }, | ||
{ "UTF8BOM", Encoding.UTF8 } | ||
}; | ||
|
||
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData) | ||
{ | ||
switch (inputData) | ||
{ | ||
case Encoding e: | ||
return e; | ||
case string s: | ||
if (StandardEncoding.ContainsKey(s)) | ||
{ | ||
return StandardEncoding[s]; | ||
} | ||
else | ||
{ | ||
return Encoding.GetEncoding(s); | ||
} | ||
default: | ||
throw new ArgumentTransformationMetadataException(); | ||
} | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return "[EncodingTransformAttribute]"; | ||
} | ||
} | ||
|
||
class EncodingCompleter : IArgumentCompleter | ||
{ | ||
public EncodingCompleter() { } | ||
public IEnumerable<CompletionResult> CompleteArgument( | ||
string commandName, | ||
string parameterName, | ||
string wordToComplete, | ||
CommandAst commandAst, | ||
IDictionary fakeBoundParameters) | ||
{ | ||
List<string> encodingValues = new List<string>(EncodingTransformAttribute.StandardEncoding.Keys); | ||
|
||
if (!string.IsNullOrEmpty(wordToComplete)) | ||
{ | ||
foreach (string encoding in encodingValues) | ||
{ | ||
yield return new CompletionResult( | ||
completionText: encoding, | ||
listItemText: encoding, | ||
resultType: CompletionResultType.ParameterValue, | ||
toolTip: string.Format("{0} {1}", "[Encoding]", encoding)); | ||
} | ||
} | ||
else | ||
{ | ||
var filteredList = encodingValues.FindAll(x => x.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase)); | ||
foreach (string result in filteredList) | ||
{ | ||
yield return new CompletionResult( | ||
completionText: result, | ||
listItemText: result, | ||
resultType: CompletionResultType.ParameterValue, | ||
toolTip: string.Format("{0} {1}", "[Encoding]", result)); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.