C# Get ADUser始终返回0

C# Get ADUser始终返回0,c#,powershell,C#,Powershell,我在PowerShell中有脚本: 获取ADUser$Login-Properties msDS UserPasswordExpiryTimeComputed| 选择-展开msDS UserPasswordExpiryTimeComputed 我试图将其导入到C WPF,但result/result2始终为0。 PowerShell脚本运行良好并返回信息 使用PowerShell PS=PowerShell.Create { PS.AddScriptGet-ADUser; PS.AddParam

我在PowerShell中有脚本:

获取ADUser$Login-Properties msDS UserPasswordExpiryTimeComputed| 选择-展开msDS UserPasswordExpiryTimeComputed 我试图将其导入到C WPF,但result/result2始终为0。 PowerShell脚本运行良好并返回信息

使用PowerShell PS=PowerShell.Create { PS.AddScriptGet-ADUser; PS.AddParameterIdentity,TextBox_userlogin.Text; PS.AddParameterProperties,计算msDS用户帐户控制; PS.AddStatement; PS.AddCommandSelect; PS.AddParameter-expand,msDS UserPasswordExpiryTimeComputed; var result=PS.Invoke; long result2=long.Parseresult.ToString; DateTime psdata=DateTime.FromFileTimeUtcresult2; _MetroWindow.TextBox\u UserPassExpire.Text=psdata.ToString; } 也尝试导入activedirectory,但结果相同:

PS.AddCommandImport-Module.AddParameterName,activedirectory; PS.AddCommandGet-ADUser.AddParameterIdentity,TextBox_userlogin.Text; PS.AddParameterProperties,计算msDS用户帐户控制; 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用户帐户控制?我使用这两个类从AD获取信息,但我无法访问该类。

我认为您在所需属性的参数中键入了一个错误:

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();
}
这项工作:

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

很遗憾,没有。仍然返回0。我还在PS.AddScriptGet-ADUser;中看到空格;。Get ADUserOh true中不应有空格。不知怎么的,我没有注意到,但什么都没有改变。第一版。result Count=0 result2 0Remove.AddStatement,它们是一个管道