Powershell “错误”;Get ADComputer:找不到标识为“的对象”;从CSV获取时

Powershell “错误”;Get ADComputer:找不到标识为“的对象”;从CSV获取时,powershell,Powershell,我正试图从CSV文件中禁用PC,但我不断收到以下错误 当我手动将设备列表复制到文本文件(记事本)中时,该代码起作用 谢谢 Get-ADComputer:找不到标识为“PC-125632”的对象 在“DC=XXX,DC=XXX,DC=XXX”下。第12行字符:15+ADC计算机= 获取ADComputer$Computer-属性描述+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 以下是我到目前为止所做的尝试 Get-Content C:\Tem

我正试图从CSV文件中禁用PC,但我不断收到以下错误

当我手动将设备列表复制到文本文件(记事本)中时,该代码起作用

谢谢

Get-ADComputer:找不到标识为“PC-125632”的对象 在“DC=XXX,DC=XXX,DC=XXX”下。第12行字符:15+ADC计算机= 获取ADComputer$Computer-属性描述+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以下是我到目前为止所做的尝试

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"    
    }    
} 
Get-ADComputer:找不到标识为“PC-125632”的对象 在“DC=XXX,DC=XXX,DC=XXX”下。第12行字符:15+ADC计算机= 获取ADComputer$Computer-属性说明+

通过(2)和(3)纠正上述错误

下面的代码我已成功执行。它很好用

(1) 我已更改csv的导入方式

 $computers=Import-Csv -Path "C:\Temp\test.csv" 
(2) 而不是

ForEach ($Computer in $Computers)
我用

ForEach ($Computer in $Computers.distinguishedName) 
其余的都一样

总体上看是这样的,

$computers=Import-Csv -Path "C:\Temp\test.csv" 

ForEach ($Computer in $Computers.distinguishedName)    
{     
    $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"    
    }    
} 
(3) 我的csv格式是这样的


错误消息确实显示了问题,但它相当微妙。。。尾部有一个空格(在计算机名称的末尾):

“PC-125632”

这是计算机名的一部分,但不应包括在内

通过使用删除任何前导或尾随空格,很容易修复

根据我之前的评论,我还使用了
导入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"
    }
}

使用
导入csv
读取csv文件,而不是
获取内容
您的csv文件是什么样子的?您的环境中是否有一台名为PC-125632的计算机?@guiwhats,我使用了导入csv,但它仍然给我相同的错误。@EBGreen该文件很小,它有一个标题(我删除的DeviceName)和20个设备,是的,我们有该名称无需删除标题,使用导入Csv,然后定义要与foreach一起使用的属性:
$Computers=Import Csv C:\temp\test.Csv
foreach($Computers.DeviceName中的计算机)
@dano要查看答案的最终编辑,可以根据需要采用/编辑。总的来说,它可以正常工作,并使用csvThanks禁用PC以获取信息,但不幸的是,由于我仍在获取((Get-ADComputer:无法在下面找到标识为“PC-125632”的对象)你使用过csv格式的我的代码吗?@Aravinda-它对你有效,因为你没有使用相同的源数据。问题实际上是计算机名有一个尾随空格,错误确实显示了这一点,但它非常微妙,很容易被忽略。你的代码更改并没有纠正这一点,所以OP仍然会得到相同的错误。谢谢你提供的信息.trim()起作用了,我没有使用过滤器,因为它给出了一些奇怪的错误。下面是我使用的$Computer=$Computer.trim()$ADComputer=Get-ADComputer$Computer-Properties说明