# Copyright © 2011-2024 ARGSOFT TECHNOLOGY LIMITED
# Proprietary Information – All Rights Reserved.
#
#
#
#
#
# This is PROPRIETARY SOURCE CODE of ARGSOFT TECHNOLOGY LIMITED.
#
# The contents of this file may not be disclosed to third parties, copied or
# duplicated in any form, in whole or in part, without the prior written
# permission of ARGSOFT TECHNOLOGY LIMITED.
#
# RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
# and Computer Software clause at DFARS 252.227-7013, and/or in similar or
# successor clauses in the FAR, DOD or NASA FAR Supplement.
#
# Unpublished - rights reserved under the Copyright Laws of the United States
# and other countries.

#******************************************************************************************************
#
# Customize SQL Server, database and export directory
#
#******************************************************************************************************

$server = "NUC34\SQLExpress"
$database = "ArgentReports"

$exportDir = "C:\TEMP"

#******************************************************************************************************
#
# Report Tables to Export
#
#******************************************************************************************************

$tables = @("ARGSOFT_AR_REPORT", "ARGSOFT_AR_REPORT_COUNTERS", "ARGSOFT_AR_REPORT_NODE", 
		"ARGSOFT_AR_REPORT_VARIABLES", "ARGSOFT_AR_REPORT_FOLDER", 
		"ARGSOFT_AR_REPORT_GROUP", "ARGSOFT_AR_REPORT_GROUP_SEQUENCE")

#******************************************************************************************************
#
# Loop and export each table to CSV file
#
#******************************************************************************************************

$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

foreach($table in $tables) {
	$command = New-Object System.Data.SqlClient.SqlCommand
	$command.CommandText = "SELECT * FROM $table"
	$command.Connection = $connection

	$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
	$SqlAdapter.SelectCommand = $command
	$DataSet = New-Object System.Data.DataSet
	$SqlAdapter.Fill($DataSet)
	$connection.Close()

	$extractFile = Join-Path $exportDir ($table + ".csv")
	
	$DataSet.Tables[0]  | Export-Csv $extractFile -NoTypeInformation

	Write-Host $extractFile
}

$csv = Join-Path $exportDir "*.csv"

$zip = Join-Path $exportDir "ArgentReports.zip"

if (Test-Path $zip) {
   Remove-Item $zip -verbose
}

Compress-Archive -Path $csv -DestinationPath $zip

foreach($table in $tables) {
	$extractFile = Join-Path $exportDir ($table + ".csv")
	Remove-Item -Path $extractFile
}

Write-Host "Successfully export data of Argent Reports to file:" $zip


