Powershell脚本在AD中查询特定的OU以获取计算机名称并导出 [英] Powershell script to query specifc OU in AD for computer names and export

查看:810
本文介绍了Powershell脚本在AD中查询特定的OU以获取计算机名称并导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎是PowerShell的新手(正在阅读),并且我正在研究一个项目,以清点网络上某些计算机上已安装的软件。我挖出一个脚本来处理软件清单部分,但是它依赖于带有计算机名称的文本文件进行扫描。我正在寻找一种在AD中查询特定OU的计算机名称的方法。理想情况下,脚本会提示用户输入要导出到的OU和文本文件的路径。然后,它将在AD中搜索匹配项,如果找到则导出到指定的路径。因此,例如,要导出OU = Workstations,OU = New York,DC = Fabrikam,DC = com中的所有计算机,用户应收到提示并输入 New York,所有计算机名称将被导出到指定的路径。
任何帮助将不胜感激。

解决方案

要简单地执行此操作,请在PowerShell中使用Active Directory模块。 / p>

您可以使用类似于以下脚本的脚本来检索计算机名称:

  $ City =读主机'City'
$ ou = OU = Workstations,OU = $ City,DC = Fabrikam,DC = com

$ Computers = Get-ADComputer-过滤器'*'-SearchBase $ ou

$ Computers | Foreach {$ _。DNSHostName} |文件外-Filepath output.txt


I'm pretty much(read completely) a novice with PowerShell and I'm working on a project to inventory installed software on some computers on our network. I dug up a script to handle the software inventory part, but it relies on a text file with computer names to scan. I'm looking for a way to query AD for computer names for specific OU's. Ideally the script would prompt the user to enter the OU and path of text file to export to. Then it would search AD for a match and if found export to the specified path. So for example, to export all computers in OU=Workstations,OU=New York,DC=Fabrikam,DC=com" the user should receive a prompt and enter "New York" and all computer names would be exported to the specfied path. Any help would be greatly appreciated.

解决方案

To do it simply, use the Active Directory module in PowerShell.

You can retrieve the computer names with a script similar to this:

$City = Read-Host 'City'
$ou = "OU=Workstations,OU=$City,DC=Fabrikam,DC=com"

$Computers = Get-ADComputer -Filter '*' -SearchBase $ou

$Computers | Foreach { $_.DNSHostName } | Out-File -Filepath "output.txt"

这篇关于Powershell脚本在AD中查询特定的OU以获取计算机名称并导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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