Manual Log Shipping on SQL server 2014

Hello Don,

I’m working on a project that will manually failover and failback our soon to be deployed Log Shipping solution for SQL server 2014.
I’ve already figured out what he’ll do and write 90% of the code that i’ll present as our permanent solution.
Yet, i’d like to have your feedbacks on the way i’m writing this. See, this will be THE solution used for my company, and as a new hired it’s my way in the company for a more permanent position. So i’m betting all in this :-).

So my code here : SQL server 2014 Log Shipping Manual Fail Over · GitHub is the function that i came up with to do the things i want. All seemed to be running fine until i had to add a functionnality ( line 51 -55 ).
It’ll always return an error saying that the path (specified in the variable) cannot be null or empty. Which make senses.
running the selected code from ISE runs the way i want, which got me puzzled.

Thus i’d like to get a little of your time asking this :
1- Am I doing this wrong? Is the way i approach it not the way i should?
2- Is the code structure easy to understand?

Thanks a ton for your site. I’ve learned powershell from your books and site for 1 month now and i’m really excited over it :-)).

Regards,

For clarity purposes, below is the error message i get when running the code:

Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At c:\scriptslib\Failover.ps1:56 char:11
+ Copy-Item $rlatesttrn $DRbicdir2 -ErrorAction Stop
+           ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

Thanks.

Rename-Item does not output a path. Use Join-Path to create a new path for the renamed file. This should resolve your error for lines 53 - 55. Use verb-noun syntax for your function name (ex: Set-Failover)

($latesttrn = Get-ChildItem $LSiddir -Filter *.trn | sort LastWriteTime | select -Last 1)
Rename-Item $latesttrn.FullName user_$id.trn -ErrorAction Stop
$newpath = join-path -Path $latesttrn.DirectoryName -ChildPath "user_$id.trn"
Copy-Item $newpath $DRiddir2 -ErrorAction Stop

Hello random commandline,
The join-path command was a good idea thanks for the heads-up!
But i have a different problem now: instead of copying the file to the directory mentioned in the copy-item command, the file was copied on the root C:\ drive causing the remaining actions to failed.
Can you see any reason why it does that? I’m kinda short on idea here.

Verify your ‘-Destination’ and add ‘-WhatIf’ at the end of your Copy-Item cmdlet. What destination is displayed during the ‘-WhatIf’ test?

Again, thanks. The destination variable name didn’t match the one in the command.
Your advises were spot on! Thanks.