Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • <SQL-SERVER\INSTANCE> = Your SQL Server instance name e.g. SQLSRV01\SQLEXPRESS

  • <database_name> = your ResourceXpress database name e.g. rxpressexpress

Code Block
languagepowershell
# Initial values, replace the {name} with proper values
$connectionString = "Data Source=<SQL-SERVER\INSTANCE-NAME>;Initial Catalog=<database_name>;Integrated Security=False; User ID=dbsupport1;Password=D8Supp0rt!;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"
$timeout = 300 # in seconds
$tenantId = 1

Write-Output('DB Maintenance - v0.1 started')
# Instantiate the connection to the SQL Database
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = $connectionString

# Define the SQL command to run
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandTimeout = $timeout
$sqlCommand.Connection = $sqlConnection
$sqlCommand.Parameters.AddWithValue("@TenantId",$tenantId)

# Opening the SQL connection
try{
    $sqlConnection.Open()
}
catch{
    Write-Output('An error occurred in opening connection'+ $_.Exception.ToString())
    Exit 1
}

# Executing the stored procedures
$spList = 'ArchiveConnectivityLog','ArchiveBookingSystemConnectivityLog','ArchiveQubiActivityLog','ArchiveBookings','ArchiveResourceMessages','ArchiveEquipmentProblems','DeleteConnectivityLogArchive','DeleteBookingSystemConnectivityLogArchive','DeleteQubiActivityLogArchive','DeleteBookingsArchive','DeleteResourceMessagesArchive','DeleteEquipmentProblemsArchive','DeleteArchiveLog',‘ArchiveGraphSubscription’,’ArchiveSchedulerInfo’,‘ArchiveAuditLog’,’ArchiveUserLoginHistory’,‘DeleteAuditLogArchive’,‘DeleteUserLoginHistoryArchive’

foreach($sp in $spList){
     try{
        Write-Output('About to execute the sp ' + $sp)
        $sqlCommand.CommandText= 'exec [dbo].[' + $sp + '] @TenantId'
		$result = $sqlCommand.ExecuteNonQuery()
        Write-Output($sp + ' executed successfully')
    }
    catch{
        Write-Output('An error occurred in executing the sp ' + $sp + ' ' + $_.Exception.ToString())
    }
}

# Close the SQL connection
$sqlConnection.Close()
Write-Output('DB Maintenance is completed successfully')

...

The number of records deleted and or archived are recorded in the database in the ArchiveLog table, to view the logs execute the below SQL query against the ResourceXpress database.

Code Block
languagesql
select * from dbo.ArchiveLog

...

Document Updates

V2023.3

Two values have been removed from the $spList.

  • Status
    colourRed
    titleRemoved
    DeleteGraphSubscription

  • Status
    colourRed
    titleRemoved
    DeleteSchedulerInfo