Skip to content

Instantly share code, notes, and snippets.

@Calvindd2f
Last active March 17, 2024 15:17
Show Gist options
  • Select an option

  • Save Calvindd2f/90ee6e7a15910c81b7150f40700905aa to your computer and use it in GitHub Desktop.

Select an option

Save Calvindd2f/90ee6e7a15910c81b7150f40700905aa to your computer and use it in GitHub Desktop.
CreatingPowershellHashtable.ps1
ConvertFrom-StringData @'
###PSLOC
Name=Value
Ass=TestX
InspectingModules=Inspecting Modules
DiagnosticSearch=Searching for Diagnostics in {0}
InvokingTest=Invoking tests in {0}
###PSLOC
'@
void ConvertFrom-PS1XML(System.Object $input)
{
if (string.IsNullOrEmpty(input))
{
string.IsNullOrEmpty(input);
}
ps1xmlsnippet = " $input";
xmlObject = (xml)ps1xmlSnippet;
jsonObject = xmlObject.using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
{
powershell.AddCommand("ConvertTo-Json");
powershell.AddArgument(10);
powershell.Invoke();
}
jsonObject;
using System;
using System.Management.Automation;
public class Converter
{
public void ConvertFromPS1XML(object input)
{
if (input == null || string.IsNullOrEmpty(input.ToString()))
{
Console.WriteLine("Input is null or empty.");
return;
}
// Assuming input is an XML string for simplification
string ps1xmlSnippet = input.ToString();
// Using PowerShell to convert XML to JSON
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.AddScript($"ConvertTo-Json -InputObject {ps1xmlSnippet}");
var results = powerShell.Invoke();
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment