将计算机列表导入Powershell以获取OU

将计算机列表导入Powershell以获取OU,powershell,Powershell,我试图做一些我认为很简单的事情,但我最终撞到了墙 我有一个从报告中导出的需要注意的计算机列表,我们按地理位置组织广告结构,我希望通过powershell运行该计算机列表以返回它们所在的OU 我已经把电脑放进了一个普通的csv中,有没有简单的方法可以让我为那些特定的电脑导出OU 例如,这里是我到目前为止得到的,但当我运行它时,它总是搜索csv的第一行,然后再搜索该行?e、 g它尝试搜索“pc1.domain.com=pc7.cdomain.com”时失败 谢谢 马特 更新: 我已经设法让它以我想要

我试图做一些我认为很简单的事情,但我最终撞到了墙

我有一个从报告中导出的需要注意的计算机列表,我们按地理位置组织广告结构,我希望通过powershell运行该计算机列表以返回它们所在的OU

我已经把电脑放进了一个普通的csv中,有没有简单的方法可以让我为那些特定的电脑导出OU

例如,这里是我到目前为止得到的,但当我运行它时,它总是搜索csv的第一行,然后再搜索该行?e、 g它尝试搜索“pc1.domain.com=pc7.cdomain.com”时失败

谢谢 马特

更新:

我已经设法让它以我想要的方式工作,除了导出CSV,它只导出命令的最后结果

$Computers = Import-Csv C:\Users\User\Desktop\Computers.csv 

ForEach ($Computer in $Computers)
{
    $pc = $Computer.("ComputerName")
    Get-ADComputer -Identity "$pc" | Export-Csv C:\Users\User\ComputersResults.csv
}

你知道我需要做什么来获取所有的结果吗?当我在没有导出的情况下运行它时,Powershell终端会显示所有这些数据…

我认为问题可能在于CSV文件如何组织数据。确保它是这种格式或类似的格式

右边可以有更多列。将此csv文件导入powershell时,它将标记为2的行作为powershell对象的第一个元素。然后您可以通过
row1.Name
访问
pc1.domain.com
。这将转化为ur代码,如下所示:

$Computers = Import-Csv C:\Users\User\Desktop\Computers.csv 

$result = ForEach ($Computer in $Computers)
{
    Get-ADComputer -Identity $($Computer.Name)
    #To get the OU alone, you should do (Get-ADComputer -Identity $($Computer.Name)).DistinguishedName
}
$result | Export-Csv C:\Users\User\ComputersResults.csv -notypeInformation

看看导入Csv和Get-AdComputer cmdlet。这似乎很有效,只是导出有点不太正确,它只是给了我字符串长度?所以导出看起来是这样的:#键入System.String Length 89 91 79 76是的,只需将以下内容添加到导出代码中:
-notypeInformation
$Computers = Import-Csv C:\Users\User\Desktop\Computers.csv 

$result = ForEach ($Computer in $Computers)
{
    Get-ADComputer -Identity $($Computer.Name)
    #To get the OU alone, you should do (Get-ADComputer -Identity $($Computer.Name)).DistinguishedName
}
$result | Export-Csv C:\Users\User\ComputersResults.csv -notypeInformation