简体   繁体   English

Get-ADUser始终返回0

[英]Get-ADUser always returns 0

I have script in PowerShell: 我在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. 并且我正尝试将其导入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();
}

also trying with import activedirectory but result is the same: 也尝试导入activedirectory,但结果是相同的:

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"? 也许有一种方法可以从“ DirectoryEntry”或“ PrincipalContext”接收msDS-User-Account-Control-Compute吗? I use these 2 classes to take information from AD but I can't access to this. 我使用这两个类从AD中获取信息,但无法访问它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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