从PC的文本文件中获取Active Directory信息-Powershell [英] Get Active Directory Information from PC's in a text file - Powershell

查看:105
本文介绍了从PC的文本文件中获取Active Directory信息-Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试运行的Powershell命令行出现问题。我们需要的是使用Powershell在文本文件列表中从PC获取PC名称和LastLogonDate。

I am running to an issue with a Powershell command line I'm attempting to run. What we are needing is to get the PC Name and LastLogonDate from PC's in a list of text files with Powershell.

这是我尝试运行的代码:

Here is the code I'm attempting to run:

Get-Content C:\Hosts.txt | ForEach-Object{Get-ADComputer $_ -Properties Name,LastLogonDate | Select-Object Name,LastLogonDate | Export-Csv C:\Output.csv}

当我运行该代码时,出现错误提示:尽管PC确实位于AD中,但找不到标识为'ComputerName'的对象。让我感到困惑的是,如果单独运行,则ForEach-Object之后的所有内容都运行良好。当您添加Get-Content和ForEach-Object时,错误开始。从错误消息中可以看到,正在从文本文件中读取每个计算机的名称,但是我想知道它是否以Get-ADComputer不喜欢的方式传递它。

When I run that code I get an error stating: "Cannot find a object with identity: 'ComputerName'" despite the fact that the PC is truly in AD. What is confusing me is that everything after the ForEach-Object runs great if you run it by itself. When you add the Get-Content and ForEach-Object the errors begin. I can see from the error messages that each individual computer name is being read from the text file, but I wonder if it is passing it in a way the Get-ADComputer doesn't like.

推荐答案

几件事。首先是查看hosts.txt文件中的内容。第二件事是将Export-Csv命令移到ForEach-Object语句之外。如果保留原样,它将仅返回在csv文件中处理的最后一个对象。我已经发布了一个示例。

A couple things. First is to see what's in the hosts.txt file. Second thing is to move the Export-Csv command outside of the ForEach-Object statement. If you leave it how it is, it will only return the last object it processed in the csv file. I've posted an example of how it should be.

Get-Content C:\Hosts.txt | ForEach-Object {Get-ADComputer $_ -Properties Name,LastLogonDate | Select-Object Name,LastLogonDate} | Export-Csv C:\Output.csv

我无法复制您的问题,并且唯一不同的是hosts.txt文件的内容,因此查看它会很有帮助。

I'm not able to replicate your problem and the only thing different is the contents of the hosts.txt file so it would be helpful to see it.

这篇关于从PC的文本文件中获取Active Directory信息-Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆