作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从CSV文件禁用PC,但是我一直收到以下错误消息。
当我手动将设备列表复制到文本文件(记事本)中时,该代码有效
谢谢
Get-ADComputer : Cannot find an object with identity: 'PC-125632 ' under: 'DC=XXX,DC=XXX,DC=XXX'. At line:12 char:15 + $ADComputer = Get-ADComputer $Computer -Properties Description+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Get-Content C:\Temp\test.csv | Select-Object -Skip 1 | Set-Content C:\Temp\List.txt
$Computers = Get-Content C:\Temp\List.txt
Start-Sleep -s 5
ForEach ($Computer in $Computers)
{
$ADComputer = $null
$ADComputer = Get-ADComputer $Computer -Properties Description
If ($ADComputer)
{
Add-Content c:\temp\computers.log -Value "The following PC $Computer has been found and disabled"
Set-ADComputer $ADComputer -Description "$($ADComputer.Description)- Disable due to inactivity - $(Get-Date) - by $env:UserName " -Enabled $False
}
Else
{
Add-Content c:\temp\computers.log -Value "$Computer not Found in Active Directory or Was disabled before"
}
}
最佳答案
该错误消息确实显示了该问题,但是它非常微妙……后面有一个空格(在计算机名的最后):
'PC-125632 '
Trim()
删除任何开头或结尾的空格很容易解决。
Import-Csv
,由于没有匹配项,因此
-Filter "Name -eq $Computer"
将返回null。
$Computers = Import-Csv C:\temp\test.csv
Foreach ($Computer in $Computers.DeviceName) {
$Computer = $Computer.Trim()
$ADComputer = Get-ADComputer -Filter "Name -eq $Computer" -Properties Description
If ($ADComputer) {
Add-Content c:\temp\computers.log -Value "The following PC $Computer has been found and disabled"
Set-ADComputer $ADComputer -Description "$($ADComputer.Description)- Disable due to inactivity - $(Get-Date) - by $env:UserName " -Enabled $False
}
Else {
Add-Content c:\temp\computers.log -Value "$Computer not Found in Active Directory or Was disabled before"
}
}
关于powershell - 从CSV读取时出现 “Get-ADComputer : Cannot find an object with identity”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50799857/
我是一名优秀的程序员,十分优秀!