How can I fix this problem? I'm trying to display powershell script output on a label. The PowerShell code working fine when I run it, but if I have to get the output to display nothing display only the error message what I write, If I change the script and put Write-Output(before get-aduser) and I run it 1601/01/01 00:00:00 this date appear 4x times(c# able to display this version)
Powershell code(display the password expiration date of the currently logged in user):
Get-ADUser -Filter "SamAccountName -eq '$env:username'" -Properties "msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property @{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} | ft ExpiryDate -HideTableHeaders
C#
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"C:\temp\passexp.ps1"));
ps.AddCommand("Out-String");
var result = ps.Invoke();
foreach (var item in result)
{
label5.Text = item.ToString();
}
if (ps.HadErrors==true)
{
label5.Text = "Error!";
}