My Script runs forever never ends

$Server = hostname

function Set-Email { $Params = @{
   Subject = "$Server SQLC DB Copy"
   Body = "$Bigbody"
   From = "no-reply@mynecom"
   To = "systems-alert@mynetom"
   smtpserver = "InternalRelay.MYNET.COM"
   } 
Send-MailMessage @Params
}

$Source = "F:\SQL-BackupData\"
$TargetFolder = "D:\SQLCopy\TGCS014-N1-SQLC"
$mydate = get-date -Uformat %m%d%y
$mytime = get-date -Uformat %H%M
$LocalDrive = "F:\SQL-BackupData"
$pathExists = Test-Path -Path $LocalDrive

If (-not ($pathExists)) {
$Node += Get-ClusterResource -name "Cluster Disk 2" | format-list -property ownernode | out-string
$Bigbody = Write-Output "SQLC DB Copy On" $Node | out-string
set-email
}
Else
{
net use D: \\ba-069dc4\usb1-1.3share1 /user:admin xxxxxxxxxx

 $Bigbody = new-item -itemtype Directory -Path "$TargetFolder\$Server-$($mydate)-$($mytime)" | out-string
 $Bigbody += copy-item $source -Destination "$TargetFolder\$Server-$($mydate)-$($mytime)" -recurse | out-string

$HowOld = -90
$Path = $TargetFolder
$filesToDelete = Get-ChildItem -Path $Path -File -Recurse | Where {$_.lastwritetime -lt (Get-Date).AddDays($HowOld)}
$Bigbody += Write-Output "Number of Files to Be Deleted" | out-string
$bigbody += $filesToDelete.Count | out-string
$filesToDelete | Remove-Item -Force

$Bigbody += get-childitem "$TargetFolder\$Server-$($mydate)-$($mytime)" -Recurse | out-string
$Bigbody = get-childitem "$TargetFolder\$Server-$($mydate)-$($mytime)" -Recurse | out-string

net use D: /del

Set-Email

}

 

How can I find out where this is hanging?

It is run using Task Scheduler every night

I have disabled it for now.

Thank you

 

Tom

 

 

 

 

Add logging to your scripts and figure it out at which process it is stuck, so it will be easy to debug the code especially when you are running the scripts in Task Scheduler.

[pre] “<verbose log>” | Out-File -FilePath C:\test.log -Encoding ascii -Append [/pre]

Kiran

 

It is stuck on line 30

 

$Bigbody += copy-item $source -Destination “$TargetFolder$Server-$($mydate)-$($mytime)” -recurse | out-string

 

Any ideas?

 

Thank you

 

Tom

Can you add the -Verbose parameter to the Copy-Item cmdlet to see what is beeing copied?

Thanks

I now can see the files being copied.

Some files at almost 10 GB in size and it takes a long time to copy them

Wonder if robocopy would be a faster method than the copy-item command

Or anyway to speed up the copy-item command?

 

Thoughts?

 

[quote quote=136629]
Wonder if robocopy would be a faster method than the copy-item command
Or anyway to speed up the copy-item command?
Thoughts?[/quote]

Robocopy is just an executable already optimized and capable of handling nearly every use case of copying, moving or mirroring folder trees. You can do nearly any of these actions in a single line and without writing long scripts.
However, despite I never thought about doing a speed comparison test between robocopy and copy-item, I would be surprised that one would be faster than the other when copying or moving a single big file of 10Gb.
This is more a standard bottleneck issue outside the scope of this forum.
Among them:

  • one common disk controller or one controller per disk
  • disk controller cache settings
  • disk controller bus type
  • disk controller capabilities

Thank you all for your help.

I just changes the times the job is scheduled and they all worked.

After using the -verbose it helped me determine which script was hanging.

I have 5 scripts that do the same function copy files from my SQL server to a external storage device.

They ran at a similar time frame and all were trying to get access to the external device at the same time sometimes.

The files 10 GB in size took aa while. So I made that job run last after the others finished

Thanks again,

this can be marked as resolved.

Tom