Created
July 19, 2021 22:13
-
-
Save paschott/087045ea6d1f96502ac2345ad952145f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module dbatools | |
Import-Module sqlserver #used for Invoke-SqlCmd to run scripts w/ the "GO" batch separator | |
$CurrentDate = (Get-Date).ToString("yyyyMMdd_hhmmss") | |
$Servername = "localhost" | |
$databases = 'CSV_Set_Of_DB_Names' | |
#Get permissions for databases | |
# Export to a sql file in case needed | |
$permissions = Export-DbaUser -SqlInstance $Servername -Database $databases | |
if (!($null -eq $permissions)) { | |
get-content $permissions | out-file ("C:\SQLScripts\Permissions_" + $CurrentDate + ".sql") | |
} | |
#Reset permissions | |
# May need to use something else here. Maybe a foreach for $permissions and Invoke-SQLCmd because of the USE statements... | |
# Can still use the generated SQL file to get permissions, though. | |
foreach ($permission in $permissions) { | |
$query = get-content($permission).ToString() -Raw | |
Invoke-SqlCmd -ServerInstance $Servername -Query $query | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment