Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 168400

Re: Problem with PowerCLI and Threads / Jobs

$
0
0

I just created a little test just to test the general functionality of threads:

 

thread-ctrl.ps1

$job = {

    Set-Location $args[0]

    .\thread.ps1 -name $($args[1])

}

$currentPath = (Split-Path -parent $MyInvocation.MyCommand.Definition)

1..10 | foreach {

    $id = "{0:D3}" -f $_

    Start-Job -ScriptBlock $job -ArgumentList $currentPath, "thread-$id"

}

Get-Job | Wait-Job

Write-Host "Done!"

 

thread.ps1

Param (

 

    [Parameter(Mandatory=$True)]
    [String]$name
)

 

Write-Host "Hi, my name is thread $name"

 

sleep -seconds 5

 

Output:

PowerCLI C:\scripts> .\thread-ctrl.ps1

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
101             Job101          Running    True            localhost            ...
103             Job103          Running    True            localhost            ...
105             Job105          Running    True            localhost            ...
107             Job107          Running    True            localhost            ...
109             Job109          Running    True            localhost            ...
111             Job111          Running    True            localhost            ...
113             Job113          Running    True            localhost            ...
115             Job115          Running    True            localhost            ...
117             Job117          Running    True            localhost            ...
119             Job119          Running    True            localhost            ...
119             Job119          Completed  True            localhost            ...
117             Job117          Completed  True            localhost            ...
115             Job115          Completed  True            localhost            ...
113             Job113          Completed  True            localhost            ...
111             Job111          Completed  True            localhost            ...
109             Job109          Completed  True            localhost            ...
107             Job107          Completed  True            localhost            ...
105             Job105          Completed  True            localhost            ...
103             Job103          Completed  True            localhost            ...
101             Job101          Completed  True            localhost            ...
Done!


PowerCLI C:\scripts> Get-Job | Receive-Job
Hi, my name is thread thread-010
Hi, my name is thread thread-009
Hi, my name is thread thread-008
Hi, my name is thread thread-007
Hi, my name is thread thread-006
Hi, my name is thread thread-005
Hi, my name is thread thread-004
Hi, my name is thread thread-003
Hi, my name is thread thread-002
Hi, my name is thread thread-001

 

Works! So it has to be something with my other code ...

 

cheers

Mathias


Viewing all articles
Browse latest Browse all 168400

Trending Articles