Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sam Cogan slides and samples #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Write-Output "Creating job credentials..."
$jobAgent= Get-AzSqlElasticJobAgent -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo" -Name "sqlDemoAgent"
$LoginPasswordSecure = (ConvertTo-SecureString -String "password!123" -AsPlainText -Force)

$MasterCred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList "masteruser", $LoginPasswordSecure
$MasterCred = $JobAgent | New-AzSqlElasticJobCredential -Name "mastercred" -Credential $MasterCred

$JobCred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList "jobuser", $LoginPasswordSecure
$JobCred = $JobAgent | New-AzSqlElasticJobCredential -Name "jobcred" -Credential $JobCred

$LoginPasswordSecure = (ConvertTo-SecureString -String "4IvufzC64R3z" -AsPlainText -Force)
$JobCred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList "DBAdmin", $LoginPasswordSecure
$JobCred = $JobAgent | New-AzSqlElasticJobCredential -Name "dbadmin" -Credential $JobCred
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Write-Output "Creating test target groups..."
$jobAgent= Get-AzSqlElasticJobAgent -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo" -Name "sqlDemoAgent"
# Create ServerGroup target group
$TargetServerName="sqlagentdemo.database.windows.net"
$MasterCredName="mastercred"
$ServerGroup = $JobAgent | New-AzSqlElasticJobTargetGroup -Name 'ServerGroup1'
$ServerGroup | Add-AzSqlElasticJobTarget -ServerName $TargetServerName -RefreshCredentialName $MasterCredName
$ServerGroup | Add-AzSqlElasticJobTarget -ServerName $TargetServerName -Database "jobdatabase" -Exclude
13 changes: 13 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/PowerShell/3_CreateTableJob.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$jobAgent= Get-AzSqlElasticJobAgent -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo" -Name "sqlDemoAgent"
$serverGroupName="ServerGroup1"
$credentialName="jobcred"

Write-Output "Creating a new job"
$JobName = "CreateTablePS"
$Job = $JobAgent | New-AzSqlElasticJob -Name $JobName -RunOnce
$Job


Write-Output "Creating job steps"
$SqlText1 = "IF NOT EXISTS (SELECT * FROM sys.tables WHERE object_id = object_id('Step1Table')) CREATE TABLE [dbo].[Step1Table]([TestId] [int] NOT NULL);"
$Job | Add-AzSqlElasticJobStep -Name "step1" -TargetGroupName $serverGroupName -CredentialName $credentialName -CommandText $SqlText1
10 changes: 10 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/PowerShell/4_startjob.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$jobName="CreateTablePS"

$jobexecution= Start-AzSqlElasticJob -JobName $jobName -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo" -AgentName "sqlDemoAgent"
$jobexecution


# $jobName="GetFragmentation"

# $jobexecution= Start-AzSqlElasticJob -JobName $jobName -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo" -AgentName "sqlDemoAgent"
# $jobexecution
36 changes: 36 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/PowerShell/5_createOutputJob.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
$jobAgent= Get-AzSqlElasticJobAgent -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo" -Name "sqlDemoAgent"
$serverGroupName="ServerGroup1"
$credentialName="dbadmin"

$outputDatebase=Get-AzSqlDatabase -DatabaseName "OutputDatabase" -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo"

Write-Output "Creating a new job"
$JobName = "GetFragmentation"
$Job = $JobAgent | New-AzSqlElasticJob -Name $JobName -RunOnce
$Job


Write-Output "Creating job steps"



$SQLCommandString = @"
SELECT
DB_NAME() AS [Current Database],
SYSDATETIME() as [QueryDate],
dbschemas.[name] as 'Schema',
dbtables.[name] as 'Table',
dbindexes.[name] as 'Index',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]
AND indexstats.index_id = dbindexes.index_id
WHERE indexstats.database_id = DB_ID()
ORDER BY indexstats.avg_fragmentation_in_percent desc
"@


$Job | Add-AzSqlElasticJobStep -Name "step1" -TargetGroupName $serverGroupName -CredentialName $credentialName -CommandText $SQLCommandString -OutputDatabaseObject $outputDatebase -OutputCredentialName $credentialName -OutputTableName "Fragmentation4" -OutputSchemaName "dbo"
3 changes: 3 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/PowerShell/6_schedulJob.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$JobName = "GetFragmentation"
$job= Get-AzSqlElasticJob -Name $jobName -ResourceGroupName "SQLAgentDemos" -ServerName "SQLAgentDemo" -AgentName "sqlDemoAgent"
$job | Set-AzSqlElasticJob -IntervalType Day -IntervalCount 1 -StartTime (Get-Date) -Enable
47 changes: 47 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/PowerShell/CreateDBUsers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# In the master database (target server)
# - Create the master user login
# - Create the master user from master user login
# - Create the job user login
$TargetServer = Get-AzSqlServer -ResourceGroupName SQLAgentDemos -ServerName sqlagentdemo
$DB1=Get-AzSqlDatabase -ResourceGroupName SQLAgentDemos -DatabaseName "sqlAgentDemoDB01" -ServerName $TargetServer.ServerName
$DB2=Get-AzSqlDatabase -ResourceGroupName SQLAgentDemos -DatabaseName "sqlAgentDemoDB02" -ServerName $TargetServer.ServerName
$DB3=Get-AzSqlDatabase -ResourceGroupName SQLAgentDemos -DatabaseName "OutputDatabase" -ServerName $TargetServer.ServerName

$AdminLogin="DBAdmin"
$AdminPassword="4IvufzC64R3z"


$Params = @{
'Database' = 'master'
'ServerInstance' = $TargetServer.ServerName + '.database.windows.net'
'Username' = $AdminLogin
'Password' = $AdminPassword
'OutputSqlErrors' = $true
'Query' = "CREATE LOGIN masteruser WITH PASSWORD='password!123'"
}
Invoke-SqlCmd @Params
$Params.Query = "CREATE USER masteruser FROM LOGIN masteruser"
Invoke-SqlCmd @Params
$Params.Query = "CREATE LOGIN jobuser WITH PASSWORD='password!123'"
Invoke-SqlCmd @Params

# For each of the target databases
# - Create the jobuser from jobuser login
# - Make sure they have the right permissions for successful script execution
$TargetDatabases = @($Db1.DatabaseName,$Db2.DatabaseName, $Db3.DatabaseName )
$CreateJobUserScript = "CREATE USER jobuser FROM LOGIN jobuser"
$GrantAlterSchemaScript = "GRANT ALTER ON SCHEMA::dbo TO jobuser"
$GrantCreateScript = "GRANT CREATE TABLE TO jobuser"

$TargetDatabases | % {
$Params.Database = $_

$Params.Query = $CreateJobUserScript
Invoke-SqlCmd @Params

$Params.Query = $GrantAlterSchemaScript
Invoke-SqlCmd @Params

$Params.Query = $GrantCreateScript
Invoke-SqlCmd @Params
}
13 changes: 13 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/PowerShell/cleanup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$resourceGroup="SQLAgentDemos"
$ServerName= "SQLAgentDemo"
$AgentName= "sqlDemoAgent"

Get-AzSqlElasticJob -ResourceGroupName $resourceGroup -ServerName $ServerName -AgentName $AgentName | Remove-AzSqlElasticJob -Force

Remove-AzSqlElasticJobTargetGroup -Name ServerGroup1 -ResourceGroupName $resourceGroup -ServerName $ServerName -AgentName $AgentName -Force


Remove-AzSqlElasticJobCredential -ResourceGroupName $resourceGroup -ServerName $ServerName -AgentName $AgentName -Name "mastercred"
Remove-AzSqlElasticJobCredential -ResourceGroupName $resourceGroup -ServerName $ServerName -AgentName $AgentName -Name "jobcred"
Remove-AzSqlElasticJobCredential -ResourceGroupName $resourceGroup -ServerName $ServerName -AgentName $AgentName -Name "dbadmin"

4 changes: 4 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/PowerShell/createJobAgent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Write-Output "Creating job agent..."
$AgentName = "sqlDemoAgent"
$JobDatabase = Get-AzSqlDatabase -ResourceGroupName "SQLAgentDemos" -ServerName "sqlagentdemo" -databasename "jobdatabase"
$JobAgent = $JobDatabase | New-AzSqlElasticJobAgent -Name $AgentName
14 changes: 14 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/SQL/CreateJobCredentials.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--Connect to the job database specified when creating the job agent

-- Create a db master key if one does not already exist, using your own password.
CREATE MASTER KEY ENCRYPTION BY PASSWORD='GlxRvu27glE8tEIHny53';

-- Create a database scoped credential.
CREATE DATABASE SCOPED CREDENTIAL myjobcred WITH IDENTITY = 'jobuser',
SECRET = 'password!123';
GO

-- Create a database scoped credential for the master database of server1.
CREATE DATABASE SCOPED CREDENTIAL mymastercred WITH IDENTITY = 'masteruser',
SECRET = 'password!123';
GO
13 changes: 13 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/SQL/createIndexRebuildJob.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


--Connect to the job database specified when creating the job agent

--Add job for create table
EXEC jobs.sp_add_job @job_name='RebuildAllIndexes', @description='Rebuild All Indexes'

-- Add job step for create table
EXEC jobs.sp_add_jobstep @job_name='RebuildIndex',
@command=N'ALTER INDEX ALL ON Table_name
REBUILD ;',
@credential_name='myjobcred',
@target_group_name='ServerGroup1'
12 changes: 12 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/SQL/createNewTableJob.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--Connect to the job database specified when creating the job agent

--Add job for create table
EXEC jobs.sp_add_job @job_name='CreateTableTest', @description='Create Table Test'

-- Add job step for create table
EXEC jobs.sp_add_jobstep @job_name='CreateTableTest',
@command=N'IF NOT EXISTS (SELECT * FROM sys.tables
WHERE object_id = object_id(''Test''))
CREATE TABLE [dbo].[Test]([TestId] [int] NOT NULL);',
@credential_name='myjobcred',
@target_group_name='ServerGroup1'
15 changes: 15 additions & 0 deletions Sessions/Sam Cogan/ElasticJobs/SQL/createServerGroup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Connect to the job database specified when creating the job agent

-- Add a target group containing server(s)
EXEC jobs.sp_add_target_group 'ServerGroup1'

-- Add a server target member
EXEC jobs.sp_add_target_group_member
'ServerGroup1',
@target_type = 'SqlServer',
@refresh_credential_name='mymastercred', --credential required to refresh the databases in server
@server_name='sqlagentdemo.database.windows.net'

--View the recently created target group and target group members
SELECT * FROM jobs.target_groups WHERE target_group_name='ServerGroup1';
SELECT * FROM jobs.target_group_members WHERE target_group_name='ServerGroup1';
Binary file not shown.
6 changes: 6 additions & 0 deletions Sessions/Sam Cogan/Functions/.vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
Binary file added Sessions/Sam Cogan/Functions/.vs/slnx.sqlite
Binary file not shown.
Loading