2

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!

1 Answers1

0

Your error indicates you do not have the Quest Module imported into the workflow session, try something like this... Get your workflow session inside of the $Session variable

Invoke-Command -Session $Session -ScriptBlock {Import-Module <ModuleName> -Verbose}

Hope this helps, Chris

Christopher Douglas
  • 1,519
  • 2
  • 9
  • 16
  • 1
    I cannot run Invoke-Command from within the workflow like this: Invoke-Command -Session $Session -ScriptBlock {Add-PSSnapinQuest.ActiveRoles.ADManagement} -verbose because the command is not usable within workflows – Asdaha Ahsla Jan 31 '13 at 15:47
  • The invoke is not usable within a workflow, sure. but if you will see my last remark before the code - get your workflow session inside of the $Session variable - $Session = Get-PsSession, with the assumption that the only session running on the box is a workflow session. Start one with New-PsWorkflowSession, get it into that variable, then - outside of the workflow. Invoke the command. – Christopher Douglas Jan 31 '13 at 16:12
  • I tried what you proposed but adding the module fails inside that session. The term 'Add-PSSnapinQuest.ActiveRoles.ADManagement' is not recognized as the name of a cmdlet, function, script file, or operable program. Get-modules also didn't show the quest tools - any other ideas how I could possibly do? – Asdaha Ahsla Feb 01 '13 at 13:00