%3CLINGO-SUB%20id%3D%22lingo-sub-1185495%22%20slang%3D%22en-US%22%3ERe%3A%20Fundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1185495%22%20slang%3D%22en-US%22%3E%3CP%3EGreat%20information!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20an%20extra%20word%20in%20this%20sentence%3F%3C%2FP%3E%3CP%3E%22%3CSPAN%3E%26nbsp%3BFurther%20jobs%20with%20more%20than%20one%20step%20can%20use%20a%20diffuse%20a%20different%20job%20credential%20for%20each%20step.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%22%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1185509%22%20slang%3D%22en-US%22%3ERe%3A%20Fundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1185509%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F553780%22%20target%3D%22_blank%22%3E%40Kate_Smith%3C%2FA%3E%26nbsp%3B-%20there%20is%20an%20error%20in%20one%20of%20your%20PowerShell%20steps%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%5C%26gt%3B%20%24refreshCred%20%3D%20%24jobAgent%20%7C%20New-AzSqlElasticJobCredential%20-Name%20%22refreshcredential%20%22%20-Credential%20%24masterCred%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20haven't%20defined%20%24masterCred%20-%20it%20should%20be%20%24refreshCred%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1185542%22%20slang%3D%22en-US%22%3ERe%3A%20Fundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1185542%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F373735%22%20target%3D%22_blank%22%3E%40platar%3C%2FA%3E%20%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F161433%22%20target%3D%22_blank%22%3E%40Martin%20Cairney%3C%2FA%3E%20-%20thanks%20for%20the%20feedback!%20Both%20issues%20have%20been%20addressed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1177939%22%20slang%3D%22en-US%22%3EFundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1177939%22%20slang%3D%22en-US%22%3E%3CTABLE%20style%3D%22background-color%3A%20transparent%3B%20border-collapse%3A%20collapse%3B%20border-spacing%3A%200px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20width%3A%20100%25%3B%20word-spacing%3A%200px%3B%20border-style%3A%20double%3B%22%20border%3D%221%22%20width%3D%22100%25%22%3E%0A%3CTBODY%20style%3D%22box-sizing%3A%20border-box%3B%22%3E%0A%3CTR%20style%3D%22box-sizing%3A%20border-box%3B%22%3E%0A%3CTD%20width%3D%22100%25%22%20style%3D%22border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20box-sizing%3A%20border-box%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-weight%3A%20300%3B%20line-height%3A%201.7142%3B%20min-width%3A%2040px%3B%20word-break%3A%20normal%3B%20padding%3A%201px%205px%201px%205px%3B%20border%3A%201px%20solid%20%23c4c4c4%3B%22%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%22%20color%3D%22%23ff0000%22%3EElastic%20Jobs%20are%20currently%20in%20public%20preview.%26nbsp%3B%20The%20feature%20is%20ready%20and%20we%20are%20preparing%20to%20make%20it%20officially%20generally%20available.%26nbsp%3B%20No%20extra%20steps%20are%20required%20to%20get%20started.%3C%2FFONT%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20my%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Felastic-jobs-in-azure-sql-database-what-and-why%2Fba-p%2F1177902%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eprevious%20post%3C%2FA%3E%2C%20I%20explained%20what%20Elastic%20Jobs%20are%20and%20why%20you%20might%20consider%20using%20them.%20In%20this%20post%2C%20I%E2%80%99ll%20be%20discussing%20two%20fundamental%20concepts%20that%20you%20should%20understand%20before%20creating%20elastic%20jobs%3A%20Idempotence%2C%20and%20Credentials.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23000000%22%3E%3CSPAN%3E%3CFONT%20size%3D%225%22%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20color%3D%22%23333333%22%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E%3CFONT%20size%3D%225%22%20style%3D%22box-sizing%3A%20border-box%3B%22%3EIdempotence%3A%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIt%20is%20very%20important%20that%20the%20T-SQL%20scripts%20being%20executed%20by%20Elastic%20Jobs%20be%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FIdempotence%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eidempotent%3C%2FA%3E.%26nbsp%3B%20This%20means%20that%20if%20they%20are%20run%20multiple%20times%20(by%20accident%20or%20intentionally)%20they%20won't%20fail%20and%20won%E2%80%99t%20produce%20unintended%20results.%20If%20an%20elastic%20job%20has%20some%20side%20effects%2C%20and%20gets%20run%20more%20than%20once%2C%20it%20could%20fail%20or%20cause%20other%20unintended%20consequences%20(like%20consuming%20double%20the%20resources%20needed%20for%20a%20large%20statistics%20update).%26nbsp%3B%20One%20way%20to%20ensure%20idempotence%20is%20to%20make%20sure%20that%20you%20check%20if%20something%20already%20exists%20before%20trying%20to%20create%20it.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIF%20NOT%20EXISTS%3C%2FP%3E%0A%3CP%3E(%3CSELECT%20object%3D%22%22%20here%3D%22%22%20you%3D%22%22%20might%3D%22%22%20create%3D%22%22%3E)%3C%2FSELECT%3E%3C%2FP%3E%0A%3CP%3ECREATE%3C%2FP%3E%0A%3CP%3E%3COBJECT%20you%3D%22%22%20intend%3D%22%22%20to%3D%22%22%20create%3D%22%22%3E%3C%2FOBJECT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20good%20way%20to%20test%20if%20your%20job%20is%20idempotent%20is%20to%20run%20your%20script%20multiple%20times%20on%20a%20single%20target%20and%20see%20if%20the%20operation%20fails%20on%20subsequent%20executions%2C%20or%20to%20make%20sure%20that%2C%20if%20a%20single%20run%20consumes%20many%20resources%2C%20repeated%20runs%20do%20not%20also%20consume%20those%20resources.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%20color%3D%22%23000000%22%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20color%3D%22%23333333%22%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E%3CFONT%20size%3D%225%22%20style%3D%22box-sizing%3A%20border-box%3B%22%3ECredentials%3A%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThere%20are%20two%20categories%20of%20credentials%20that%20are%20needed%20to%20create%20and%20run%20Elastic%20Jobs.%26nbsp%3B%20The%20%3CEM%3ERefresh%20Credential%3C%2FEM%3E%20and%20the%20%3CEM%3EJob%20Credential%3C%2FEM%3E.%26nbsp%3B%20In%20practice%2C%20a%20single%20elastic%20job%20will%20have%20one%20refresh%20credential%2C%20and%20each%20job%20step%20must%20have%20one%20job%20credential.%26nbsp%3B%20The%20refresh%20credential%20and%20the%20job%20credential%20do%20not%20have%20to%20be%20the%20same%20credential.%26nbsp%3B%20Further%2C%20jobs%20with%20more%20than%20one%20step%20can%20use%20a%20different%20job%20credential%20for%20each%20step.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%E2%80%AF%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1409784424%22%20id%3D%22toc-hId--1409784424%22%20id%3D%22toc-hId--1409784424%22%20id%3D%22toc-hId--1409784424%22%20id%3D%22toc-hId--1409784424%22%20id%3D%22toc-hId--1409784424%22%20id%3D%22toc-hId--1409784424%22%20id%3D%22toc-hId--1409784424%22%3E%3CSPAN%3E%3CEM%3ERefresh%20Credential%3C%2FEM%3E%3C%2FSPAN%3E%3CSPAN%3E%3CEM%3E%26nbsp%3B%3C%2FEM%3E%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EThe%20elastic%20job%20agent%20needs%20one%20credential%20that%20provides%20enough%20access%20to%20all%20servers%20within%20the%20target%20group%20to%20enumerate%20the%20databases%20there.%26nbsp%3B%20In%20some%20examples%20you%20may%20see%2C%20this%20credential%20is%20called%20%22mastercred%22.%26nbsp%3B%20However%2C%20I%20am%20calling%20it%20the%20Refresh%20Credential%20because%20when%20setting%20this%20value%20for%20the%20elastic%20job%20in%20PowerShell%2C%20the%20argument%20to%20set%20the%20credential%20on%20the%20job%20is%20called%20RefreshCredentialName.%26nbsp%3B%26nbsp%3B%26nbsp%3B%20For%20all%20target%20servers%20in%20the%20groups%20being%20targeted%20by%20the%20job%2C%20this%20credential%20should%20have%20view%20permissions%20%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Ein%20the%20master%20database%20(sufficient%20to%20look%20at%20the%20contents%20of%20the%20system%20view%20%3CEM%3Esys.databases%3C%2FEM%3E)%2C%20as%20it%20is%20used%20to%20enumerate%20all%20databases%20in%20the%20server.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%E2%80%AF%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1077728409%22%20id%3D%22toc-hId-1077728409%22%20id%3D%22toc-hId-1077728409%22%20id%3D%22toc-hId-1077728409%22%20id%3D%22toc-hId-1077728409%22%20id%3D%22toc-hId-1077728409%22%20id%3D%22toc-hId-1077728409%22%20id%3D%22toc-hId-1077728409%22%3E%3CSPAN%3E%3CEM%3EJob%20Credential%3C%2FEM%3E%3C%2FSPAN%3E%3CSPAN%3E%3CEM%3E%26nbsp%3B%3C%2FEM%3E%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EEach%20job%20step%20operates%20within%20a%20database%20using%20its%20own%20credential%2C%20provided%20when%20you%20define%20the%20job%20step.%26nbsp%3B%20A%20single%20job%2C%20comprised%20of%20different%20steps%2C%20can%20use%20a%20different%20credential%20for%20each%20step.%26nbsp%3B%20It%20is%20important%2C%20though%2C%20to%20make%20sure%20that%20the%20credential%20for%20the%20step%20satisfies%20both%20of%20the%20following%3A%20%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSPAN%3EThe%20credential%20has%20appropriate%20permissions%20for%20the%20operation%20being%20performed%2C%20and%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3EThe%20credential%20e%3CSPAN%3Exists%20on%20all%20target%20databases%20in%20the%20group%20with%20those%20permissions.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH2%20id%3D%22toc-hId--729726054%22%20id%3D%22toc-hId--729726054%22%20id%3D%22toc-hId--729726054%22%20id%3D%22toc-hId--729726054%22%20id%3D%22toc-hId--729726054%22%20id%3D%22toc-hId--729726054%22%20id%3D%22toc-hId--729726054%22%20id%3D%22toc-hId--729726054%22%3E%3CSPAN%3ECreating%20Credentials%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EEach%20of%20these%20credential%20types%20needs%20to%20be%20created%20on%20both%20the%20target%20(against%20which%20the%20jobs%20will%20run)%20and%20on%20the%20database%20hosting%20the%20Elastic%20Jobs%20Agent.%20I%20discuss%20each%20separately%20here.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--1836115939%22%20id%3D%22toc-hId--1836115939%22%20id%3D%22toc-hId--1836115939%22%20id%3D%22toc-hId--1836115939%22%20id%3D%22toc-hId--1836115939%22%20id%3D%22toc-hId--1836115939%22%20id%3D%22toc-hId--1836115939%22%20id%3D%22toc-hId--1836115939%22%3EOn%20the%20targets%20%E2%80%93%20%3CEM%3ERefresh%20Credential%3C%2FEM%3E%3A%3C%2FH4%3E%0A%3CP%3E%3CSPAN%3EEach%20of%20the%20two%20types%20of%20credentials%20can%20be%20created%20on%20the%20targets%20via%20T-SQL%3B%20this%20is%20not%20an%20action%20specific%20to%20Elastic%20Jobs%2C%20and%20so%20there%20is%20no%20special%20PowerShell%20support.%20%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%20You%20must%20create%20the%20refresh%20credential%20in%20the%20target%E2%80%99s%20%3CSTRONG%3Emaster%3C%2FSTRONG%3E%20db.%20Connect%20to%20master%20and%20issue%20the%20following%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ECREATE%20LOGIN%20refreshcredential%20WITH%20PASSWORD%3D%E2%80%99strongpassword%E2%80%99%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ECREATE%20USER%20refreshcredential%20FROM%20LOGIN%20refreshcredential%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-651396894%22%20id%3D%22toc-hId-651396894%22%20id%3D%22toc-hId-651396894%22%20id%3D%22toc-hId-651396894%22%20id%3D%22toc-hId-651396894%22%20id%3D%22toc-hId-651396894%22%20id%3D%22toc-hId-651396894%22%20id%3D%22toc-hId-651396894%22%3E%3CSPAN%3EOn%20the%20targets%20%E2%80%93%20%3CEM%3EJob%20Credential%3C%2FEM%3E%3A%3C%2FSPAN%3E%3C%2FH4%3E%0A%3CP%3E%3CSPAN%3EThe%20best%20way%20is%20to%20connect%20to%20master%20and%20create%20the%20login%2C%20and%20then%20to%20create%20the%20user%20on%20each%20individual%20DB%20you%20wish%20to%20include%20as%20a%20target%20of%20the%20elastic%20job.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESo%2C%20in%20%3CSTRONG%3Emaster%3C%2FSTRONG%3E%2C%20you%20would%20issue%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ECREATE%20LOGIN%20jobcredential%20WITH%20PASSWORD%3D%E2%80%99alsostrongpassword%E2%80%99%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThen%2C%20on%20%3CSTRONG%3EEACH%20of%20the%3C%2FSTRONG%3E%20%3CSTRONG%3Etarget%20databases%3C%2FSTRONG%3E%2C%20you%20need%20to%20connect%20and%20issue%20the%20following%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ECREATE%20USER%20jobcredential%20FROM%20LOGIN%20jobcredential%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH4%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2012px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20id%3D%22toc-hId--1156057569%22%20id%3D%22toc-hId--1156057569%22%20id%3D%22toc-hId--1156057569%22%20id%3D%22toc-hId--1156057569%22%20id%3D%22toc-hId--1156057569%22%20id%3D%22toc-hId--1156057569%22%20id%3D%22toc-hId--1156057569%22%20id%3D%22toc-hId--1156057569%22%3EOn%20the%20Elastic%20Job%20Agent%20host%20database%20%E2%80%93%20%3CEM%3Eboth%20types%20of%20credentials%3C%2FEM%3E%3A%3C%2FH4%3E%0A%3CP%3EHaving%20credentials%20on%20the%20target%20is%20the%20first%20step%2C%20next%20you%20need%20to%20add%20these%20%3CEM%3Esame%20credentials%3C%2FEM%3E%20to%20the%20database%20in%20which%20you%20have%20created%20the%20Elastic%20Job%20Agent.%20For%20this%20step%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%2C%20you%20issue%20commands%20ONLY%20in%20the%20database%20where%20the%20Elastic%20Job%20Agent%20is%20hosted%20(we%20will%20call%20this%20the%20host%20or%20job%20database).%20These%20connections%20are%20to%20the%20specific%20database%2C%20and%20not%20master%20or%20the%20server%20generally.%20As%20a%20best%20practice%2C%20the%20Elastic%20Job%20Agent%20and%20the%20target%20databases%20would%20be%20located%20on%20different%20servers.%20Separating%20the%20target%20from%20the%20host%20of%20the%20jobs%20being%20targeted%20is%20a%20good%20practice%20for%20maintenance%20and%20billing.%20It%20also%20prevents%20the%20jobs%20from%20inadvertently%20targeting%20the%20Elastic%20Jobs%20Agent%20host%20database%20itself.%3C%2FP%3E%0A%3CP%3EThere%20are%20two%20ways%20to%20create%20the%20credentials%20on%20the%20host%20database.%20This%20can%20be%20done%20via%20T-SQL%2C%20but%20the%20%3CEM%3Epreferred%3C%2FEM%3E%20way%20to%20do%20this%20is%20to%20create%20the%20credentials%20using%20PowerShell%20in%20the%20Azure%20Command%20Shell%20because%20those%20credentials%20are%20seen%20as%20%E2%80%9Cfirst%20class%20citizens%E2%80%9D%20from%20the%20perspective%20of%20the%20Elastic%20Job%20Agent%2C%20as%20the%20agent%20is%20the%20principal%20associated%20with%20the%20credential%20and%20has%20full%20permissions%20to%20view%20and%20modify%2Fdelete%20it.%26nbsp%3B%20If%20the%20credentials%20are%20created%20in%20T-SQL%2C%20you%20will%20still%20be%20able%20to%20use%20them%2C%20but%20you%20won%E2%80%99t%20be%20able%20to%20delete%20or%20modify%20them%20through%20the%20agent.%20Another%20way%20of%20looking%20at%20this%20is%20that%20the%20T-SQL%20credentials%20are%20more%20general%20and%20not%20created%20in%20a%20scope%20limited%20to%20the%20Elastic%20Jobs%20Agent%2C%20while%20the%20credentials%20created%20via%20the%20Elastic%20Job%20Agent%20via%20PowerShell%20are%20associated%20with%20the%20appropriate%20principal%20and%20database%20role.%20T-SQL%20created%20credentials%20are%20associated%20with%20the%20principal%20from%20which%20you%20create%20the%20user%20in%20T-SQL%20(so%2C%20if%20you%20connected%20to%20the%20database%20and%20created%20the%20credentials%20via%20your%20admin%20account%2C%20they%20would%20be%20associated%20with%20the%20dbo%20principal).%20This%20scope%20is%20larger%20than%20necessary%20for%20the%20task%20of%20connecting%20to%20the%20target%20database%20to%20execute%20jobs.%20Thus%2C%20for%20security%20and%20maintenance%20purposes%2C%20creating%20the%20credentials%20using%20PowerShell%20is%20preferred.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-1331455264%22%20id%3D%22toc-hId-1331455264%22%20id%3D%22toc-hId-1331455264%22%20id%3D%22toc-hId-1331455264%22%20id%3D%22toc-hId-1331455264%22%20id%3D%22toc-hId-1331455264%22%20id%3D%22toc-hId-1331455264%22%20id%3D%22toc-hId-1331455264%22%3EUsing%20T-SQL%20(less%20preferred)%3A%3C%2FH4%3E%0A%3CP%3EConnect%20to%20the%20job%20host%20database%2C%20issue%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECREATE%20MASTER%20KEY%20ENCRYPTION%20BY%20PASSWORD%20%3D%20'password'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECREATE%20DATABASE%20SCOPED%20CREDENTIAL%20refreshcredential%3C%2FP%3E%0A%3CP%3EWITH%20IDENTITY%20%3D%20'refreshcredential'%2C%3C%2FP%3E%0A%3CP%3ESECRET%20%3D%20'password1'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECREATE%20DATABASE%20SCOPED%20CREDENTIAL%20jobcredential%3C%2FP%3E%0A%3CP%3EWITH%20IDENTITY%20%3D%20'jobcredential'%2C%3C%2FP%3E%0A%3CP%3ESECRET%20%3D%20'password2'%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%E2%80%AF%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH4%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2012px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20id%3D%22toc-hId--475999199%22%20id%3D%22toc-hId--475999199%22%20id%3D%22toc-hId--475999199%22%20id%3D%22toc-hId--475999199%22%20id%3D%22toc-hId--475999199%22%20id%3D%22toc-hId--475999199%22%20id%3D%22toc-hId--475999199%22%20id%3D%22toc-hId--475999199%22%3EUsing%20Azure%20Cloud%20Shell%20(preferred%20method)%3A%3C%2FH4%3E%0A%3CP%3EOpen%20the%20Azure%20Cloud%20Shell%20by%20clicking%20on%20this%20icon%20on%20the%20top%20right%2Fmiddle%20of%20the%20screen%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-180px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AzureCloudShellIcon.png%22%20style%3D%22width%3A%20287px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F171519iF7AC388E5886F6EE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22AzureCloudShellIcon.png%22%20alt%3D%22AzureCloudShellIcon.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAt%20this%20point%2C%20you%20will%20have%20to%20already%20have%20created%20the%20Elastic%20Jobs%20Agent.%20I%20am%20not%20going%20to%20explain%20that%20process%20in%20this%20post%20(it%20will%20come%20in%20more%20detail%20when%20I%20do%20a%20full%20walkthrough).%26nbsp%3B%20For%20now%2C%20assume%20we%20have%20created%20it%20already%2C%20and%20we%20can%20do%20the%20following%20to%20bind%20the%20agent%20so%20that%20we%20can%20use%20it%20for%20creating%20the%20credentials%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24db%20%3D%20Get-azsqldatabase%20-ServerName%20ElasticJobsAgenthostServer%20-DatabaseName%20ElasticJobsAgentHostDB%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24server%20%3D%20%24db%20%7C%20Get-AzSqlServer%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24jobAgent%20%3D%20%24server%20%7C%20Get-AzSqlElasticJobAgent%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20create%20the%20credentials%2C%20execute%20the%20following%20commands%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24loginPasswordSecure1%20%3D%20(ConvertTo-SecureString%20-String%20%22strongPWD1%22%20-AsPlainText%20-Force)%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24loginPasswordSecure2%20%3D%20(ConvertTo-SecureString%20-String%20%22strongPWD2%22%20-AsPlainText%20-Force)%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24refreshCred%20%3D%20New-Object%20-TypeName%20%22System.Management.Automation.PSCredential%22%20-ArgumentList%20%22refreshcredential%22%2C%20%24loginPasswordSecure1%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24refreshCred%20%3D%20%24jobAgent%20%7C%20New-AzSqlElasticJobCredential%20-Name%20%22refreshcredential%20%22%20-Credential%20%24refreshCred%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24jobCred%20%3D%20New-Object%20-TypeName%20%22System.Management.Automation.PSCredential%22%20-ArgumentList%20%22jobcredential%22%2C%20%24loginPasswordSecure2%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24jobCred%20%3D%20%24jobAgent%20%7C%20New-AzSqlElasticJobCredential%20-Name%20%22jobcredential%22%20-Credential%20%24jobCred%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20check%20on%20the%20status%20of%20these%20new%20credentials%20in%20two%20ways.%26nbsp%3B%20One%20is%20by%20querying%20them%20in%20the%20host%20database%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESELECT%20*%20FROM%20sys.database_scoped_credentials%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ethe%20other%20is%20via%20Azure%20Cloud%20Shell%20by%20issuing%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3A%5C%26gt%3B%20%24jobAgent%20%7C%20Get-AzSqlElasticJobCredential%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAt%20this%20point%2C%20you%20should%20understand%20the%20why%20and%20how%20of%20creating%20appropriate%20credentials%20for%20Elastic%20Jobs%2C%20and%20feel%20prepared%20to%20create%20and%20test%20the%20idempotence%20of%20any%20scripts%20you%20would%20want%20to%20run%20via%20Elastic%20Jobs.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20blog%20is%20part%20of%20a%20series%20about%20Elastic%20Jobs%20on%20Azure%20SQL%20Database.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Felastic-jobs-in-azure-sql-database-what-and-why%2Fba-p%2F1177902%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EElastic%20Jobs%20in%20Azure%20SQL%20Database%20%E2%80%93%20What%20and%20Why%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Ffundamental-concepts-for-elastic-jobs-in-azure-sql-database%2Fba-p%2F1177939%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EFundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Fcreating-an-elastic-jobs-agent-credentials-and-jobs-for-azure%2Fba-p%2F1179929%22%20target%3D%22_self%22%3ECreating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20in%20PowerShell%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Fcreating-an-elastic-jobs-agent-credentials-and-jobs-for-azure%2Fba-p%2F1180096%22%20target%3D%22_self%22%3ECreating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20in%20T-SQL%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3ERunning%2C%20Scheduling%20and%20Monitoring%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLI%3E%0A%3CLI%3ETroubleshooting%20Common%20issues%20with%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLI%3E%0A%3C%2FOL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1177939%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EIn%20my%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Felastic-jobs-in-azure-sql-database-what-and-why%2Fba-p%2F1177902%22%20target%3D%22_blank%22%3Eprevious%20post%3C%2FA%3E%2C%20I%20explained%20what%20Elastic%20Jobs%20are%20and%20why%20you%20might%20consider%20using%20them.%20In%20this%20post%2C%20I%E2%80%99ll%20be%20discussing%20two%20fundamental%20concepts%20that%20you%20should%20understand%20before%20creating%20elastics%20jobs%3A%20Idempotence%2C%20and%20Credentials.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1204989%22%20slang%3D%22en-US%22%3ERe%3A%20Fundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1204989%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F569025%22%20target%3D%22_blank%22%3E%40platar1362%3C%2FA%3E%20%2C%20I%20think%20that%20should%20still%20be%20the%20case.%26nbsp%3B%20When%20you%20use%20PowerShell%2C%20the%20agent%20is%20used%20in%20the%20process%20of%20creating%20the%20credentials%2C%20so%20you%20will%20likely%20see%20the%20same%20underlying%20link%2C%20regardless%20of%20how%20the%20PowerShell%20command%20is%20issued.%26nbsp%3B%20If%20you%20query%20the%20credentials%20(using%20the%26nbsp%3B%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20font-family%3A%20'SegoeUI'%2C'Lato'%2C'Helvetica%20Neue'%2CHelvetica%2CArial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EGet-AzSqlElasticJobCredential%3C%2FSPAN%3E%20command)%2C%20you%20will%20see%20a%20two-column%20output.%26nbsp%3B%20If%20only%20one%20column%20is%20filled%20out%2C%20that's%20the%20%22T-SQL%22%20style%20credential.%26nbsp%3B%20If%20both%20columns%20are%20populated%2C%20then%20you%20have%20the%20proper%20linkage.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1201718%22%20slang%3D%22en-US%22%3ERe%3A%20Fundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1201718%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Kate%2C%3C%2FP%3E%3CP%3EQuestion%20about%20this%20statement%3A%3C%2FP%3E%3CP%3E%22%3CEM%3EThis%20can%20be%20done%20via%20T-SQL%2C%20but%20the%26nbsp%3Bpreferred%3C%2FEM%3E%3CSPAN%3E%3CEM%3E%26nbsp%3Bway%20to%20do%20this%20is%20to%20create%20the%20credentials%20using%20PowerShell%20in%20the%20Azure%20Command%20Shell%20because%20those%20credentials%20are%20seen%20as%20%E2%80%9Cfirst%20class%20citizens%E2%80%9D%20from%20the%20perspective%20of%20the%20Elastic%20Job%20Agent%2C%20as%20the%20agent%20is%20the%20principal%20associated%20with%20the%20credential%20and%20has%20full%20permissions%20to%20view%20and%20modify%2Fdelete%20it.%3C%2FEM%3E%22%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EWill%20the%20credentials%20still%20be%20seen%20as%20%22first%20class%20citizens%22%20if%20the%20PowerShell%20commands%20are%20executed%20in%20a%20Jupyter%20notebook%20in%20Azure%20Data%20Studio%2C%20connected%20to%20my%20Azure%20SQL%20instance%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks!%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft
Elastic Jobs are currently in public preview.  The feature is ready and we are preparing to make it officially generally available.  No extra steps are required to get started.

 

In my previous post, I explained what Elastic Jobs are and why you might consider using them. In this post, I’ll be discussing two fundamental concepts that you should understand before creating elastic jobs: Idempotence, and Credentials.

 

Idempotence:

It is very important that the T-SQL scripts being executed by Elastic Jobs be idempotent.  This means that if they are run multiple times (by accident or intentionally) they won't fail and won’t produce unintended results. If an elastic job has some side effects, and gets run more than once, it could fail or cause other unintended consequences (like consuming double the resources needed for a large statistics update).  One way to ensure idempotence is to make sure that you check if something already exists before trying to create it.

 

IF NOT EXISTS
(<SELECT OBJECT HERE YOU MIGHT CREATE>)
CREATE
<OBJECT YOU INTEND TO CREATE>

 

A good way to test if your job is idempotent is to run your script multiple times on a single target and see if the operation fails on subsequent executions, or to make sure that, if a single run consumes many resources, repeated runs do not also consume those resources.

 

Credentials:

Elastic jobs are processes hosted on the Elastic Jobs Agent Host, and yet they are running against databases and servers in the defined target groups (we call these the targets).  In order to do this, the Elastic Jobs agent has to have some way to authenticate to the target so that it can execute commands.  Any credentials you would need to execute on the target must also be created in the Elastic Jobs Agent Host Database.  We create credentials on the host because that is the securest way to store them for use later. 

There are two categories of credentials that are needed to create and run Elastic Jobs.  The Refresh Credential and the Job Credential.  In practice, a single elastic job will have one refresh credential, and each job step must have one job credential.  The refresh credential and the job credential do not have to be the same credential.  Further, jobs with more than one step can use a different job credential for each step. 

 

Refresh Credential 

The elastic job agent needs one credential that provides enough access to all servers within the target group to enumerate the databases there.  In some examples you may see, this credential is called "mastercred".  However, I am calling it the Refresh Credential because when setting this value for the elastic job in PowerShell, the argument to set the credential on the job is called RefreshCredentialName.    For all target servers in the groups being targeted by the job, this credential should have view permissions  in the master database (sufficient to look at the contents of the system view sys.databases), as it is used to enumerate all databases in the server. 

Note: If your target group is defined as just databases (and no servers), you do not need the refresh credential.  The refresh credential is used only to enumerate databases on servers in the target group.  

 

Job Credential 

Each job step operates within a database using its own credential, provided when you define the job step.  A single job, comprised of different steps, can use a different credential for each step.  It is important, though, to make sure that the credential for the step satisfies both of the following:  

  1. The credential has appropriate permissions for the operation being performed, and  
  2. The credential exists on all target databases in the group with those permissions. 

Creating Credentials

Each of these credential types needs to be created on both the target (against which the jobs will run) and on the database hosting the Elastic Jobs Agent. I discuss each separately here.

 

On the targets – Refresh Credential:

Each of the two types of credentials can be created on the targets via T-SQL; this is not an action specific to Elastic Jobs, and so there is no special PowerShell support.   You must create the refresh credential in the target’s master db. Connect to master and issue the following:

 

CREATE LOGIN refreshcredential WITH PASSWORD=’strongpassword’
CREATE USER refreshcredential FROM LOGIN refreshcredential

 

On the targets – Job Credential:

The best way is to connect to master and create the login, and then to create the user on each individual DB you wish to include as a target of the elastic job.

 

So, in master, you would issue:

 

CREATE LOGIN jobcredential WITH PASSWORD=’alsostrongpassword’

 

Then, on EACH of the target databases, you need to connect and issue the following:

 

CREATE USER jobcredential FROM LOGIN jobcredential

 

On the Elastic Job Agent host database – both types of credentials:

Having credentials on the target is the first step, next you need to add these same credentials to the database in which you have created the Elastic Job Agent. For this step , you issue commands ONLY in the database where the Elastic Job Agent is hosted (we will call this the host or job database). These connections are to the specific database, and not master or the server generally. As a best practice, the Elastic Job Agent and the target databases would be located on different servers. Separating the target from the host of the jobs being targeted is a good practice for maintenance and billing. It also prevents the jobs from inadvertently targeting the Elastic Jobs Agent host database itself.

There are two ways to create the credentials on the host database. This can be done via T-SQL, but the preferred way to do this is to create the credentials using PowerShell in the Azure Command Shell because those credentials are seen as “first class citizens” from the perspective of the Elastic Job Agent, as the agent is the principal associated with the credential and has full permissions to view and modify/delete it.  If the credentials are created in T-SQL, you will still be able to use them, but you won’t be able to delete or modify them through the agent. Another way of looking at this is that the T-SQL credentials are more general and not created in a scope limited to the Elastic Jobs Agent, while the credentials created via the Elastic Job Agent via PowerShell are associated with the appropriate principal and database role. T-SQL created credentials are associated with the principal from which you create the user in T-SQL (so, if you connected to the database and created the credentials via your admin account, they would be associated with the dbo principal). This scope is larger than necessary for the task of connecting to the target database to execute jobs. Thus, for security and maintenance purposes, creating the credentials using PowerShell is preferred.

 

Using T-SQL (less preferred):

Connect to the job host database, issue the following:

 

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'password'

CREATE DATABASE SCOPED CREDENTIAL refreshcredential
WITH IDENTITY = 'refreshcredential',
SECRET = 'password1'


CREATE DATABASE SCOPED CREDENTIAL jobcredential
WITH IDENTITY = 'jobcredential',
SECRET = 'password2'

Using Azure Cloud Shell (preferred method):

Open the Azure Cloud Shell by clicking on this icon on the top right/middle of the screen: 

 

AzureCloudShellIcon.png

 

At this point, you will have to already have created the Elastic Jobs Agent. I am not going to explain that process in this post (it will come in more detail when I do a full walkthrough).  For now, assume we have created it already, and we can do the following to bind the agent so that we can use it for creating the credentials:

 

:\> $db = Get-azsqldatabase -ServerName ElasticJobsAgenthostServer -DatabaseName ElasticJobsAgentHostDB 
:\> $server = $db | Get-AzSqlServer
:\> $jobAgent = $server | Get-AzSqlElasticJobAgent

 

To create the credentials, execute the following commands:

 

:\> $loginPasswordSecure1 = (ConvertTo-SecureString -String "strongPWD1" -AsPlainText -Force)
:\> $loginPasswordSecure2 = (ConvertTo-SecureString -String "strongPWD2" -AsPlainText -Force)
:\> $refreshCred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList "refreshcredential", $loginPasswordSecure1
:\> $refreshCred = $jobAgent | New-AzSqlElasticJobCredential -Name "refreshcredential " -Credential $refreshCred
:\> $jobCred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList "jobcredential", $loginPasswordSecure2
:\> $jobCred = $jobAgent | New-AzSqlElasticJobCredential -Name "jobcredential" -Credential $jobCred

 

You can check on the status of these new credentials in two ways.  One is by querying them in the host database:

 

SELECT * FROM sys.database_scoped_credentials

 

the other is via Azure Cloud Shell by issuing the following:

 

:\> $jobAgent | Get-AzSqlElasticJobCredential

 

At this point, you should understand the why and how of creating appropriate credentials for Elastic Jobs, and feel prepared to create and test the idempotence of any scripts you would want to run via Elastic Jobs.

 

This blog is part of a series about Elastic Jobs on Azure SQL Database.

Code samples are also available on GitHub

  1. Elastic Jobs in Azure SQL Database – What and Why
  2. Fundamental Concepts for Elastic Jobs in Azure SQL Database
  3. Creating an Elastic Jobs Agent, Credentials, and Jobs for Azure SQL Database in PowerShell
  4. Creating an Elastic Jobs Agent, Credentials, and Jobs for Azure SQL Database in T-SQL
  5. Running, Scheduling and Monitoring Elastic Jobs in Azure SQL Database
  6. Troubleshooting Common issues with Elastic Jobs in Azure SQL Database
5 Comments
Senior Member

Great information!

 

Is there an extra word in this sentence?

" Further jobs with more than one step can use a diffuse a different job credential for each step. "

@Kate_Smith - there is an error in one of your PowerShell steps:

 

\> $refreshCred = $jobAgent | New-AzSqlElasticJobCredential -Name "refreshcredential " -Credential $masterCred

 

You haven't defined $masterCred - it should be $refreshCred

 

Microsoft

 @platar@Martin Cairney - thanks for the feedback! Both issues have been addressed.

 

Frequent Visitor

Hi Kate,

Question about this statement:

"This can be done via T-SQL, but the preferred way to do this is to create the credentials using PowerShell in the Azure Command Shell because those credentials are seen as “first class citizens” from the perspective of the Elastic Job Agent, as the agent is the principal associated with the credential and has full permissions to view and modify/delete it."

 

Will the credentials still be seen as "first class citizens" if the PowerShell commands are executed in a Jupyter notebook in Azure Data Studio, connected to my Azure SQL instance?

 

Thanks!

Microsoft

Hi @platar1362 , I think that should still be the case.  When you use PowerShell, the agent is used in the process of creating the credentials, so you will likely see the same underlying link, regardless of how the PowerShell command is issued.  If you query the credentials (using the Get-AzSqlElasticJobCredential command), you will see a two-column output.  If only one column is filled out, that's the "T-SQL" style credential.  If both columns are populated, then you have the proper linkage.