need help passing argument to script or function called in Start-JOB

  • Hi,

    I am automating delete from personal mailbox

    mails to be placed in 60 different folders and over weekend total number of email could be 10000+

    initially I created function that get subfolder name from file and delete records in each folder one by one , delete might take a while....

    so I modified script to use start-job in order to run delete in 60 folders in parallel ,

    ( see script bellow runs fine and take about 1 minutes to delete 10000+ emails from 60 folders)

    $ScriptBlock =

    {

    param([int] $OlderThenDays, [string] $Subfolder)

    $MailBox = "DBA@mMydomain.com"

    # Outlook Connection

    $Outlook = New-Object -ComObject Outlook.Application

    # Outlook folder to clean

    $EmailsInFolder = $Outlook.Session.Folders.Item($MailBox).Folders.Item($Subfolder).Items

    $Today = get-date

    $i=($EmailsInFolder.count-1)

    For($i; $i -ge 1; $i--)

    {

    $difference = New-TimeSpan -Start $($EmailsInFolder)[$i].SentOn -End $Today

    if ($difference.Days -ge $OlderThenDays)

    {

    $($EmailsInFolder)[$i].SentOn

    $($EmailsInFolder)[$i].Subject

    $($EmailsInFolder)[$i].delete()

    }

    }

    }

    $x = 0

    $data = Get-Content "U:\PS\LabOutput\Outlooksubfolders_test3.txt" # I already got list of subfolders

    foreach ($line in $data)

    {

    write-host $line

    Start-Job $ScriptBlock -ArgumentList $x , $line

    }

    What I prefer to have: 1 function defined in module or file and call to this function in Start-JOB

    something like ...

    $x = 0

    $data = Get-Content "U:\PS\LabOutput\Outlooksubfolders_test3.txt" # I already got list of subfolders

    foreach ($line in $data)

    {

    write-host $line

    Start-Job -ScriptBlock {My-Function-With-Parameters-To-Delete-From-Subfolder -OlderThenDays $args[0] -$Subfolder $args[1]} -ArgumentList $x, $line }

    }

  • Are you asking how you define a named function in a script file?

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • I want to call function in Start-Job and submit parameters to the function

    Example

    function DoSomeWork (param [int] param1, [string] param2)

    {

    code...

    }

    I want to call DoSomeWork like ..

    Start-Job -ScriptBlock {DoSomeWork -param1 $args[0] -param2 $args[1] } -ArgumentList '1', 'test'

    or

    create Test1.ps1 with code bellow

    ----

    Import-Module \\dc1\Share\PS\Myfunctions

    (param [int] param1, [string] param2)

    function DoSomeWork (param [int] param1, [string] param2)

    ----

    and call it like

    Start-Job -ScriptBlock {C:\Scripts\Test1.ps1 $args[0] -param2 $args[1] } -ArgumentList '1', 'test'

  • The following is a real example from one of my ps1 script files:

    function New-EosDistributionGroup

    {

    [CmdletBinding()]

    Param

    (

    [Parameter(Position=0,Mandatory=$true)][string]$Name

    ,[Parameter(Position=1,Mandatory=$true)][string]$Domain

    ,[string]$DisplayName

    ,[string]$Trustee

    ,[bool]$InternalGroup

    )

    # Some code here.

    }

    ...and this is a call:

    New-EosDistributionGroup "Contracts" "GaryVarga.com" -DisplayName "Gary Varga" -Trustee "Gary Varga"

    I hope this helps. Any questions? Just ask!!!

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Gary Varga (3/7/2016)


    The following is a real example from one of my ps1 script files:

    function New-EosDistributionGroup

    {

    [CmdletBinding()]

    Param

    (

    [Parameter(Position=0,Mandatory=$true)][string]$Name

    ,[Parameter(Position=1,Mandatory=$true)][string]$Domain

    ,[string]$DisplayName

    ,[string]$Trustee

    ,[bool]$InternalGroup

    )

    # Some code here.

    }

    ...and this is a call:

    New-EosDistributionGroup "Contracts" "GaryVarga.com" -DisplayName "Gary Varga" -Trustee "Gary Varga"

    I hope this helps. Any questions? Just ask!!!

    Hi Gaz,

    the goal is to call function New-EosDistributionGroup in parallel 60 times using Start-job

    Start-Job -ScriptBlock {New-EosDistributionGroup $args[0] $args[1] $args[2] $args[3]} -ArgumentList "Contracts" "GaryVarga.com" -DisplayName "Gary Varga" -Trustee "Gary Varga"

  • I haven't used Start-Job but I assume that you just call it in a for loop.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply