当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 管理 Active Directory 中的计算机

作者:精品下载站 日期:2024-12-14 06:14:42 浏览:15 分类:玩电脑

如何使用 PowerShell 管理 Active Directory 中的计算机


在用户可以登录计算机并访问网络和基于域的资源之前,该计算机必须是 Active Directory 环境的成员。在本指南中,您将了解如何自动执行与计算机帐户相关的日常任务,例如如何轻松创建、重命名和删除帐户。

总之,您将学习如何使用 PowerShell 执行以下计算机帐户管理任务:

  • 将计算机加入域

    • 将多台计算机加入域
  • 使用 PowerShell 从域中删除计算机
  • 在 AD 中创建计算机对象

    • 从 CSV 文件创建计算机帐户
  • 从 AD 中删除计算机

    • 使用列表删除计算机帐户
  • 使用 PowerShell 删除 Active Directory 中的过时计算机帐户
  • 重命名计算机

    • 重命名计算机并将其加入域
  • 禁用 AD 计算机帐户

    • 使用列表禁用计算机帐户
  • 重置 AD 计算机帐户
  • PowerShell ISE 是处理 PowerShell 脚本的最佳工具。按“Windows+R”并在“运行”窗口中输入“runas /profile /user:Administrator PowerShell_ISE”,以管理员权限启动 PowerShell ISE 工具。 (或者,您可以右键单击 PowerShell ISE 图标并选择“以管理员身份运行”选项。)出现提示时输入管理员密码。

    在使用 AD 及其对象之前,您需要导入 Windows PowerShell 的 Active Directory 模块。在 Microsoft Windows Server 2008 R2 中,您需要通过运行以下命令来启用此模块:

    Import-Module ActiveDirectory

    在 Microsoft Windows Server 2012 及更高版本中,默认启用此模块。

    将计算机加入域

    最常见的任务是将计算机连接到域控制器。要将 PC 加入 Active Directory 域,请在本地运行以下 PowerShell 脚本:

    $dc = "ENTERPRISE" # Specify the domain to join.
    $pw = "Password123" | ConvertTo-SecureString -asPlainText –Force # Specify the password for the domain admin.
    $usr = "$dcT.Simpson" # Specify the domain admin account.
    $creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
    Add-Computer -DomainName $dc -Credential $creds -restart -force -verbose # Note that the computer will be restarted automatically.

    计算机将重新启动,然后加入域;它将被添加到默认容器中。

    要将计算机远程连接到 DC,您需要通过以下方式增强此脚本:

    $dc = "ENTERPRISE"
    $pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
    $usr = "$dcT.Simpson"
    $pc = "R07GF" # Specify the computer that should be joined to the domain.
    $creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
    Add-Computer -ComputerName $pc -LocalCredential $pcadmin -DomainName $dc -Credential $creds -Verbose -Restart -Force

    $pc 变量和 -LocalCredential 参数用于向域验证计算机。请注意,为了使用此方法,您必须禁用本地计算机上的防火墙。

    将多台计算机加入域

    您可以通过在命令行中将多台计算机指定为逗号分隔列表或从文本文件导入其名称来将多台计算机添加到域中。

    以下是如何在逗号分隔列表中指定计算机:

    $dc = "ENTERPRISE"
    $pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
    $usr = "$dcT.Simpson"
    $pc = "WKS034, WKS052, WKS057" # Specify the computers that should be joined to the domain.
    $creds = New-Object System.Management.Automation.PSCredential($usr$pw)
    Add-Computer -ComputerName $pc -LocalCredential $pcadmin -DomainName $dc -Credential $creds -Restart -Force

    以下是如何使用包含应加入的计算机列表的文本文件:

    $dc = "ENTERPRISE"
    $pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
    $usr = "$dcT.Simpson"
    $pc = Get-Content -Path C:Computers.txt # Specify the path to the computers list.
    $creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
    Add-Computer -ComputerName $pc -LocalCredential $pcadmin -DomainName $dc -Credential $creds -Restart -Force

    使用 PowerShell 从域中删除计算机

    要从域中远程删除计算机,请使用 Remove-Computer cmdlet。在这里,我们将从域中删除计算机,因此不需要本地凭据,并且可以跳过 ?LocalCredential 参数:

    $dc = "ENTERPRISE"
    $pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
    $usr = "$dcT.Simpson"
    $pc = "R07GF"
    $creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
    Remove-Computer -ComputerName $pc -Credential $creds –Verbose –Restart –Force

    [玩转系统] 如何使用 PowerShell 管理 Active Directory 中的计算机

    要使用 TXT 文件中的列表删除多台计算机,请使用上述脚本将计算机加入 DC,并将 Add-Computer cmdlet 替换为 Remove-Computer。请注意,您仍然需要域管理员凭据才能完成此取消加入操作。

    在 AD 中创建计算机对象

    要创建计算机对象,请使用New-ADComputer cmdlet。例如,执行以下 cmdlet 参数来创建名称为“WKS932”且默认 LDAP 路径值的计算机对象:

    New-ADComputer –Name “WKS932” –SamAccountName “WKS932”

    从 CSV 文件创建计算机帐户

    如果您有应导入 Active Directory 的计算机列表,请将该列表保存到 CSV 文件,其标题为“计算机”,计算机名称列表位于其下方的列中。在域控制器上运行以下 PowerShell 脚本以从 CSV 文件添加计算机,确保正确设置“路径”和“文件”变量:

    $File="C:scriptsComputers.csv" # Specify the import CSV position.
    $Path="OU=Devices,DC=enterprise,DC=com" # Specify the path to the OU.
    Import-Csv -Path $File | ForEach-Object { New-ADComputer -Name $_.Computer -Path $Path -Enabled $True}

    从 AD 中删除计算机

    要从 AD 中删除计算机帐户,请使用 Remove-ADObject cmdlet。 -Identity 参数指定要删除的 Active Directory 计算机。您可以通过计算机的专有名称、GUID、安全标识符 (SID) 或安全帐户管理器 (SAM) 帐户名称来指定计算机。

    Remove-ADObject -Identity "WKS932"

    系统将提示您确认删除。

    使用列表删除计算机帐户

    如果您有一个包含旧计算机列表的文本文件,则可以使用 PowerShell 简化删除它们的任务。以下脚本将从 TXT 文件中读取计算机名称,并通过一系列命令或管道删除相应的帐户:

    Get-Content C:scriptscomputersfordeletion.txt | % { Get-ADComputer -Filter { Name -eq $_ } } | Remove-ADObject -Recursive

    使用 PowerShell 从 Active Directory 中删除过时的计算机帐户

    Active Directory 中的过时帐户可能会受到损害,从而导致安全事件,因此密切关注它们至关重要。此 PowerShell 脚本将查询 Active Directory 并返回过去 30 天内未登录的所有计算机;您可以轻松地在脚本中更改此默认值。它还将删除这些帐户以保持您的 AD 干净。

    $stale = (Get-Date).AddDays(-30) # means 30 days since last logon, can be changed to any number.
    
    Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $stale} | FT Name,lastLogonDate
    
    Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $stale} | Remove-ADComputer

    [玩转系统] 如何使用 PowerShell 管理 Active Directory 中的计算机

    有一台计算机 FS1,已超过 30 天没有登录。从域中删除之前系统会提示确认:

    [玩转系统] 如何使用 PowerShell 管理 Active Directory 中的计算机

    如果您想要禁用而不是删除不活动的计算机帐户,请将 Remove-ADComputer cmdlet 替换为 Set-ADComputer -Enabled $false 参数和值。

    重命名计算机

    要更改计算机名称,请使用Rename-Computer cmdlet。请注意,计算机必须在线并连接到 Active Directory。

    Rename-Computer –ComputerName "FS1" -NewName "FS2"

    如果您想在本地运行此脚本,它将如下所示:

    Rename-Computer -NewName "newname" -DomainCredential "DomainAdministrator"

    重命名计算机并将其加入域

    您可以通过将计算机加入域并同时将其放入指定的 OU 来改进重命名脚本。该脚本应在目标计算机上运行,而不是在域控制器上运行。

    $NewComputerName = "Server3" # Specify the new computer name.
    
    $DC = "contoso.com" # Specify the domain to join.
    
    $Path = "OU=TestOU,DC=contoso,DC=com" # Specify the path to the OU where to put the computer account in the domain.
    Add-Computer -DomainName $DC -OUPath $Path -NewName $NewComputerName –Restart –Force

    该脚本将提示输入有权将计算机加入域的帐户的凭据,然后计算机将被重命名、重新启动并加入域。

    禁用 AD 计算机帐户

    使用Disable-ADAccount cmdlet 禁用 Active Directory 用户、计算机和服务帐户。如果指定计算机帐户名称,请记住在名称末尾附加美元符号 ($);否则,脚本执行后会出现错误。

    Disable-ADAccount -Identity fs1$

    使用列表禁用计算机帐户

    您还可以使用文本文件中的列表批量禁用计算机帐户:

    $Pclist = Get-Content C:scriptsComputer.txt # Specify the path to the computers list.
    Foreach($pc in $Pclist)
    {
    Disable-ADAccount -Identity "$pc"
    Get-ADComputer -Identity "$pc" | Move-ADObject -TargetPath “OU=Disabled Computers,DC=enterprise,DC=com”
    }

    重置 AD 计算机帐户

    与用户帐户一样,计算机帐户使用密码与 Active Directory 进行交互。但对于计算机帐户,默认情况下每 30 天启动一次密码更改,并且密码不受域密码策略的约束。密码更改由客户端(计算机)驱动,而不是 AD。

    用户通常不知道计算机凭据,因为它们是由计算机随机设置的。但您可以设置自己的密码;这是一个用于执行此操作的 PowerShell 脚本:

    $pc = read-host –Prompt “Input computer name to reset“ # Specify the computer name.
    $pw = read-host –Prompt “Input random characters for temp password“ –AsSecureString # Specify the password.
    Get-ADComputer $pc | Set-ADAccountPassword –NewPassword:$pw -Reset:$true

    结论

    现在您已经了解了如何使用 PowerShell 管理 Active Directory 计算机帐户。您可以自行增强所有这些脚本,使它们适合您的目的。

    请记住,密切跟踪计算机帐户的所有更改至关重要,这样您就可以快速发现任何不需要的修改并做出适当的响应。

    您需要 登录账户 后才能发表评论

    取消回复欢迎 发表评论:

    关灯