2017-04-11 117 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

相關問題