powershell - 从 txt 禁用多个计算机帐户
问题描述
$Computers = Get-Content c:\temp\computers.txt
foreach ($Computer in $Computers) {
$ADComputer = $null
$ADComputer = Get-ADComputer $Computer -Properties Description
if ($ADComputer) {
Add-Content C:\temp\computers.log -Value "Found $Computer, disabling"
Set-ADComputer $ADComputer -Description "Computer Disabled on $(Get-Date)" -Enabled $false
} else {
Add-Content C:\temp\computers.log -Value "$Computer not in Active Directory"
}
}
有人可以验证上面的代码是否正确吗?上面的代码假设:
- 禁用多台计算机,从 txt 文件中读取。
- 编辑禁用计算机的描述。
- 为结果创建日志文件。
客户没有任何我们可以测试的测试环境。所以我只是不想冒险。
解决方案
原始代码工作正常。所以我将其发布为答案。
$Computers = Get-Content c:\temp\computers.txt
foreach ($Computer in $Computers) {
$ADComputer = $null
$ADComputer = Get-ADComputer $Computer -Properties Description
if ($ADComputer) {
Add-Content C:\temp\computers.log -Value "Found $Computer, disabling"
Set-ADComputer $ADComputer -Description "Computer Disabled on $(Get-Date)" -Enabled $false
} else {
Add-Content C:\temp\computers.log -Value "$Computer not in Active Directory"
}
}
推荐阅读
- javascript - 带有 html 表单文本的字符串插值
- javascript - React 组件的 props 是如何异步更新的?
- mysql - 如何使用sql将数据从一个表插入到另一个表
- javascript - 装满条码扫描器的输入框
- amazon-ec2 - 在 EC2 实例中使用 Dask 会引发“无法收集 1 个密钥...”
- python-3.x - 了解python numpy数组的内存分配
- javascript - JavaScript:循环遍历对象数组并过滤特定属性/属性
- wordpress - Bootstrap 3.3.7 nav 默认通过 Wordpress 在移动设备上下拉
- php - 具有相同值的数组值的总和
- spring-boot - 请求时的自定义标头