I am stuck with a simple task and need community help, please. I'm running a PowerShell script in a C# code and need to convert the output to a dictionary. The code is
private void button_datecheck_Click(object sender, EventArgs e)
{
var script = "Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties \"DisplayName\", \"msDS-UserPasswordExpiryTimeComputed\" | Select-Object -Property \"Displayname\",@{Name=\"ExpiryDate\";Expression={[datetime]::FromFileTime($_.\"msDS-UserPasswordExpiryTimeComputed\")}}";
var ps = PowerShell.Create().AddScript(script);
var temp_list = ps.Invoke().ToList();
using (TextWriter tw = new StreamWriter("C:\\c#\\test_output.txt"))
{
foreach (dynamic item in temp_list)
{
tw.WriteLine(item);
}
}
Not sure if PSObject can be directly converted to a dictionary, so converted it to a List first (Also, to have a look at what kind of output I do have from PS) Output looks like
@{Displayname=Name Surname; ExpiryDate=1/02/2022 9:24:48 am} @{Displayname=Name1 Surname1; ExpiryDate=2/02/2023 11:20:27 am}
And here I am stuck, do not know how to convert it to a dictionary or maybe there is a way to convert it directly to a dictionary without using lists?
Thanks for any help