0
votes

I have a scriptblock I would like to run as a background job. Below is the command I would like to run:

Get-ChildItem -Recurse $source |  Get-DfsrFileHash | Export-csv -Append C:\Temp\Test_Source_Checksum.csv

If I run this command it goes through successfully with no issues.

I have tried the following for 'Start-Job'

Start-Job -ScriptBlock { Get-ChildItem -Recurse $source |  Get-DfsrFileHash | Export-csv -Append C:\Temp\Test_Source_Checksum.csv } 

This results in 'Get-Job' displaying as completed, when it actually hasnt, or doesnt appear to have judging by the missing file: 'Test_Source_Checksum.csv'

I have also tried using the following for 'Invoke-Command'

Invoke-Command -AsJob -ComputerName ($env:COMPUTERNAME) -ScriptBlock { Get-ChildItem -Recurse $source |  Get-DfsrFileHash | Export-csv -Append C:\Temp\Test_Source_Checksum.csv } 

This results in 'Get-Job' displaying failed.

If I display the failure using:

(get-job -name Job38).JobStateInfo.Reason

I get nothing back...

Am I using Start-Job/Invoke-Command incorrectly here?

The reason I would like to run this as a background job is, im trying to copy large amounts of data and checksum it (for a DFS Migration). I would like to copy the data in smaller subsets, then checksum the data which has been copied whilst its copying the next batch over...rinse and repeat

Thanks,

Chris

EDIT: Here is a copy of the whole script:

##----------------------------------------------------------------------------------##
$source="E:\DFSR_Migration_Test_Prod"
$dest="F:\DFSR_Migration_Test_Prod"
$what = @("/COPYALL","/B","/SEC","/E","/xd","dfsrprivate")
$options = @("/R:6","/tee","/MT:32")
$cmdArgs = @("$source","$dest",$what,$options)
##----------------------------------------------------------------------------------##
robocopy @cmdArgs
Write-Output "Prod_Copied @" (get-date) | Out-File C:\Temp\File_Copy.txt -Encoding ascii -Append -NoClobber
Write-Output "Initiating Prod Source Checksum @" (get-date) | Out-File C:\Temp\File_Copy.txt -Encoding ascii -Append -NoClobber
Start-Job -ScriptBlock { Get-ChildItem -Recurse $source | Get-DfsrFileHash | Export-csv C:\Temp\Prod_Source_Checksum.csv }
Write-Output "Initiating Prod Destination Checksum @" (get-date) | Out-File C:\Temp\File_Copy.txt -Encoding ascii -Append -NoClobber
Start-Job -ScriptBlock { Get-ChildItem -Recurse $dest | Get-DfsrFileHash | Export-csv C:\Temp\Prod_Destination_Checksum.csv }
2
Do you see any errors displayed if you try to receive the results from the job? Get-Job | Receive-Jobboeprox
Yes I do, Thank you, it appears WinRM wasnt setup so couldnt run Invoke-Command. This is now resolved, but now, both Invoke-Command and Start-Job are the same, as in, they both say completed but have not executed the scriptblock (no 'Test_Source_Checksum.csv')Chris Robinson
What version of PowerShell are you running? Have you tried to import the module within the scriptblock prior to running the Get-DfsrFileHash command?boeprox
Running version 5. Have just tried to add: -InitializationScript {Import-Module DFSN,DFSR} But issue persistsChris Robinson

2 Answers

0
votes

How are you passing $source? Because if you're doing nothing other than writing the variable name it'll be null, and if it's null you'll be executing Get-ChildItem for $pwd.Path on the remote system.

The simplest solution is to make $source into $using:Source.

Invoke-Command -AsJob -ComputerName ($env:COMPUTERNAME) -ScriptBlock { Get-ChildItem -Recurse $using:source |  Get-DfsrFileHash | Export-csv -Append C:\Temp\Test_Source_Checksum.csv } 
0
votes

In addition to the good information above, remember that starting a job is a lot like opening a new console (that you cannot see directly), this means that you may need to re-connect or re-authenticate. As an example, if you want to start a new job that uses PowerCLI, you will have to re-connect to your vCenter Server so in your scriptblock, you would preface your command with (connect-viserver...): start-job -scriptblock {connect-viserver -server <vCenterServer>; powercli command}.