Home
%3CLINGO-SUB%20id%3D%22lingo-sub-369091%22%20slang%3D%22en-US%22%3EHow%20to%20create%20a%20native%20PowerShell%20app%20registration%20to%20connect%20to%20Azure%20SQL%20DB%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-369091%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Oct%2030%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20How%20to%20create%20a%20native%20app%20registration%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20This%20guide%20assumes%20that%20you%20already%20own%20an%20Azure%20Subscription%2C%20an%20Azure%20Active%20Directory%20properly%20setup%20and%20your%20PowerShell%20environment%20properly%20set%20for%20running%20scripts.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20You%20might%20want%20to%20refer%20to%20this%20article%20first%3A%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fazuresqldbsupport%2F2018%2F10%2F26%2Fazure-sql-database-token-base-authentication-with-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fblogs.msdn.microsoft.com%2Fazuresqldbsupport%2F2018%2F10%2F26%2Fazure-sql-database-token-base-authentication-with-powershell%2F%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Problem%20summary%3A%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20There%20might%20be%20a%20time%20when%20we%20you%20will%20want%20to%20create%20a%20PowerShell%20script%20that%20can%20perform%20some%20management%20operations%20against%20your%20Azure%20SQL%20Database%2C%20therefore%20you%20would%20want%20to%20have%20a%20trustworthy%20app%20that%20could%20connect%20to%20your%20Azure%20SQL%20DB%20instance%20and%20so%20it%20for%20you.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EExplanation%20of%20this%20guide%3A%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20This%20guide%20will%20explain%20how%20to%20register%20your%20PowerShell%20script%20as%20a%20native%20application%20registration%20in%20AAD%2C%20so%20that%20you%20are%20able%20to%20run%20automated%20tasks%20from%20your%20PowerShell%20script.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EStep%201%3A%20Create%20your%20native%20PowerShell%20application%20registration%20in%20Azure%20Active%20Directory%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Go%20to%20your%20Azure%20portal%20and%20navigate%20all%20the%20way%20to%20your%20Azure%20Active%20Directory%20resource.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89044iAF0CE9D24598B41A%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Once%20you%20are%20there%2C%20you%20should%20proceed%20to%20%3CEM%3E%20App%20Registrations%20%3C%2FEM%3E%20under%20the%20%22Manage%22%20section.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89045i89DA3B4BFF7117B8%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%20proceed%20to%20click%20on%20%3CEM%3E%20%2BNew%20application%20registration%20%3C%2FEM%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89046iDCF4C8362FB347BE%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Once%20you%20are%20here%2C%20you%20will%20be%20prompted%20for%20some%20configurations%20and%20settings.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89047iE1C898847C7E6856%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Under%20this%20configuration%2C%20please%20make%20sure%20you%20give%20your%20application%20a%20name%2C%20an%20application%20type%20and%20a%20redirect%20uri.%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3EName%3A%20%3C%2FSTRONG%3E%20application%20name%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20Application%20Type%3A%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CEM%3EWebApp%3A%20%3C%2FEM%3E%20this%20is%20a%20web%20app%20application.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CEM%3ENative%3A%20%3C%2FEM%3E%20this%20is%20a%20native%20application%2C%20please%20choose%20this%20option.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3ERedirect%20URI%3A%20%3C%2FSTRONG%3E%20since%20this%20is%20a%20local%20application%2C%20please%20redirect%20it%20to%20your%20localhost.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20Once%20you%20have%20created%20the%20application%2C%20you%20should%20see%20a%20screen%20like%20this%20one%20below.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89048i6F774FBB9DAC9BB6%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20After%20you%20see%20this%20screen%2C%20there%20is%20some%20additional%20configuring%20we%20need%20to%20do.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Step%202%3A%20Configuring%20the%20Native%20app%20in%20AAD%20to%20connect%20to%20an%20Azure%20SQL%20DB%20endpoint.%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%20that%20we%20have%20our%20app%20registered%20and%20created%20in%20AAD%2C%20please%20proceed%20to%20click%20on%20the%20%22Settings%22%20button%2C%20as%20show%20in%20the%20image%20above.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20You%20should%20be%20able%20to%20see%20a%20blade%20with%20these%20options%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89049i7B4A28F6BFCBA4D4%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%2C%20we%20will%20need%20to%20configure%20the%20Redirect%20URIs%2C%20Owners%20and%20Required%20Permissions.%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3ERedirect%20URIs%3A%20%3C%2FSTRONG%3E%20this%20was%20previously%20configured%20when%20creating%20the%20app.%20Default%20URI%3A%20https%3A%2F%2Flocalhost%2F%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3EOwners%3A%20%3C%2FSTRONG%3E%20from%20your%20AAD%20domain%2C%20you%20could%20choose%20one%20or%20more%20people%20that%20own%20this%20application.%20For%20my%20case%2C%20I%20have%20identity%20as%20the%20owner.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3ERequired%20permissions%3A%20%3C%2FSTRONG%3E%20this%20requires%20a%20bit%20more%20explanation.%20What%20this%20Required%20permissions%20do%20is%20actually%20allow%20your%20application%20to%20connect%20to%20an%20Azure%20SQL%20Database%20endpoint%20or%20any%20other%20endpoint%20that%20you%20give%20it%20access%20to.%20This%20required%20permission%20pretty%20much%20lets%20you%20to%20connect%20to%20the%20resource%20URI%20you%20are%20requesting%20in%20your%20PowerShell%20application%2C%20which%20will%20be%20explained%20later.%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EClick%20on%20%2BAdd%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89050iB862CB42E7B6C28A%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EThis%20will%20bring%20up%20another%20blade%20with%20the%20corresponding%20API%20for%20the%20endpoint.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89051iCEF60D548669089C%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EThis%20might%20sound%20counter-intuitive%2C%20but%20you%20don't%20actually%20get%20a%20list%20of%20the%20available%20APIs%20we%20have%20in%20Azure%2C%20so%20you%20will%20need%20to%20type%20it%20in%20manually.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EIn%20the%20search%20bar%20when%20you%20click%20on%20%22Select%20an%20API%22%2C%20type%20in%3A%20Azure%20SQL%20Database%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89052iE8C7A1D115A056CD%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3ENow%20select%20it%20and%20go%20back%20to%20point%202%2C%20where%20it%20tells%20you%20to%20%22Select%20permissions%22.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EHit%20on%20the%20check%20box%20where%20it%20says%3A%20%22Delegated%20permissions%22%20%26gt%3B%20%22Access%20Azure%20SQL%20Database%20and%20Data%20Warehouse%22%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89053iA321E2DE95F076AD%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3ENow%20hit%20Select%20and%20you%20should%20be%20good%20to%20go%20for%20now.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89054i1D3C61E3EE433BB9%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CSTRONG%3E%20Step%203%3A%20Configuring%20your%20PowerShell%20script%20in%20your%20personal%20computer.%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Fire%20up%20your%20PowerShell%20ISE%20window%20or%20Visual%20Studio%20code%20and%20make%20sure%20you%20add%20the%20following%20lines%20of%20code%20to%20your%20script.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%2C%20we%20must%20make%20sure%20to%20configure%20the%20following%20variables%20in%20our%20script%3A%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3ETenant%20Name%3A%20%3C%2FSTRONG%3E%20this%20is%20found%20in%20the%20AAD%20Properties%20option%20under%20the%20Manage%20section.%20Please%20review%20the%20%22Directory%20ID%22%20field%20and%20copy%20that%20guid%20and%20place%20it%20in%20the%20script%20as%20the%20%24tenantName%20value.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3EClient%20ID%3A%20%3C%2FSTRONG%3E%20the%20client%20ID%20can%20be%20found%20in%20your%20app%20registration%20window.%20It's%20the%20guid%20called%20%22Application%20ID%22.%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89055iE1753FB9E509EE75%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3ERedirect%20URI%3A%20%3C%2FSTRONG%3E%20we%20previously%20added%20this%20in%20the%20Redirect%20URI%20in%20the%20Settings%20of%20the%20Native%20app%20registration.%20The%20value%20should%20be%3A%20%22https%3A%2F%2Flocalhost%2F%22%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3EResource%20URI%3A%20%3C%2FSTRONG%3E%20this%20URI%20should%20be%20pointing%20to%20the%20Azure%20SQL%20Database%20API.%20The%20value%20should%20be%3A%20%22%3CA%20href%3D%22https%3A%2F%2Fdatabase.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdatabase.windows.net%2F%3C%2FA%3E%22%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CB%3EAuthority%20URI%3A%20%3C%2FB%3E%20last%20but%20not%20least%2C%20the%20Authority%20URI.%20This%20is%20useful%20if%20you%20are%20connecting%20with%20AAD%20authentication.%20It's%20the%20STS%20link%20where%20the%20tenant%20will%20be%20authenticated%20against.%20The%20value%20should%20be%3A%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%3C%2FA%3E%22%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CSTRONG%3EScript%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22PowerShell%22%5D%20%3CBR%20%2F%3E%20%24tenantName%20%3D%20%22%7BAAD%20DIRECTORY%20ID%7D%22%20%23%20Tenant%20Name%20this%20is%20found%20in%20AAD%20%26gt%3B%20Properties%20%26gt%3B%20Directory%20ID%20%3CBR%20%2F%3E%20%24clientId%20%3D%20%22%7BAPPLICATION%20ID%7D%22%20%23%20Application%20ID%20is%20found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Application%20ID%20%3CBR%20%2F%3E%20%24redirectUri%20%3D%20%22%7BREDIRECT%20URI%7D%22%20%23%20Rediret%20URI%20is%20found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Settings%20%26gt%3B%20Redirect%20URIs%20%3CBR%20%2F%3E%20%24resourceUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fdatabase.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdatabase.windows.net%2F%3C%2FA%3E%22%20%23%20Resource%20URI%20%26gt%3B%20Found%20in%20AAD%20%26gt%3B%20App%20Registrations%20%26gt%3B%20Settings%20%26gt%3B%20Required%20Permissions%20%26gt%3B%20%3CBR%20%2F%3E%20%23%20Add%20%26gt%3B%20Search%20Bar%20*Type%20in*%20%26gt%3B%20Azure%20SQL%20DB%20%26gt%3B%20Add%20Rights%20%26gt%3B%20OK%20%26gt%3B%20OK%20%26gt%3B%20Grant%20permissions%20%3CBR%20%2F%3E%20%24authorityUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%24tenantName%3C%2FA%3E%22%20%23%20Authority%20URI%20%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%7BDirectoryId%7CTenantName%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%7BDirectoryId%7CTenantName%3C%2FA%3E%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20function%20GetAuthToken%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%2064%20bit%20make%20sure%20to%20change%20the%20version%20to%20the%20one%20available%20on%20your%20machine%20%3CBR%20%2F%3E%20%24adalPath%20%3D%20%22%24%7Benv%3AProgramFiles%7D%5CWindowsPowerShell%5CModules%5CAzureRM.profile%5C5.5.1%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%2032%20bit%20uncomment%20if%20this%20is%20your%20scenario%20%3CBR%20%2F%3E%20%23%20adalPath%20%3D%20%22%24%7Benv%3AProgramFiles(x86)%7D%5CWindowsPowerShell%5CModules%5CAzureRM.profile%5C5.3.4%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24adal%20%3D%20%22%24adalPath%5CMicrosoft.IdentityModel.Clients.ActiveDirectory.dll%22%20%3CBR%20%2F%3E%20%24adalforms%20%3D%20%22%24adalPath%5CMicrosoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadFrom(%24adal)%20%7C%20Out-Null%20%3CBR%20%2F%3E%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadFrom(%24adalforms)%20%7C%20Out-Null%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24authContext%20%3D%20New-Object%20%22Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext%22%20-ArgumentList%20%24authorityUri%20%3CBR%20%2F%3E%20%24authResult%20%3D%20%24authContext.AcquireToken(%24resourceUri%2C%20%24clientId%2C%20%24redirectUri%2C%20%22Always%22)%20%23%20Not%20sure%20when%20to%20use%20AcquireToken()%20or%20AcquireTokenAsync()%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%20%24authResult%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Call%20GetAuthToken%20to%20acquire%20token%20based%20on%20credentials%20entered%20in%20prompt%20%3CBR%20%2F%3E%20%24authResult%20%3D%20GetAuthToken%20%3CBR%20%2F%3E%20%24authResult.AccessToken%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Server%20name%2C%20database%20name%20and%20the%20connection%20string%20that%20will%20be%20used%20to%20open%20connection%20%3CBR%20%2F%3E%20%24sqlServerUrl%20%3D%20%22fcobo.database.windows.net%22%20%3CBR%20%2F%3E%20%24database%20%3D%20%22TESTDB%22%20%3CBR%20%2F%3E%20%24connectionString%20%3D%20%22Server%3Dtcp%3A%24sqlServerUrl%2C1433%3BInitial%20Catalog%3D%24database%3BPersist%20Security%20Info%3DFalse%3BMultipleActiveResultSets%3DFalse%3BEncrypt%3DTrue%3BTrustServerCertificate%3DFalse%3B%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Create%20the%20connection%20object%20%3CBR%20%2F%3E%20%24connection%20%3D%20New-Object%20System.Data.SqlClient.SqlConnection(%24connectionString)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Set%20AAD%20generated%20token%20to%20SQL%20connection%20token%20%3CBR%20%2F%3E%20%24connection.AccessToken%20%3D%20%24authResult.AccessToken%20%3CBR%20%2F%3E%20%24connection.AccessToken%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20prints%20the%20connection%20information%2C%20it's%20still%20closed%20at%20this%20point%2C%20will%20open%20later%20on.%20The%20token%20is%20already%20attached.%20%3CBR%20%2F%3E%20%24connection%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Query%20that%20will%20be%20sent%20when%20the%20connection%20is%20open.%20I%20had%20a%204%2C000%20record%20table%20and%20I%20was%20able%20to%20truncate%20with%20this%20script%20%3CBR%20%2F%3E%20%24query%20%3D%20%22TRUNCATE%20TABLE%20TEST1%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Opens%20connection%20to%20Azure%20SQL%20Database%20and%20executes%20a%20query%20%3CBR%20%2F%3E%20%24connection.Open()%20%3CBR%20%2F%3E%20%23%20After%20this%2C%20the%20token%20is%20no%20longer%20there%2C%20I%20believe%20this%20is%20because%20the%20authentication%20went%20through%20already%2C%20so%20it%20gets%20rid%20of%20it.%20%3CBR%20%2F%3E%20%24connection%20%3CBR%20%2F%3E%20%24command%20%3D%20New-Object%20-Type%20System.Data.SqlClient.SqlCommand(%24query%2C%20%24connection)%20%3CBR%20%2F%3E%20%24command.ExecuteNonQuery()%20%3CBR%20%2F%3E%20%24connection.Close()%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%20feel%20free%20to%20modify%20the%20script%20and%20adapt%20it%20to%20your%20particular%20needs!%20%3A)%3C%2Fimg%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Hope%20this%20guide%20helps%20you%20on%20your%20endeavors.%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-369091%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Oct%2030%2C%202018%20How%20to%20create%20a%20native%20app%20registrationThis%20guide%20assumes%20that%20you%20already%20own%20an%20Azure%20Subscription%2C%20an%20Azure%20Active%20Directory%20properly%20setup%20and%20your%20PowerShell%20environment%20properly%20set%20for%20running%20scripts.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-369091%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAAD%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Active%20Directory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eazure%20sql%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20SQL%20Database%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ecoding%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Enative%20app%20registration%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Oct 30, 2018
How to create a native app registration

This guide assumes that you already own an Azure Subscription, an Azure Active Directory properly setup and your PowerShell environment properly set for running scripts.

You might want to refer to this article first: https://blogs.msdn.microsoft.com/azuresqldbsupport/2018/10/26/azure-sql-database-token-base-auth...

Problem summary:

There might be a time when we you will want to create a PowerShell script that can perform some management operations against your Azure SQL Database, therefore you would want to have a trustworthy app that could connect to your Azure SQL DB instance and so it for you.

Explanation of this guide:

This guide will explain how to register your PowerShell script as a native application registration in AAD, so that you are able to run automated tasks from your PowerShell script.

Step 1: Create your native PowerShell application registration in Azure Active Directory

Go to your Azure portal and navigate all the way to your Azure Active Directory resource.



Once you are there, you should proceed to App Registrations under the "Manage" section.



Now proceed to click on +New application registration .



Once you are here, you will be prompted for some configurations and settings.



Under this configuration, please make sure you give your application a name, an application type and a redirect uri.

  • Name: application name

  • Application Type:

    • WebApp: this is a web app application.

    • Native: this is a native application, please choose this option.



  • Redirect URI: since this is a local application, please redirect it to your localhost.


Once you have created the application, you should see a screen like this one below.



After you see this screen, there is some additional configuring we need to do.

Step 2: Configuring the Native app in AAD to connect to an Azure SQL DB endpoint.

Now that we have our app registered and created in AAD, please proceed to click on the "Settings" button, as show in the image above.

You should be able to see a blade with these options:



Now, we will need to configure the Redirect URIs, Owners and Required Permissions.

  • Redirect URIs: this was previously configured when creating the app. Default URI: https://localhost/

  • Owners: from your AAD domain, you could choose one or more people that own this application. For my case, I have identity as the owner.

  • Required permissions: this requires a bit more explanation. What this Required permissions do is actually allow your application to connect to an Azure SQL Database endpoint or any other endpoint that you give it access to. This required permission pretty much lets you to connect to the resource URI you are requesting in your PowerShell application, which will be explained later.

    • Click on +Add

    • This will bring up another blade with the corresponding API for the endpoint.


    • This might sound counter-intuitive, but you don't actually get a list of the available APIs we have in Azure, so you will need to type it in manually.

    • In the search bar when you click on "Select an API", type in: Azure SQL Database


    • Now select it and go back to point 2, where it tells you to "Select permissions".

    • Hit on the check box where it says: "Delegated permissions" > "Access Azure SQL Database and Data Warehouse"


    • Now hit Select and you should be good to go for now.





Step 3: Configuring your PowerShell script in your personal computer.

Fire up your PowerShell ISE window or Visual Studio code and make sure you add the following lines of code to your script.

Now, we must make sure to configure the following variables in our script:

  • Tenant Name: this is found in the AAD Properties option under the Manage section. Please review the "Directory ID" field and copy that guid and place it in the script as the $tenantName value.

  • Client ID: the client ID can be found in your app registration window. It's the guid called "Application ID".




  • Redirect URI: we previously added this in the Redirect URI in the Settings of the Native app registration. The value should be: "https://localhost/"

  • Resource URI: this URI should be pointing to the Azure SQL Database API. The value should be: "https://database.windows.net/"

  • Authority URI: last but not least, the Authority URI. This is useful if you are connecting with AAD authentication. It's the STS link where the tenant will be authenticated against. The value should be: "https://login.microsoftonline.com/$tenantName"


Script

[code language="PowerShell"]
$tenantName = "{AAD DIRECTORY ID}" # Tenant Name this is found in AAD > Properties > Directory ID
$clientId = "{APPLICATION ID}" # Application ID is found in AAD > App Registrations > Application ID
$redirectUri = "{REDIRECT URI}" # Rediret URI is found in AAD > App Registrations > Settings > Redirect URIs
$resourceUri = "https://database.windows.net/" # Resource URI > Found in AAD > App Registrations > Settings > Required Permissions >
# Add > Search Bar *Type in* > Azure SQL DB > Add Rights > OK > OK > Grant permissions
$authorityUri = "https://login.microsoftonline.com/$tenantName" # Authority URI https://login.microsoftonline.com/{DirectoryId|TenantName}

function GetAuthToken {

# 64 bit make sure to change the version to the one available on your machine
$adalPath = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\5.5.1"

# 32 bit uncomment if this is your scenario
# adalPath = "${env:ProgramFiles(x86)}\WindowsPowerShell\Modules\AzureRM.profile\5.3.4"

$adal = "$adalPath\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "$adalPath\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"

[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authorityUri
$authResult = $authContext.AcquireToken($resourceUri, $clientId, $redirectUri, "Always") # Not sure when to use AcquireToken() or AcquireTokenAsync()

return $authResult
}

# Call GetAuthToken to acquire token based on credentials entered in prompt
$authResult = GetAuthToken
$authResult.AccessToken

# Server name, database name and the connection string that will be used to open connection
$sqlServerUrl = "fcobo.database.windows.net"
$database = "TESTDB"
$connectionString = "Server=tcp:$sqlServerUrl,1433;Initial Catalog=$database;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;"

# Create the connection object
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)

# Set AAD generated token to SQL connection token
$connection.AccessToken = $authResult.AccessToken
$connection.AccessToken

# prints the connection information, it's still closed at this point, will open later on. The token is already attached.
$connection

# Query that will be sent when the connection is open. I had a 4,000 record table and I was able to truncate with this script
$query = "TRUNCATE TABLE TEST1"

# Opens connection to Azure SQL Database and executes a query
$connection.Open()
# After this, the token is no longer there, I believe this is because the authentication went through already, so it gets rid of it.
$connection
$command = New-Object -Type System.Data.SqlClient.SqlCommand($query, $connection)
$command.ExecuteNonQuery()
$connection.Close()
[/code]

Now feel free to modify the script and adapt it to your particular needs! :)

Hope this guide helps you on your endeavors.