functionNew-DbaLogShippingSecondaryPrimary{<#
.SYNOPSIS
New-DbaLogShippingPrimarySecondary sets up the primary information for the primary database.

.DESCRIPTION
New-DbaLogShippingPrimarySecondary sets up the primary information, adds local and remote monitor links,
and creates copy and restore jobs for the specified primary database.
This is executed on the secondary server.

.PARAMETER SqlInstance
SQL Server instance. You must have sysadmin access and server version must be SQL Server version 2000 or greater.

.PARAMETER BackupSourceDirectory
The directory where transaction log backup files from the primary server are stored.

.PARAMETER BackupDestinationDirectory
The directory on the secondary server where backup files are copied to.

.PARAMETER CopyJob
The name to use for the SQL Server Agent job being created to copy transaction log backups to the secondary server.

.PARAMETER CopyJobID
The UID associated with the copy job on the secondary server.

.PARAMETER FileRetentionPeriod
The length of time, in minutes, that a backup file is retained on the secondary server in the path specified by the BackupDestinationDirectory parameter before being deleted.
The default is 14420.

.PARAMETER MonitorServer
Is the name of the monitor server. The default is the secondary server.

.PARAMETER MonitorServerLogin
Is the username of the account used to access the monitor server.

.PARAMETER MonitorServerPassword
Is the password of the account used to access the monitor server.

.PARAMETER MonitorServerSecurityMode
The security mode used to connect to the monitor server. Allowed values are 0, "sqlserver", 1, "windows"
The default is 1 or Windows.

.PARAMETER PrimaryServer
The name of the primary instance of the Microsoft SQL Server Database Engine in the log shipping configuration.

.PARAMETER PrimaryDatabase
Is the name of the database on the primary server.

.PARAMETER RestoreJob
Is the name of the SQL Server Agent job on the secondary server that restores the backups to the secondary database.

.PARAMETER RestoreJobID
The UID associated with the restore job on the secondary server.

.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.

.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.

.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

.PARAMETER Force
The force parameter will ignore some errors in the parameters and assume defaults.
It will also remove the any present schedules with the same name for the specific job.

# Try connecting to the instancetry{$ServerSecondary=Connect-SqlInstance-SqlInstance$SqlInstance-SqlCredential$SqlCredential}catch{Stop-Function-Message"Error occurred while establishing connection to $instance"-CategoryConnectionError-ErrorRecord$_-Target$SqlInstance-Continue}

# Try connecting to the instancetry{$ServerPrimary=Connect-SqlInstance-SqlInstance$PrimaryServer-SqlCredential$PrimarySqlCredential}catch{Stop-Function-Message"Error occurred while establishing connection to $instance"-CategoryConnectionError-ErrorRecord$_-Target$PrimaryServer-Continue}

# Check if the backup UNC path is correct and reachableif([bool]([uri]$BackupDestinationDirectory).IsUnc-and$BackupDestinationDirectory-notmatch'^\\(?:\\[^<>:`"/\\|?*]+)+$'){Stop-Function-Message"The backup destination path should be formatted in the form \\server\share."-Target$SqlInstancereturn}else{if(-not((Test-Path$BackupDestinationDirectory-PathTypeContainer-IsValid)-and((Get-Item$BackupDestinationDirectory).PSProvider.Name-eq'FileSystem'))){Stop-Function-Message"The backup destination path is not valid or can't be reached."-Target$SqlInstancereturn}}

# Check of the MonitorServerSecurityMode value is of type string and set the integer valueif($MonitorServerSecurityMode-notin0,1){$MonitorServerSecurityMode=switch($MonitorServerSecurityMode){"WINDOWS"{1}"SQLSERVER"{0}}Write-Message-Message"Setting monitor server security mode to $MonitorServerSecurityMode."-LevelVerbose}

# Check the MonitorServerSecurityMode if it's SQL Server authenticationif($MonitorServerSecurityMode-eq0-and-not$MonitorCredential){Stop-Function-Message"The MonitorServerCredential cannot be empty when using SQL Server authentication."-Target$SqlInstance-Continuereturn}elseif($MonitorServerSecurityMode-eq0-and$MonitorCredential){# Get the username and password from the credential$MonitorLogin=$MonitorCredential.UserName$MonitorPassword=$MonitorCredential.GetNetworkCredential().Password

# Check if the user is in the databaseif($ServerSecondary.Databases['master'].Users.Name-notcontains$MonitorLogin){Stop-Function-Message"User $MonitorLogin for monitor login must be in the master database."-Target$SqlInstance-Continuereturn}}

# Check if the database is present on the primary sql serverif($ServerPrimary.Databases.Name-notcontains$PrimaryDatabase){Stop-Function-Message"Database $PrimaryDatabase is not available on instance $PrimaryServer"-Target$PrimaryServer-Continuereturn}

# Execute the query to add the log shipping primaryif($PSCmdlet.ShouldProcess($SqlServer,("Configuring logshipping making settings for the primary database to secondary database on $SqlInstance"))){try{Write-Message-Message"Configuring logshipping making settings for the primary database."-LevelVerboseWrite-Message-Message"Executing query:`n$Query"-LevelVerbose$ServerSecondary.Query($Query)}catch{Write-Message-Message"$($_.Exception.InnerException.InnerException.InnerException.InnerException.Message)"-LevelWarningStop-Function-Message"Error executing the query.`n$($_.Exception.Message)"-ErrorRecord$_-Target$SqlInstance-Continue}}