# Step 1: Define SQL connection to the master database
$sqlConnection = New-Object System.Data.SqlClient.SQLConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=master")
# Step 2: Create SQL command object
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand
$sqlCommand.Connection = $sqlConnection
# Step 3: Ensure 'NT AUTHORITY\SYSTEM' login exists
$sqlCommand.CommandText = "IF NOT EXISTS (SELECT * FROM [master].[sys].[server_principals] WHERE name = 'NT AUTHORITY\SYSTEM') CREATE LOGIN [NT AUTHORITY\SYSTEM] FROM WINDOWS"
$sqlConnection.Open()
if ($sqlCommand.ExecuteNonQuery() -ne 0) {
echo "Failed: Step 1"
}
# Step 4: Create the PADS4FIDS database if it doesn't exist
$sqlCommand.CommandText = "IF DB_ID('PADS4FIDS') IS NULL CREATE DATABASE [PADS4FIDS];"
if ($sqlCommand.ExecuteNonQuery() -ne 0) {
echo "Failed: Step 2"
}
# Step 5: Assign db_owner role to NT AUTHORITY\SYSTEM in the new database
$sqlCommand.CommandText = "USE PADS4FIDS; EXEC sp_addrolemember 'db_owner', 'NT AUTHORITY\SYSTEM'"
if ($sqlCommand.ExecuteNonQuery() -ne 0) {
echo "Failed: Step 3"
}
# Step 6: Close SQL connection
$sqlConnection.Close()