Author Posts

January 1, 2012 at 12:00 am

by bpearson32 at 2013-01-30 13:24:10

I've already built the query I need to use:

Get-ADComputer -Filter {OperatingSystem -eq "Windows 2000 Server"} -Property * | Sort-Object -Property Name | Select-Object -Property Name, OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion

Now instead of running it only on my current domain I have several forests (4 to be exact) with multiple domains inside that I need to search with the above query. I'm pretty sure I need a Do...While or a ForEach-Object type of looping structure but I haven't been able to figure that part out yet. The list of domains that I need to search will be in a text file (I named it domains.txt) one domain name per line.

Here is an example of the type of data in my domains.txt file:

prod.mydomain.local
exchange.prod.mydomain.local
qa.mydomain.local
dev.mydomain.local
sandbox.dev.mydomain.local
public.ourpublicdomain.com

So far I've been unable to figure out the looping structure that keeps the data in the pipeline so that it can be written out to a file.

I'm definitely new to PowerShell as I just finished Learn PowerShell 3 in a month of lunches and I've started Learn PowerShell ToolMaking in a month of lunches. So it may be a simple solution but I haven't been able to figure it out.

by ArtB0514 at 2013-01-30 14:20:48

Here are a couple of ideas that should get you started.

First, never, ever, try to store data in a pipeline. You'll almost certainly not get out what you expect. Think of separating your task into several pipelines such as Data Collection, Data Processing, and Data Presentation. When looping over a set of objects where each is going to provide a set of data with identical properties, Create an array object outside of the loop that you add each objects data to inside the loop.$colComputers = @()
foreach ($domain in $domainlist) {
$RawData = Get-ADComputer -Server $domain -Credentials $DomainAdmin -filter "whatever" -properties "whatever","list","you","want"
$colComputers += $RawData | Select Property,@{Name='CreatedProperty';Expression={arbitrary-script-block}}
}

In this particular case, though, rather than running Get-ADComputer locally, I'd set up a PSSession to a domain controller in each of the domains and use Invoke-Command -AsJob to execute the data collection in parallel on each domain.foreach ($domain in $domainlist) {
$DC =
$dcSession = New-PSSession -ComputerName $DC -Credential $DomainAdmin
Invoke-Command {
Add-Module ActiveDirectory
Get-ADComputer -filter "whatever" -properties "whatever","list","you","want"
} -Session $dcSession -AsJob
}
Wait-Job
$colComputers = Get-Job | Receive-Job