Script per estrarre le stored

This commit is contained in:
Gaetano Savo 2025-06-05 15:09:44 +02:00
parent 0fc74c5dd6
commit 9edbd0a91d
6 changed files with 176 additions and 13 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
logs/
logs/
stored*/

31
script/conf.ps1 Normal file
View File

@ -0,0 +1,31 @@
# Configurazione database
$config = @{
# Credenziali database
DbUser = "sa"
DbPassword = "_p1sap1a"
# DbHost = "DATABASE_PDC_LOCALE"
DbHost = "testbes.armundia.com"
DbName = "C6StampeCentralizzate"
# Directory di log
LogDir = Join-Path $PSScriptRoot "..\logs"
}
# Esporta le variabili come variabili d'ambiente
$env:dbUser = $config.DbUser
$env:dbPassword = $config.DbPassword
$env:dbHost = $config.DbHost
$env:dbName = $config.DbName
$env:logDir = $config.LogDir
# Funzione per ottenere la connection string
function Get-DatabaseConnectionString {
return "Server=$($config.DbHost);Database=$($config.DbName);User Id=$($config.DbUser);Password=$($config.DbPassword);"
}
# Crea directory di log se non esiste
if (-not (Test-Path $config.LogDir)) {
New-Item -ItemType Directory -Path $config.LogDir | Out-Null
}
# La configurazione sarà disponibile quando lo script viene dot-sourced

View File

@ -1,24 +1,23 @@
# SQL Server connection parameters
# Importa configurazione
. "$PSScriptRoot\conf.ps1"
# Imposta directory di lavoro
$env:compareDir = $PSScriptRoot
$env:workdir = Split-Path -Path $env:compareDir -Parent
$env:logDir = Join-Path $env:workdir "logs"
$env:scriptDir = Join-Path $env:workdir "script"
if (-not (Test-Path $env:logDir)) {
New-Item -ItemType Directory -Path $env:logDir | Out-Null
}
$sqlbase = Join-Path $env:workdir "sql"
$env:dbUser = "sa"
$env:dbPassword = "_p1sap1a"
$env:dbHost = "DATABASE_PDC_LOCALE"
$scriptName = $MyInvocation.MyCommand.Name
$logFile = Join-Path $env:logDir "${scriptName}.log" # for debug
"start" | Out-File -FilePath $logFile
# List of SQL files to execute in order
$sqlFiles = @(
"C6SC01_creadb.sql",
"C6SC08_creauser.sql",
"StampeCentralizzateBackupRidotto.sql",
# "C6SC01_creadb.sql",
# "C6SC08_creauser.sql",
# "StampeCentralizzateBackupRidotto.sql",
# "enable_sa_full.sql",
"enable_F701264_full.sql",
""
)
@ -52,7 +51,7 @@ foreach ($fileName in $sqlFiles) {
IF ( $esitoComando -eq 0 )
{
"run $fileName " | Out-File -FilePath $logFile -Append
Remove-Item -Path $fileLog | Out-File -FilePath $logFile -Append
# Remove-Item -Path $fileLog | Out-File -FilePath $logFile -Append
(Get-Date).ToString("yyyy-MM-dd_HH:mm:ss")+" item: $fileSql completed risultato in $fileLog " | Out-File -FilePath $logFile -Append
} else {
$(Get-Date -Format "yyyy-MM-dd_HH:mm:ss")+" $esitoComando"

57
script/estrae_stored.ps1 Normal file
View File

@ -0,0 +1,57 @@
# Importa configurazione
. "$PSScriptRoot\conf.ps1"
# Imposta il percorso di output nella cartella sql
$OutputPath = Join-Path $env:workdir "sql\stored"
# Verifica che la directory di output esista, altrimenti la crea
if (-not (Test-Path -Path $OutputPath)) {
New-Item -ItemType Directory -Path $OutputPath | Out-Null
}
# Query per estrarre le stored procedures
$query = @"
SELECT
OBJECT_SCHEMA_NAME(object_id) as SchemaName,
OBJECT_NAME(object_id) as ProcedureName,
OBJECT_DEFINITION(object_id) as ProcedureDefinition
FROM sys.procedures
ORDER BY SchemaName, ProcedureName
"@
try {
# Importa il modulo SqlServer se non è già caricato
if (-not (Get-Module -Name SqlServer)) {
Import-Module SqlServer
}
# Esegui la query per ottenere le stored procedures
$storedProcedures = Invoke-Sqlcmd -ServerInstance $env:dbHost -Database $env:dbName -Query $query -Username $env:dbUser -Password $env:dbPassword
# Per ogni stored procedure trovata
foreach ($sp in $storedProcedures) {
$schemaName = $sp.SchemaName
$procName = $sp.ProcedureName
$definition = $sp.ProcedureDefinition
# Crea il nome del file
$fileName = Join-Path $OutputPath "${schemaName}_${procName}.sql"
# Scrivi il contenuto nel file
$content = @"
-- Schema: $schemaName
-- Stored Procedure: $procName
-- Data Estrazione: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
$definition
"@
$content | Out-File -FilePath $fileName -Encoding UTF8
Write-Host "Creato file per stored procedure: $schemaName.$procName"
}
Write-Host "`nEstrazione completata con successo!"
} catch {
Write-Error "Errore durante l'estrazione delle stored procedures: $_"
exit 1
}

View File

@ -0,0 +1,43 @@
-- Abilita l'autenticazione mista (Windows e SQL)
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2
GO
-- Disabilita le policy di password a livello server
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'PasswordComplexity', REG_DWORD, 0
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'MinPasswordLength', REG_DWORD, 0
GO
IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = 'F701264')
BEGIN
CREATE LOGIN [F701264] WITH PASSWORD = 'Contr$ei2023'
END
GO
-- Abilita l'account F701264 e imposta password
ALTER LOGIN [F701264] WITH
PASSWORD = N'contrsei',
CHECK_POLICY = OFF,
CHECK_EXPIRATION = OFF
GO
ALTER LOGIN [F701264] ENABLE
GO
-- Assegna il ruolo sysadmin all'utente F701264
ALTER SERVER ROLE [sysadmin] ADD MEMBER [F701264]
GO
-- Concedi permessi TCP/IP
EXEC sp_configure 'remote access', 1
GO
RECONFIGURE
GO
-- Abilita il protocollo TCP/IP
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'remote access', 1
GO
RECONFIGURE
GO

32
sql/enable_sa_full.sql Normal file
View File

@ -0,0 +1,32 @@
-- Abilita l'autenticazione mista (Windows e SQL)
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2
GO
-- Abilita l'account SA e imposta password
ALTER LOGIN [sa] WITH
PASSWORD = N'_p1sap1a',
CHECK_POLICY = OFF,
CHECK_EXPIRATION = OFF
GO
ALTER LOGIN [sa] ENABLE
GO
-- Assegna il ruolo sysadmin all'utente sa
ALTER SERVER ROLE [sysadmin] ADD MEMBER [sa]
GO
-- Concedi permessi TCP/IP
EXEC sp_configure 'remote access', 1
GO
RECONFIGURE
GO
-- Abilita il protocollo TCP/IP
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'remote access', 1
GO
RECONFIGURE
GO