Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to log job output to a file after the jobs are completed #173

Open
ghost opened this issue Jan 10, 2018 · 4 comments
Open

How to log job output to a file after the jobs are completed #173

ghost opened this issue Jan 10, 2018 · 4 comments

Comments

@ghost
Copy link

ghost commented Jan 10, 2018

Tried doing this
Get-RSJob | Receive-RSJob | Format-Table -AutoSize | Out-File $outfile

Got below error
The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread. Validate that the cmdlet makes these calls correctly, or contact Microsoft Customer Support Services.

I am calling the script containing Start-RSJob from another script using
Invoke-Expression { script file + params }

@ghost ghost changed the title The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread. Validate that the cmdlet makes these calls correctly, or contact Microsoft Customer Support Services. How to log job output to a file after the jobs are completed Jan 10, 2018
@MVKozlov
Copy link
Contributor

any info about your environment ?
I can't reproduce this

@ghost
Copy link
Author

ghost commented Jan 11, 2018 via email

@MVKozlov
Copy link
Contributor

MVKozlov commented Jan 11, 2018

The error itself not in RSJob module
Seems there is something in Invoke-SqlCmd use direct output to $host, so it is not thread safe
this code variant show that error catched inside Invoke-SQLCmd
and you can use this method to catch errors :)

$serverName = "localhost"
$databaseName = "master"

1..31 | Start-RSJob -Throttle 31 -Name { $_ } -ScriptBlock {

	$params = @{
		ServerInstance = $Using:serverName
		Database = $Using:databaseName
		Query = "select getdate()"
		OutputSqlErrors = $true
		QueryTimeout = 65536
	}
	$err = $null
	try {
		Invoke-SqlCmd @params | Out-Null
	}
	catch {
		$err = $_
	}

    [pscustomobject]@{
		FileToProcess = $_
		Server=$Using:serverName
		Database=$Using:databaseName
 		Time = $(Get-Date)
		Error = $err
    }

} | Wait-RSJob -ShowProgress | Receive-RSJob -WarningVariable wv -ErrorVariable er
$wv
$er

@ghost
Copy link
Author

ghost commented Jan 12, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants