gpt4 book ai didi

powershell - 从CSV读取时出现 “Get-ADComputer : Cannot find an object with identity”错误

转载 作者:行者123 更新时间:2023-12-02 23:37:52 25 4
gpt4 key购买 nike

我正在尝试从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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com