2017-04-11 115 views
0

我有一个包含名为MY-OU.txtPowershell的获得对用户登录OU在Active Directory

OU=offic-Computers,OU=comp-computers,DC=my,DC=company,DC=com

我可以得到该OU中的所有计算机列表的文本文件:

ForEach ($OU in Get-Content "C:\temp\MY-OU.txt") 
{ 
Get-ADComputer -SearchBase $OU -Filter '*' | Select -Exp Name | Out-File -filepath "C:\temp\MY-OU-computers.txt" 
} 

,我可以得到当前登录用户从上面的代码的输出:

$content = Get-Content C:\temp\MY-OU-computers.txt 
$output = foreach ($PC in $content) {Get-WmiObject –ComputerName $PC –Class Win32_ComputerSystem | Select-Object UserName} 
$output | Out-File -filepath "C:\temp\CAD-OU-computers-users.txt" 

如何这两部分结合起来,并得到当前在特定OU中登录的用户?

回答

0

相反的输出从Get-ADComputer结果到一个文件,你可以将它们分配给一个变量($Computers),然后用其作为输入你的下一个foreach循环:

foreach ($OU in Get-Content "C:\temp\MY-OU.txt"){$Computers += Get-ADComputer -SearchBase $OU -Filter '*' | Select -ExpandProperty Name} 

$output = foreach ($PC in $Computers) {Get-WmiObject –ComputerName $PC –Class Win32_ComputerSystem | Select-Object UserName} 
$output | Out-File -filepath "C:\temp\CAD-OU-computers-users.txt" 
+0

谢谢@詹姆斯C. – Eric

相关问题