# Script per elencare gli oggetti del database # Importa la configurazione . $PSScriptRoot\conf.ps1 # Imposta il percorso di output nella cartella sql $OutputPath = Join-Path $env:workdir "transito" # Crea la directory transito se non esiste if (-not (Test-Path -Path $OutputPath)) { New-Item -ItemType Directory -Path $OutputPath -Force | Out-Null } # Carica l'assembly System.Data Add-Type -AssemblyName System.Data try { # Query per ottenere l'elenco degli oggetti $query = @" SELECT CASE type WHEN 'U' THEN 'Table' WHEN 'V' THEN 'View' WHEN 'P' THEN 'Stored Procedure' WHEN 'FN' THEN 'Function' WHEN 'TR' THEN 'Trigger' ELSE type END AS ObjectType, SCHEMA_NAME(schema_id) AS SchemaName, name AS ObjectName FROM sys.objects WHERE type IN ('U', 'V', 'P', 'FN', 'TR') ORDER BY ObjectType, SchemaName, ObjectName "@ # Esegui la query e salva i risultati $connectionString = "Server=$($env:dbHostInput);Database=$($env:dbNameInput);User Id=$($env:dbUserInput);Password=$($env:dbPasswordInput);TrustServerCertificate=True" $connection = New-Object System.Data.SqlClient.SqlConnection($connectionString) $command = New-Object System.Data.SqlClient.SqlCommand($query, $connection) $adapter = New-Object System.Data.SqlClient.SqlDataAdapter($command) $dataset = New-Object System.Data.DataSet $adapter.Fill($dataset) | Out-Null $results = $dataset.Tables[0] # Prepara l'output $output = "Elenco oggetti del database $($env:DbName) su $($env:DbHostInput) il $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`r`n" $output += "Tipo;Schema;Nome`r`n" # Formatta e aggiungi ogni oggetto all'output foreach ($row in $results) { $output += "$($row.ObjectType);$($row.SchemaName);$($row.ObjectName)`r`n" } # Scrivi il risultato su file $output | Out-File -FilePath (Join-Path -Path $OutputPath -ChildPath "_elenco_oggetti.csv") -Encoding UTF8 Write-Host "Elenco oggetti salvato correttamente in _elenco_oggetti.csv" } catch { Write-Error "Si รจ verificato un errore: $_" exit 1 }