{"id":32013,"date":"2024-06-05T20:04:09","date_gmt":"2024-06-05T18:04:09","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=32013"},"modified":"2024-06-06T08:58:16","modified_gmt":"2024-06-06T06:58:16","slug":"make-sql-server-services-more-secure-with-managed-service-accounts","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/","title":{"rendered":"Make SQL Server services more secure with Managed Service Accounts"},"content":{"rendered":"\n<p>In the past years, I actively have been involved in securing MSSQL Instances (and other services).<br>This lead me to use the Managed Service Accounts (MSA) and the grouped Managed Service Accounts (gMSA)<br>The MSA have been introduced in Windows Server 2008 R2 and the gMSA in Windows Server 2012.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id=\"h-i-what-exactly-are-msa-or-gmsa\">I. <strong><u>What exactly are MSA or gMSA<\/u><\/strong><\/h2>\n\n\n\n<p>MSA (Managed Service Accounts) or gMSA (group Managed Service Accounts) are Active Directory Managed Accounts used to start services (Service Accounts).<\/p>\n\n\n\n<p>They have several advantages though they are still not permanently used (at least from what I saw at most of my customers).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>They are linked to a single server (MSA) or to a group of Server (gMSA) and cannot be used on other server than the ones they are dedicated to.<\/li>\n\n\n\n<li>They cannot be used to gain access to the server as they can&#8217;t be used to login (no privilege escalation is possible).<\/li>\n<\/ul>\n\n\n\n<p>They simplify the management of Service Principle Names (SPN) as the SPN for the related service is automatically generated when the service starts the first time.<br>This is obviously a huge advantage in terms of security as Kerberos Authentication is factually running on the go.<\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-ii-implementation-configuration-of-msa-gmsa\">II. Implementation \/ Configuration of MSA\/gMSA<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>II.1 Prerequisites:<\/strong><\/p>\n\n\n\n<p>In order to create a MSA or a gMSA you will need to go the Powershell way.<\/p>\n\n\n\n<p>Normally the setup will be performed on a Server which is not an AD Server.<\/p>\n\n\n\n<p>Therefore the first step will be to install the RSAT Tools:<\/p>\n\n\n\n<p>On Windows Server:<\/p>\n\n\n\n<p>Install-WindowsFeature RSAT-AD-PowerShell<\/p>\n\n\n\n<p>On Windows 10\/11: Add-WindowsCapability -Name Rsat.ActiveDirectory.DS-LDS.Tools -Online<\/p>\n\n\n\n<p>The creation of both types can easily be performed with simple scripts though this requires AD Admin permissions (or at least a delegation for creating Accounts)<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>II.2 Create a MSA:<\/strong><\/p>\n\n\n\n<p><strong>New-ADServiceAccount -Name NAME -Enabled $true -Description &#8220;Managed Service Account for xxxx&#8221; -DisplayName &#8220;MSA 1 \u2013 xxxx&#8221; -RestrictToSingleComputer<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"72\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png\" alt=\"\" class=\"wp-image-33397\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png 940w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5-300x23.png 300w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5-768x59.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p>Once the account got created, it will be added in Active Directory under Managed Service Accounts:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"305\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-6.png\" alt=\"\" class=\"wp-image-33398\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-6.png 940w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-6-300x97.png 300w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-6-768x249.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>II.3 Create a gMSA:<\/strong><\/p>\n\n\n\n<p>gMSA creation is slightful different as there\u2019s a need to create the Account and grant the defined computer Objects to use it. Fortunately, this can als easily be done with a single PS Query:<\/p>\n\n\n\n<p><strong>New-ADServiceAccount -Name msa_exhib2 -DNSHostName DNS Server -PrincipalsAllowedToRetrieveManagedPassword Server1$, Server2$<\/strong><\/p>\n\n\n\n<p>Here it is important to add the $ sign at the end of the device name as the query gives the permissions to the Computer Object.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"56\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-7.png\" alt=\"\" class=\"wp-image-33399\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-7.png 940w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-7-300x18.png 300w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-7-768x46.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"90\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-8.png\" alt=\"\" class=\"wp-image-33400\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-8.png 940w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-8-300x29.png 300w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-8-768x74.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-iii-installation-and-usage-of-the-msa-gmsa\">III. Installation and Usage of the MSA \/ gMSA:<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>III.1 Installation of the created Account<\/strong><\/p>\n\n\n\n<p>Once the Account got created it will need to be installed on the target server (Requires the RSAT AD Tools to be installed as mentioned previously):<\/p>\n\n\n\n<p>Install-ADServiceAccount -Identity ServiceName<\/p>\n\n\n\n<p>Be Aware that the MSA is restricted only to one server. Therefore, it can only be installed once. Trying to install it on another server will end with the error:<\/p>\n\n\n\n<p>Once the Account got created it will need to be installed on the target server (Requires the RSAT AD Tools to be installed as mentioned previously):<\/p>\n\n\n\n<p><strong>Install-ADServiceAccount -Identity ServiceName<\/strong><\/p>\n\n\n\n<p>Be Aware that the MSA is restricted only to one server. Therefore, it can only be installed once. Trying to install it on another server will end with the error:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"191\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-9.png\" alt=\"\" class=\"wp-image-33401\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-9.png 940w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-9-300x61.png 300w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-9-768x156.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><mark class=\"has-inline-color has-luminous-vivid-orange-color\">If the action gets confirmed, the MSA will be removed from it\u2019s original server and the services relying on it will be stopped.<\/mark><\/p>\n\n\n\n<p>The issue will not exist with gMSA as it can be installed on all Server \/ Computer which have been granted the access to it.<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>III.2 Grant the permissions to the MSA \/ gMSA<\/strong><\/p>\n\n\n\n<p>The created accounts need to be granted segregated permissions. As my Colleague <a href=\"https:\/\/www.dbi-services.com\/blog\/author\/stephane-haby\/\">St\u00e9phane Haby<\/a> already mentioned in <a href=\"https:\/\/www.dbi-services.com\/blog\/sql-server-security-ensure-that-sql-server-service-accounts-are-not-a-member-of-the-windows-local-administrator-group\/\">this Article<\/a>, for security reasons a SQL Server Service Account should never be granted the Local Administrator Permissions.<\/p>\n\n\n\n<p>This can be performed on several ways:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<ul class=\"wp-block-list\">\n<li>GPO: This requires the Domain Architect to define the GPOs properly<\/li>\n\n\n\n<li>By managing the Local Security Policies:<\/li>\n<\/ul>\n\n\n\n<p>Under Local Policies\\User Rights Assignment add the created account to all relevant permissions (Basically, for a SQL Server: Logon as a Batch Job, Logon as a service, Perform Volume Maintenance Tasks (eventually Lock Pages in Memory if this is required by the software using MSSQL)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By using a powershell script:<\/li>\n<\/ul>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code># Variables used - change as required\n$TempLocation = \"C:\\Service\"\n$SQLServiceAccount = $ServiceName #Account used for the SQL Service\n\n## This lines are required to change in the cfg file\n$ChangeFrom = \"SeManageVolumePrivilege = \"\n$ChangeFrom2 = \"SeLockMemoryPrivilege = \"\n$ChangeFrom3 = \"SeBatchLogonRight = \"\n$ChangeFrom4 = \"SeServiceLogonRight = \"\n$ChangeFrom5 = \"SeAuditPrivilege = \"\n\n## Build the new lines \n$ChangeTo = \"SeManageVolumePrivilege = $SQLServiceAccount,\"\n$ChangeTo2 = \"SeLockMemoryPrivilege = $SQLServiceAccount,\"\n$ChangeTo3 = \"SeBatchLogonRight = $SQLServiceAccount,\"\n$ChangeTo4 = \"SeServiceLogonRight = $SQLServiceAccount,\"\n$ChangeTo5 = \"SeAuditPrivilege = $SQLServiceAccount,\"\n\n\n## Set a name for the Security Policy cfg file.\n$fileName = \"$TempLocation\\SecPolExport.cfg\"\n\n## export currect Security Policy config\nWrite-Host \"Exporting Security Policy to file: $filename\"\nsecedit \/export \/cfg $filename\nCopy-Item $filename -Destination \"$filename.save_before\"\nWrite-Host \"make a copy of initial Security policy export: $filename.save_before\"\nWrite-Host \"start to modify Security Policy Export file\"\n\n## delete the last 4 lines in the export file; this is needed if some attrubutes are not yet set and they are added at the end of the file; after this end-section (last 4 lines) the secpol is not importing it, so delete these values and add them at the end of the script\n$content = get-content $filename\n$content&#091;0..($content.length-4)] | out-file $filename\n\n\n# Use Get-Content to change the text in the cfg file and then save it\n\n# As the line for the option only exists if there is something already in the group\n# this will check for it and add your $SQLServiceAccount or use Add-Contect to append option and your $SQLServiceAccount\n\n#Option SeManageVolumePrivilege (Perform maintenance volumne tasks)\nIF ((Get-Content $fileName) | where { $_.Contains(\"SeManageVolumePrivilege\") })\n{\nWrite-Host \"Appending line containing SeManageVolumePrivilege with $SQLServiceAccount\"\n(Get-Content $fileName) -replace $ChangeFrom, $ChangeTo | Set-Content $fileName\n}\nelse\n{\nWrite-Host \"Adding new line containing SeManageVolumePrivilege\"\nAdd-Content $filename \"`nSeManageVolumePrivilege = $SQLServiceAccount\"\n}\n\n## Option SeLockMemoryPrivilege (Lock Pages in Memory)\n## This is optinal depending on the requirements\n## IF ((Get-Content $fileName) | where { $_.Contains(\"SeLockMemoryPrivilege\") })\n## {\n## Write-Host \"Appending line containing SeLockMemoryPrivilege with $SQLServiceAccount\"\n## (Get-Content $fileName) -replace $ChangeFrom2, $ChangeTo2 | Set-Content $fileName\n## }\n## else\n## {\n## Write-Host \"Adding new line containing SeLockMemoryPrivilege\"\n## Add-Content $filename \"`nSeLockMemoryPrivilege = $SQLServiceAccount\"\n## }\n\n#Option SeBatchLogonRight (Log on as Batch job)\nIF ((Get-Content $fileName) | where { $_.Contains(\"SeBatchLogonRight\") })\n{\nWrite-Host \"Appending line containing SeBatchLogonRight with $SQLServiceAccount\"\n(Get-Content $fileName) -replace $ChangeFrom3, $ChangeTo3 | Set-Content $fileName\n}\nelse\n{\nWrite-Host \"Adding new line containing SeBatchLogonRight\"\nAdd-Content $filename \"`nSeBatchLogonRight = $SQLServiceAccount\"\n}\n\n\n#Option SeServiceLogonRight (log on as a service)\nIF ((Get-Content $fileName) | where { $_.Contains(\"SeServiceLogonRight\") })\n{\nWrite-Host \"Appending line containing SeServiceLogonRight with $SQLServiceAccount\"\n(Get-Content $fileName) -replace $ChangeFrom4, $ChangeTo4 | Set-Content $fileName\n}\nelse\n{\nWrite-Host \"Adding new line containing SeServiceLogonRight\"\nAdd-Content $filename \"`nSeServiceLogonRight = $SQLServiceAccount\"\n}<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>III.3 Configure the services to use the MSA \/ gMSA<\/strong><\/p>\n\n\n\n<p>Once the Accounts have been granted the required permissions, the services need to be configured to use them.<\/p>\n\n\n\n<p>This can be performed by using a command of the <a href=\"https:\/\/dbatools.io\/\">DBATools Module<\/a> :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Update-DbaServiceAccount -ServiceName 'MSSQLSERVER','SQLSERVERAGENT' -UserName 'Domain\\MSAName$'<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>IV. An easy way to configure everything in one shot<\/strong><\/p>\n\n\n\n<p>One of my teacher once told me that a good administrator is a lazy administrator. With these words he meant everything what can be scripted or automated should be.<\/p>\n\n\n\n<p>That&#8217;s why you will find a complete script to perform the above mentioned tasks below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>## Install DbaTools (Requires the Server to have an Internet Connection\n\nWrite-Host \"Now let's install DBATools\"\nInstall-Module DBATools -Scope AllUsers\nread-host \"If no error occured, press any key to continue\"\n## Create Service Folder\n\nWrite-Host \"Creation of the Service Folder\"\nif (Test-Path \"C:\\Service\") {\n    # Folder exists - Do something here\n    Write-host \"Folder Exists!\" -f Green\n}\nelse {\n    # Folder does not exist - Do something else here\n    New-Item -ItemType Directory -Path C:\\Service\n}\n\n## Install AD Powershell Tools\n\nWrite-Host \"First we install the AD Powershell Tools\"\nInstall-WindowsFeature RSAT-AD-PowerShell\nread-host \"RSAT Tools installed - press enter to continue\"\n\n## Define Variables\n\n$ServerName = hostname\n$ServiceName = 'sv'+ $ServerName ## Here you can define your own naming convention for the MSA\n#Create MSA for SQL Server\n\nWrite-Host \"Creation of the Managed Service Account for this Server\"\nif (!(Get-ADServiceAccount -Filter \"Name -like '$ServiceName'\")) {\nNew-ADServiceAccount -Name $ServiceName -Enabled $true -Description \"Managed Service Account for SQL Server $ServerName\" -DisplayName \"MSA - $ServerName\" -RestrictToSingleComputer\nWrite-Host \"Service Account has been created\"\n}\nelse {\nWrite-host \"Service Account already exists\"\n}\n\nread-host \"Service Account created - please press enter to continue\"\n\n#Install MSA on Local Server\n\nWrite-Host \"Installation of the MSA on this Server\"\nInstall-ADServiceAccount -Identity $ServiceName\nread-host \"Service Account installed - Press Enter to continue\"\n\n## Adding sql service account to local policy\nwrite-host \"Local Security Policies will be applied according to the default SQL settings\"\n#read-host \"this is only for debug purporsed - please press enter to proceed\"\n\n# Variables used - change as required\n$TempLocation = \"C:\\Service\"\n\n## This lines are required to change in the cfg file\n$ChangeFrom = \"SeManageVolumePrivilege = \"\n$ChangeFrom2 = \"SeLockMemoryPrivilege = \"\n$ChangeFrom3 = \"SeBatchLogonRight = \"\n$ChangeFrom4 = \"SeServiceLogonRight = \"\n$ChangeFrom5 = \"SeAuditPrivilege = \"\n\n## Build the new lines \n$ChangeTo = \"SeManageVolumePrivilege = $ServiceName,\"\n$ChangeTo2 = \"SeLockMemoryPrivilege = $ServiceName,\"\n$ChangeTo3 = \"SeBatchLogonRight = $ServiceName,\"\n$ChangeTo4 = \"SeServiceLogonRight = $ServiceName,\"\n$ChangeTo5 = \"SeAuditPrivilege = $ServiceName,\"\n\n\n## Set a name for the Security Policy cfg file.\n$fileName = \"$TempLocation\\SecPolExport.cfg\"\n\n## export currect Security Policy config\nWrite-Host \"Exporting Security Policy to file: $filename\"\nsecedit \/export \/cfg $filename\nCopy-Item $filename -Destination \"$filename.save_before\"\nWrite-Host \"make a copy of initial Security policy export: $filename.save_before\"\nWrite-Host \"start to modify Security Policy Export file\"\n\n## delete the last 4 lines in the export file; this is needed if some attrubutes are not yet set and they are added at the end of the file; after this end-section (last 4 lines) the secpol is not importing it, so delete these values and add them at the end of the script\n$content = get-content $filename\n$content&#091;0..($content.length-4)] | out-file $filename\n\n\n# Use Get-Content to change the text in the cfg file and then save it\n\n# As the line for the option only exists if there is something already in the group\n# this will check for it and add your $ServiceName or use Add-Contect to append option and your $ServiceName\n\n#Option SeManageVolumePrivilege (Perform maintenance volumne tasks)\nIF ((Get-Content $fileName) | where { $_.Contains(\"SeManageVolumePrivilege\") })\n{\nWrite-Host \"Appending line containing SeManageVolumePrivilege with $ServiceName\"\n(Get-Content $fileName) -replace $ChangeFrom, $ChangeTo | Set-Content $fileName\n}\nelse\n{\nWrite-Host \"Adding new line containing SeManageVolumePrivilege\"\nAdd-Content $filename \"`nSeManageVolumePrivilege = $ServiceName\"\n}\n\n## Option SeLockMemoryPrivilege (Lock Pages in Memory)\n## This is optinal depending on the requirements\n## IF ((Get-Content $fileName) | where { $_.Contains(\"SeLockMemoryPrivilege\") })\n## {\n## Write-Host \"Appending line containing SeLockMemoryPrivilege with $ServiceName\"\n## (Get-Content $fileName) -replace $ChangeFrom2, $ChangeTo2 | Set-Content $fileName\n## }\n## else\n## {\n## Write-Host \"Adding new line containing SeLockMemoryPrivilege\"\n## Add-Content $filename \"`nSeLockMemoryPrivilege = $ServiceName\"\n## }\n\n#Option SeBatchLogonRight (Log on as Batch job)\nIF ((Get-Content $fileName) | where { $_.Contains(\"SeBatchLogonRight\") })\n{\nWrite-Host \"Appending line containing SeBatchLogonRight with $ServiceName\"\n(Get-Content $fileName) -replace $ChangeFrom3, $ChangeTo3 | Set-Content $fileName\n}\nelse\n{\nWrite-Host \"Adding new line containing SeBatchLogonRight\"\nAdd-Content $filename \"`nSeBatchLogonRight = $ServiceName\"\n}\n\n\n#Option SeServiceLogonRight (log on as a service)\nIF ((Get-Content $fileName) | where { $_.Contains(\"SeServiceLogonRight\") })\n{\nWrite-Host \"Appending line containing SeServiceLogonRight with $ServiceName\"\n(Get-Content $fileName) -replace $ChangeFrom4, $ChangeTo4 | Set-Content $fileName\n}\nelse\n{\nWrite-Host \"Adding new line containing SeServiceLogonRight\"\nAdd-Content $filename \"`nSeServiceLogonRight = $ServiceName\"\n}\n\n\n#Option SeAuditPrivilege (Generate security audits)\n## Optional: Only required if a Log Management Server is configured and you want to generate Audit Files \n## IF ((Get-Content $fileName) | where { $_.Contains(\"SeAuditPrivilege\") })\n## {\n## Write-Host \"Appending line containing SeAuditPrivilege with $ServiceName\"\n## (Get-Content $fileName) -replace $ChangeFrom5, $ChangeTo5 | Set-Content $fileName\n## }\n## else\n## {\n## Write-Host \"Adding new line containing SeAuditPrivilege\"\n## Add-Content $filename \"`nSeAuditPrivilege = $ServiceName\"\n## }\n\n\n# Import new Security Policy cfg (using '1&gt; $null' to keep the output quiet)\nWrite-Host \"Importing Security Policy...\"\nsecedit \/configure \/db secedit.sdb \/cfg $fileName 1&gt; $null\nWrite-Host \"done: local security policies changed according to SQL Standards\"\n\n## Set the SQL Services to run with the new Managed Service account\nUpdate-DbaServiceAccount -ServiceName 'MSSQLSERVER','SQLSERVERAGENT' -UserName $ServiceName<\/code><\/pre>\n<\/div><\/div>\n\n\n","protected":false},"excerpt":{"rendered":"<p>In the past years, I actively have been involved in securing MSSQL Instances (and other services).This lead me to use the Managed Service Accounts (MSA) and the grouped Managed Service Accounts (gMSA)The MSA have been introduced in Windows Server 2008 R2 and the gMSA in Windows Server 2012. I. What exactly are MSA or gMSA [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229,99],"tags":[272,3371,3370],"type_dbi":[2874],"class_list":["post-32013","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","category-sql-server","tag-powershell","tag-service-accounts","tag-sql-security","type-sql-server"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Make SQL Server services more secure with Managed Service Accounts - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Make SQL Server services more secure with Managed Service Accounts\" \/>\n<meta property=\"og:description\" content=\"In the past years, I actively have been involved in securing MSSQL Instances (and other services).This lead me to use the Managed Service Accounts (MSA) and the grouped Managed Service Accounts (gMSA)The MSA have been introduced in Windows Server 2008 R2 and the gMSA in Windows Server 2012. I. What exactly are MSA or gMSA [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-05T18:04:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-06T06:58:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png\" \/>\n<meta name=\"author\" content=\"Microsoft Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Microsoft Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/\"},\"author\":{\"name\":\"Microsoft Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\"},\"headline\":\"Make SQL Server services more secure with Managed Service Accounts\",\"datePublished\":\"2024-06-05T18:04:09+00:00\",\"dateModified\":\"2024-06-06T06:58:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/\"},\"wordCount\":798,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png\",\"keywords\":[\"PowerShell\",\"Service Accounts\",\"SQL Security\"],\"articleSection\":[\"Database Administration &amp; Monitoring\",\"SQL Server\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/\",\"name\":\"Make SQL Server services more secure with Managed Service Accounts - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png\",\"datePublished\":\"2024-06-05T18:04:09+00:00\",\"dateModified\":\"2024-06-06T06:58:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png\",\"contentUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png\",\"width\":940,\"height\":72},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Make SQL Server services more secure with Managed Service Accounts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\",\"name\":\"Microsoft Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"caption\":\"Microsoft Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/microsoft-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Make SQL Server services more secure with Managed Service Accounts - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/","og_locale":"en_US","og_type":"article","og_title":"Make SQL Server services more secure with Managed Service Accounts","og_description":"In the past years, I actively have been involved in securing MSSQL Instances (and other services).This lead me to use the Managed Service Accounts (MSA) and the grouped Managed Service Accounts (gMSA)The MSA have been introduced in Windows Server 2008 R2 and the gMSA in Windows Server 2012. I. What exactly are MSA or gMSA [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/","og_site_name":"dbi Blog","article_published_time":"2024-06-05T18:04:09+00:00","article_modified_time":"2024-06-06T06:58:16+00:00","og_image":[{"url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png","type":"","width":"","height":""}],"author":"Microsoft Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Microsoft Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/"},"author":{"name":"Microsoft Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4"},"headline":"Make SQL Server services more secure with Managed Service Accounts","datePublished":"2024-06-05T18:04:09+00:00","dateModified":"2024-06-06T06:58:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/"},"wordCount":798,"commentCount":0,"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png","keywords":["PowerShell","Service Accounts","SQL Security"],"articleSection":["Database Administration &amp; Monitoring","SQL Server"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/","url":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/","name":"Make SQL Server services more secure with Managed Service Accounts - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png","datePublished":"2024-06-05T18:04:09+00:00","dateModified":"2024-06-06T06:58:16+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#primaryimage","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/06\/image-5.png","width":940,"height":72},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/make-sql-server-services-more-secure-with-managed-service-accounts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Make SQL Server services more secure with Managed Service Accounts"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4","name":"Microsoft Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","caption":"Microsoft Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/microsoft-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/32013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=32013"}],"version-history":[{"count":4,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/32013\/revisions"}],"predecessor-version":[{"id":34165,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/32013\/revisions\/34165"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=32013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=32013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=32013"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=32013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}