Get-ADUser始终返回0

时间:2018-07-25 12:37:33

标签: c# powershell

我在PowerShell中有脚本:

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

,并且我尝试将其导入C#(WPF),但是result / result2始终为0。 PowerShell脚本运行良好,并返回信息。

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();
}

也尝试使用导入活动目录,但是结果是相同的:

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");

编辑: 尝试其他方法:

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

结果是:

result  Count = 1
result2 0

也许有一种方法可以从“ DirectoryEntry”或“ PrincipalContext”接收msDS-User-Account-Control-Compute吗?我使用这两个类从AD中获取信息,但无法访问它。

2 个答案:

答案 0 :(得分:0)

我认为您在要搜索的属性的参数中输入了错字:

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

应该是

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

我猜

编辑

您要更新/试用哪个版本?

我认为完整的应该是

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();
}

答案 1 :(得分:0)

这有效:

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