I created the following script block, which should look for a file matching a regex on a UNC path and copy it to the target folder:
$ScriptBlock = {
param (
[string]$serverName,
[string]$ip,
[string]$fullPathRegEx,
[string]$target
)
if (!(Test-Connection $ip -Count 1 -Quiet)) {
$msg = "{0} is not responding!" -f $serverName
Write-Host $msg -ForegroundColor red
return
}
$file = Get-ChildItem -Path $fullPathRegEx
if ($file) {
$msg = "Copy-Item {0} {1}" -f $file, $target
Write-Host $msg -ForegroundColor yellow
Copy-Item $file $target
# $cmd = "Robocopy {0} {1} {2} /NJH /NJS /NDL" -f $file.Directory, $target, $file.Name
# Write-Host $cmd -ForegroundColor green
# Invoke-Expression -Command $cmd
$?
}
else {
$msg = "No file matching {0} found in {1}" -f $fullPathRegEx, $serverName
Write-Host $msg -ForegroundColor red
}
}
It works fine when called like this:
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $server,$ip[$server],$regEx,$targetPath
But when I call it via Start-Job, it doesn't copy the file:
foreach($serverName in $servers.keys)
{
$regEx = '\\{0}\c$\Data\DB-Backup\{1}{2}*.txt' -f $servers[$serverName], $serverName, $date
Start-Job $ScriptBlock -ArgumentList $serverName,$servers[$serverName],$regEx,$targetPath -name $serverName
}
The output is there, it causes network traffic, but the target folder remains empty. I'm not quite sure if this happened before as well, but I also get an error for the second server (only tried with 2 so far), saying that the target folder is in use??
I tried it with robocopy as well, which are the commented lines. I would even prefer robocopy, since that provides progress status (still need to figure out how to parse and pass it to the caller). But there I get a similar situation: it works if the scriptblock is called directly, but when called via start-job, it gives me this:
ERROR 123 (0x0000007B) Accessing Destination Directory C:\Users\abv\Documents\DBDumps_20180924\
The filename, directory name, or volume label syntax is incorrect.
User contributions licensed under CC BY-SA 3.0