重新启动特定 OU 中的所有计算机
最近我收到了按房间关闭电脑的请求。具体来说,这是关于一所学校的学生经常忘记关闭电脑的故事。当然,这可以通过 PowerShell 实现,还有什么呢?
目标
首先,我们必须检索所有组织单位。不仅是父母的,还有孩子的。我喜欢 Out-GridView 的用户友好性。
$room=Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Clients,OU=Workstations,DC=sid-500,DC=com' -SearchScope OneLevel | Select-Object DistinguishedName | Out-GridView -PassThru -Title 'Select Computers OU to restart all Computers in that OU'
请注意管道后面的 Out-GridView 命令。这使我们能够在图形界面中选择 OU。
选择 OU 后,单击“确定”。该 OU 中的所有计算机都将关闭。
代码
请注意,您必须根据您认为合适的方式调整第 1 行与您的 OU 结构。
$room=Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Workstations,DC=sid-500,DC=com' -SearchScope OneLevel | Select-Object DistinguishedName | Out-GridView -PassThru -Title 'Select Computers OU to restart all Computers in that OU'
$comp=(Get-ADComputer -SearchBase $room.distinguishedname -Filter *).Name
Foreach ($c in $comp)
{Restart-Computer -ComputerName $c -ErrorAction SilentlyContinue -Force -Verbose}
就是这样。自动化完成。
