【问题标题】:AD Delegated user password changeAD 委派用户密码更改
【发布时间】:2019-03-25 11:35:46
【问题描述】:

我正在尝试在 PowerShell 中编写代码,该代码将循环直到用户想要退出。因此,允许委派用户更改学生帐户的密码。

我已经测试了密码更改的代码,它没有任何问题。

只要我尝试添加一个 while 循环,代码就不会运行。

#Open powershell as Administrator
Start-process Powershell -verb RunAs

# Connect to the Domain Controller
$session = New-PSSession -ComputerName "" -Credential (Get-Credential)
Invoke-Command $session -Scriptblock { Import-Module ActiveDirectory }
Import-PSSession -session $session -Module ActiveDirectory

#verify that you're connectedto the Domain
Get-ADDomainController -Filter * | Select-Object name

#run Password Change
$userid = Read-Host -Prompt 'Please enter the studnets login ID'
$newPassword = Read-Host -Prompt 'Please enter the studnets new password'

$run = 'yes'
while ($run -eq 'yes')
    {
        if (Get-ADUser -Filter {SamAccountName -eq $userid})
        {
           Set-ADAccountPassword $userid -Reset -NewPassword (ConvertTo-SecureString -Force -AsPlainText '$newPassword')
           Write - Host "$userid` password has now been changed to: $newPassword"
        }
        else
        {
            Write - Host "$userid` does not exist, please try again."  
        }
        $answer = Read-Host -Prompt 'Would you like to change another studnets password? [y|n]'
        while ($answer -notcontains 'n' -or $answer -notcontains 'N' -or $answer -notcontains 'y' -or $answer -notcontains 'Y')
        {
             $answer = Read-Host -Prompt 'Please answer [y|n]'
        }
        if ($answer -eq 'y' -or $answer -eq 'Y')
        {
            $run = 'yes'
        }
        if ($answer-eq 'n' -or $answer -eq 'N')
        {
            exit
        }
    }

【问题讨论】:

    标签: powershell


    【解决方案1】:

    PowerShell 默认/设计不区分大小写。因此,无需检查。

    根据你所追求的,你可能过度设计了这个项目。

    我建议将其更改为简单的 Do..Until。

    Clear-Host
    do
    {
        "`n"
        $TargetUser = Read-Host -Prompt 'Enter a user SamAccountName'
        "Processing  user $TargetUser"
        Get-ADUser -Identity $TargetUser
        "`n"
        $answer = Read-Host -Prompt 'Would you like to see another user? [y|n]'    
    }
    until ($answer -eq 'n')
    
    "You entered $answer. Finished processing routine."
    
    Enter a user SamAccountName: Administrator
    Process user Administrator
    
    
    ...
    Enabled           : True
    GivenName         : 
    Name              : Administrator
    ...
    SamAccountName    : Administrator
    ...
    
    Would you like to see another user? [y|n]: y
    
    Enter a user SamAccountName: sqladmin
    Process user sqladmin
    
    ...
    Enabled           : True
    GivenName         : SqlAdmin
    Name              : SqlAdmin ServiceAccount
    ...
    SamAccountName    : sqladmin
    ...
    
    Would you like to see another user? [y|n]: n
    You entered n. Finished processing routine.
    

    就个人而言,如果是我这样做,我会以不同的方式处理。

    1. 在可以使用用户 SamAccountName 更新的共享上创建文件 / 用于目标更改的 ID。
    2. 在代码中读入那个文件。
    3. 在代码中,自动生成密码。
    4. 根据需要在代码中执行任何其他步骤。

    从而消除所有人工提示劳动。

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      相关资源
      最近更新 更多