We help IT Professionals succeed at work.
Get Started

Need some verification on Powershell Multi-threading

268 Views
Last Modified: 2015-09-15
I don't like massive amounts of code in a single file.  I know it can't always be avoided, but in my case, I actually need to avoid putting some blocks of code in the same file.  I decided to break-out my code in four different files:
File 1:  Automate.ps1  (serves as initiator)
File 2:  au_CreateADUser.ps1
File 3:  au_CreateEXContact.ps1 (creates an Exchange 2010 contact card)
File 4:  au_CreateMSOLUser.ps1 (creates a student's email account and assigns licensing)

I want to execute files 2 - 4 in parallel.  Each file has to iterate through a DataSet that I will pass.  I have the paremterization setup fine, but I need confirmation that my syntax for executing each script in parallel using RunSpaces is correct.  Following is the code I have generated for doing this.  By the way, I have borrowed heavily from forums for what limited understanding I have about it.
#region Create Active Directory User
#######################################################################################
    $AD = 
    {
        Invoke-Expression ".\automation\au_CreateADUser.ps1 -DataSet $ds"
    }
           
    $thread1 = [System.Management.Automation.PowerShell]::Create()
    $thread1.RunspacePool = $Pool
    $thread1.AddScript($AD)
    $thread1.BeginInvoke()
#endregion


#region Create MSOL Services Account
#######################################################################################
    $MSOL = 
    {
        Invoke-Expression ".\automation\au_CreateMSOLUser.ps1 -DataSet $ds"
    }
           
    $thread2 = [System.Management.Automation.PowerShell]::Create()
    $thread2.RunspacePool = $Pool
    $thread2.AddScript($MSOL)
    $thread2.BeginInvoke()
#endregion


#region CreateExchange Server Contact
#######################################################################################
    $ExContact = 
    {
        Invoke-Expression ".\automation\au_CreateEXContact.ps1 -DataSet $ds"
    }
           
    $thread3 = [System.Management.Automation.PowerShell]::Create()
    $thread3.RunspacePool = $Pool
    $thread3.AddScript($ExContact)
    $thread3.BeginInvoke()
#endregion

Open in new window

Comment
Watch Question
The More I know, the more I don't know
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 2 Answers and 13 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE