Powershell script request OU specification in AD for computer names and export

I pretty much (completely read) a newbie to 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 part of the software inventory, but it relies on a text file with computer names to scan. I am looking for a way to query AD for computer names for specific organizational units. Ideally, the script will prompt the user to enter the OU and the path to the text file for export. He will then look for AD to match and, if found, is exported to the specified path. For example, to export all computers to OU = workstations, OU = New York, DC = Fabrikam, DC = com, the user should be prompted and enter "New York", and all computer names will be exported to a specific path.Any help would be greatly appreciated.

+3
source share
2 answers

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

You can get computer names using 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"
+3
source
   $City = Read-Host 'City'
   $file = Read-host 'File path'
   try{
   $ou = "OU=Workstations,OU=$City,DC=Fabrikam,DC=com"
   Get-ADComputer -Filter '*' -SearchBase $ou -properties DNSHostName | %  
   DNSHostName | Out-File -Filepath $file
   }
   catch {
   $error[0].exception.message
   }

it will be a little faster and less memory consumed.

+1
source

All Articles