使用Powershell从AD获取AllowLogon不会';不要总是显示正确的信息

使用Powershell从AD获取AllowLogon不会';不要总是显示正确的信息,powershell,powershell-2.0,powershell-4.0,powershell-remoting,Powershell,Powershell 2.0,Powershell 4.0,Powershell Remoting,我有一个脚本,它使用一个带有用户SamAccountName的输入文件。它运行脚本并更新用户配置文件以选中 “拒绝此用户登录远程桌面会话主机服务器的权限”。 一旦设置了值,我还想读取它,以确保它被正确更改 对大多数用户来说,它是有效的,但对一些用户来说,它会说它改变了,但实际上并没有改变 $ADUsers = Get-Content "C:\PSTOOLS\Passwordneverexpire\disable-enbale deny logon.txt" ForEa

我有一个脚本,它使用一个带有用户SamAccountName的输入文件。它运行脚本并更新用户配置文件以选中 “拒绝此用户登录远程桌面会话主机服务器的权限”。 一旦设置了值,我还想读取它,以确保它被正确更改

对大多数用户来说,它是有效的,但对一些用户来说,它会说它改变了,但实际上并没有改变

   $ADUsers = Get-Content "C:\PSTOOLS\Passwordneverexpire\disable-enbale deny logon.txt"

ForEach ($User in $ADUsers) {
    $UserDN = (Get-ADUser -identity $User).distinguishedName
    if ($UserDN) {
        $Command = [ADSI] "LDAP://$UserDN"
        $Command.psbase.invokeSet("AllowLogon",0) # it will NOT allow remote logon
        $Command.setinfo()
        Write-Host $User + "Allow logon " + $Command.psbase.InvokeGet("AllowLogon") -as [Boolean]
    }
    else {
        Write-Host "$UserDN Does Not Exist"
    }
}

这可能是因为它在列表中的运行速度有多快。尝试添加一个
start sleep
。我认为这不会修复它,因为如果我们为一个帐户运行它,仍然会显示错误的结果。在使用另一个
命令=[ADSI]“LDAP://$UserDN”
进行设置后,您需要刷新用户数据。您好,您的命令被切断了。我看不到全文。我认为在运行
.SetInfo()
之后,您不需要刷新
$Command
对象。我无法重现你的问题。我假设如果对象需要刷新,那么问题只会是可视的、一致的,而不是真实的、断断续续的。你还没有解释为什么你认为它不起作用。