简体   繁体   中英

Get-ADUser always returns 0

I have script in PowerShell:

Get-ADUser $Login -Properties msDS-UserPasswordExpiryTimeComputed |
    select -expand msDS-UserPasswordExpiryTimeComputed

and I'm trying to import it to C# (WPF), but result/result2 is always 0. PowerShell script works well and returns informations.

using (PowerShell PS = PowerShell.Create())
{
    PS.AddScript("Get - ADUser");
    PS.AddParameter("Identity", TextBox_UserLoginIn.Text);
    PS.AddParameter("Properties", "msDS-User-Account-Control-Computed");
    PS.AddStatement();
    PS.AddCommand("Select");
    PS.AddParameter("-expand", "msDS-UserPasswordExpiryTimeComputed");

    var result = PS.Invoke();

    long result2 = long.Parse(result.ToString());

    DateTime psdata = DateTime.FromFileTimeUtc(result2);
    _MetroWindow.TextBox_UserPassExpire.Text = psdata.ToString();
}

also trying with import activedirectory but result is the same:

PS.AddCommand("Import-Module").AddParameter("Name", "activedirectory");
PS.AddCommand("Get - ADUser").AddParameter("Identity", TextBox_UserLoginIn.Text);
PS.AddParameter("Properties", "msDS-User-Account-Control-Computed");
PS.AddStatement();
PS.AddParameter("-expand", "msDS-UserPasswordExpiryTimeComputed");

EDIT: Trying else:

PS.AddScript("import-module activedirectory");
PS.AddScript("Get-ADUser -Identity " + TextBox_UserLoginIn.Text + " -Properties msDS-UserPasswordExpiryTimeComputed | select -expand msDS-UserPasswordExpiryTimeComputed");

And result is:

result  Count = 1
result2 0

Maybe there is a way to receive msDS-User-Account-Control-Computed from "DirectoryEntry" or "PrincipalContext"? I use these 2 classes to take information from AD but I can't access to this.

I think you have made a typo in the parameter for the properties you seek:

PS.AddParameter("Properties", "msDS-User-Account-Control-Computed");

should be

PS.AddParameter("Properties", "msDS-UserPasswordExpiryTimeComputed");

i guess

EDIT

Which of the two versions are you updating/trying out?

I think the complete thing should be

using (PowerShell PS = PowerShell.Create())
{
    PS.AddCommand("Import-Module").AddParameter("Name", "activedirectory");
    PS.AddScript("Get-ADUser");
    PS.AddParameter("Identity", TextBox_UserLoginIn.Text);
    PS.AddParameter("Properties", "msDS-UserPasswordExpiryTimeComputed");
    PS.AddStatement();
    PS.AddCommand("Select");
    PS.AddParameter("-expand", "msDS-UserPasswordExpiryTimeComputed");

    var result = PS.Invoke();

    long result2 = long.Parse(result.ToString());

    DateTime psdata = DateTime.FromFileTimeUtc(result2);
    _MetroWindow.TextBox_UserPassExpire.Text = psdata.ToString();
}

这有效:

PS.AddScript("Get-ADUser " + TextBox_UserLoginIn.Text + " -Properties msDS-UserPasswordExpiryTimeComputed | Select -Expand \"msDS-UserPasswordExpiryTimeComputed\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM