Question

The title says it all i have a Start-Job with a script block that instead of executing the commands it output the info about the job.

The order in which this gets executed is the following

$location = $(Get-Location).toString()

$oldModulePath = $env:PSModulePath
$env:PSModulePath = $env:PSModulePath + ";" + $location + "\LongitudePowershellModules"

$env:PSModulePath

$configXML = [xml](Get-Content $location"\InstallationConfig.XML")
$config = $configXML.root

Import-Module CreateComponentsFolder -PassThru
Import-Module CreateTransferFolder -PassThru
Import-Module ConfigureDB -PassThru
Import-Module FastProxyTools -PassThru 
Import-Module WspTools -PassThru
Import-Module CopySharepointPowershellXML -PassThru
Import-Module SearchCenterTools -PassThru
Import-Module ConfigureFAST -PassThru

# 1 - CreateComponentsFolder

CreateLongitudeComponentsFolder -currentLocation $location

And the module with the start-job

function CreateLongitudeComponentsFolder
{
    Param(
       [Parameter(Mandatory=$True, Position=1)]
       [string]$currentLocation
    )
    
    $scriptBlock = {Write-Host "Copying files to '"C:\Program Files\Ba-insight\Longitude Fast Component"'"`

            Copy-Item $currentLocation"\Longitude Fast Component" "C:\Program Files\Ba-insight\Longitude Fast Component" -recurs`
    }


    Start-Job -ScriptBlock $scriptBlock -ArgumentList $currentLocation -Name "CopyingFiles"
    
    $scriptBlock = {$fileAndFolderList = Get-ChildItem $currentLocation"\Longitude Fast Component" -recurs
        $targetLocationFileAndFolderList = Get-ChildItem "C:\Program Files\Ba-insight\Longitude Fast Component" -recurs
    
        $timer = new-object System.Timers.Timer
        $timer.Interval = 500 #0.5 sec 
            $timer.AutoReset = $true
        $itemsPresent = $fileAndFolderList | Measure-Object

        $action = {foreach($item in $fileAndFolderList)`
            {`
                if($itemsPresent -ne 0)`
                {`
                    if($targetLocationFileAndFolderList.IndexOf($item) -ne -1)`
                    {`
                        $itemsPresent = $itemsPresent - 1`
                    }`
                    else`
                    {`
                        $itemsPresent = $fileAndFolderList | Measure-Object`
                    }`
                }`
                else`
                {`
                    $timer.stop()`
                }`
            }`
    }
    
    Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action $action | Out-Null

        $timer.Enabled = $true
    }

    Start-Job -ScriptBlock $scriptBlock -ArgumentList $currentLocation -Name "CheckingFiles"
    
    Wait-Job -Name "CheckingFiles"
    Write-Host "All files have been copied."
}

I don't get any errors but instead of executing the commands it writes the following for both start-jobs

HasMoreData   : True
StatusMessage :
Location      : localhost
Command       : Write-Host "Copying files to '"C:\Program Files\Ba-insight\Longitude Fast Component"'"`

                            Copy-Item $currentLocation"\Longitude Fast Component" "C:\Program Files\Ba-insight\Longitud
                e Fast Component" -recurs`

JobStateInfo  : Running
Finished      : System.Threading.ManualResetEvent
InstanceId    : 70ab6414-0ca4-467e-b283-20057e4141ad
Id            : 1
Name          : CopyingFiles
ChildJobs     : {Job2}
Output        : {}
Error         : {}
Progress      : {}
Verbose       : {}
Debug         : {}
Warning       : {}
State         : Running

It probably has to do with how i wrote the script blocks but i cannot figure out what is different from all the other examples I've seen.

Also is it possible to have a start-job wait for user input using the read-host command?

EDIT: I found the problem with why the job was not executing. The parameter was not being passed correctly. Here is a link for where i found the solution.

Passing parameters to start-job

The last example works and you have to do it like that even for a single parameter.

I still do have one question. The stuff i wrote above still gets outputed to the console. Is there any way to suppress unwanted output from the start-job cmdlet?

No correct solution

OTHER TIPS

I think you need to include the parameters in the script block so that you can pass that down to the job using the argumentlist parameter, as it stands I believe $currentLocation will be null in the script block.

So you need something like this in your script blocks:

$scriptBlock = {
    param([string]$currentLocation)

    $source - Join-Path -Path $currentLocation -ChildPath "Longitude Fast Component"
    $destination = "C:\Program Files\Ba-insight\Longitude Fast Component"
    Write-Host "Copying files to [$destination]"
    Copy-Item $source $destination -recurse
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top