I am pretty new to Powershell and have little problems in running my code parallel. My current code works in sequence but every attempt to make it run parallel failed so far. Here is what I plan to do:
I need to query multiple domain controllers (I use get-qaduser etc from the Quest cmdlets) to gather all the information I need. Since I currently contact one domain controller by the other the script runs a long time. My idea was to use the new Workflow feature of PS 3.0 but I am obviously not allowed to export my results to a file.
Working Script (in sequence):
Add-PSSnapin Quest.ActiveRoles.ADManagement
get-QADUser -Service 'domaincontroller:389' -SizeLimit 0 > OutputFile.csv
get-QADUser -Service 'domaincontroller2:389' -SizeLimit 0 > OutputFile2.csv
and so on
Here is what I have tried so far:
Just an excerpt - there are more get-qaduser and domains in the real script
Workflow Get-Domainaccounts{
Parallel{
get-QADUser -Service 'domaincontroller:389' -SizeLimit 0
}
}
but while running i get this error message:
Microsoft.PowerShell.Utility\Write-Error : The term 'get-QADUser' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
So I thought about adding the snippets to my workflow:
Workflow Get-Domainaccounts{
inlinescript {Add-PSSnapin Quest.ActiveRoles.ADManagement}
Parallel{
get-QADUser -Service 'domaincontroller:389' -SizeLimit 0
}
}
simply adding them doesn't work, using the inlinescript command doesn't pass it to the thread where get-QADUser is executed. I also tried adding the snapin via my profile but the workflow ignores it.
On Technet I found this function which actually works for built-in cmdlets but again not for the Quest tools. ForEach-Parallel.ps1 The function uses a clean runspace (which I assume the workflow also does).
Here is how I tried to run it:
the hosts.txt contains:
get-QADUser -Service 'domaincontroller:389' -SizeLimit 0 > OutputFile.csv
get-content .\hosts.txt | ForEach-Parallel -ScriptBlock {
$_ | invoke-expression
}
But I don't get any output - no error message, nothing. Running the code without the | invoke-expression works and shows me the content of the file. What am I doing wrong?
All I want to do is to run the Quest cmdlets in parallel. Any help is highly appreciated!