Run PS scripts in separate PS windows

Welcome Forums General PowerShell Q&A Run PS scripts in separate PS windows

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #34960

    Participant
    Points: 0
    Rank: Member

    I have the below script in a file called export.ps1 which runs against a text files with user names. The challenge is now I have several files in the same directory called files1.txt,file2.txt,...,filen.txt. I need to do a get-childitem c:\files\*.txt and run the below ps script against each one of them in its own Powershell script with passing arguments and variables to each separate running script. Any ideas how to accomplish this? Note that the $date is a variable that will change and I will need to pass to the running scripts.

    Add-PSsnapin Microsoft.Exchange.Management.PowerShell.E2010
    $users = Get-Content c:\files\file1.txt
    $date = "09/01/2015"

    foreach ($user in $users) {
    $Mailbox = Get-Mailbox -Identity $user

    New-MailboxExportRequest -Mailbox $Mailbox -ContentFilter {Received -lt $date} -FilePath \\Server\File1\$Mailbox.pst
    }

  • #34961

    Participant
    Points: 0
    Rank: Member

    Why do you need to call a separate powershell script for each file? Why not just create a foreach loop around the script you already have?

    Add-PSsnapin Microsoft.Exchange.Management.PowerShell.E2010
    Get-ChildItem c:\files\*.txt | ForEach-Object {
        $users = Get-Content $_.fullname
        etc.
        etc.
        etc.
    }
    
  • #34962

    Participant
    Points: 0
    Rank: Member

    Running multiple instance will finish faster instead running things serially. The number of users split across the text files can be in the hundreds or maybe thousands.

  • #34963

    Participant
    Points: 0
    Rank: Member

    You can use PSJob cmdlets (IE Start-Job), but probably more efficient would be to use runspaces (https://mcpmag.com/articles/2015/08/06/multithread-your-commands.aspx).

  • #34964

    Participant
    Points: 0
    Rank: Member

    I am completely new to that approach. I will have to read little bit more about it.

    Thanks

The topic ‘Run PS scripts in separate PS windows’ is closed to new replies.