【问题标题】:Using credentials with ADSI to reset a remote local computer account password使用 ADSI 凭据重置远程本地计算机帐户密码
【发布时间】:2019-10-09 14:01:24
【问题描述】:

尝试通过 PowerShell 使用显式凭据更改远程 Windows 密码。

我已经能够使用 ADSI 界面完成这项工作,但脚本是从非提升帐户运行的,因此没有权限。我需要向用户询问提升的帐户,然后在建立连接时使用该帐户。

这行得通:

$server = "Server1"
$adminID = "Administrator"
$Password = "NewPassword"
$OldPassword = "OldPassword"

([ADSI] "WinNT://$server/$adminID").SetPassword($Password)

但是,我还需要包含该帐户的旧密码,以便我有权进行更改。

有没有办法以$AdminD$OldPassword 建立与服务器的连接,然后将其更改为$NewPassword

【问题讨论】:

标签: powershell adsi


【解决方案1】:

找到了解决办法。

        $credential = New-Object System.Management.Automation.PSCredential($($server + "\" +$AdminID),$Oldpassword)

        $pth = "\\$($server)\admin$"

        write-host "Making connection to server with creds." -nonewline

            # test for the drive, and remove if present.    

        if (![bool]([string]::IsNullOrEmpty($(get-psdrive x -ErrorAction SilentlyContinue).root)))
        {
            remove-PSDrive -Name X
            write-host "Drive unmapped," -nonewline
        }

        New-PSDrive -Name X -PSProvider filesystem  -Root $pth -Credential $credential | out-null
        write-host " PS Drive established.. " -nonewline


        ([ADSI] "WinNT://$server/$adminID").SetPassword($Password)


通过首先建立与服务器的连接,第二次重置密码的调用使用已建立的相同凭据。

完成后,我调用以下命令断开驱动器..

    # test for the drive, and remove if present.    
    if (![bool]([string]::IsNullOrEmpty($(get-psdrive x -ErrorAction SilentlyContinue).root)))
    {
        remove-PSDrive -Name X
        write-host "Drive unmapped."
    }

如果有更好的方法,我很想知道它是什么。但这至少似乎有效。

【讨论】:

    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    相关资源
    最近更新 更多