I have finished the script auto download file backup from storage to import for a new environment and delete older users from file backup to meet security:
$DBServer = 'dev01.database.windows.net'
$DbUser = 'admin'
$DBPassword = '12131231'
$StorageAccountName = 'adminstorage'
$StorageAccountKey = 'sdfsdfsdf'
$ContainerName = 'Catalog-backup'
$Backupfilename = 'Catalog-2022-08-23.bacpac'
$DBName = 'Catalog'
Copy file from blob storage
$ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
write-output "backup file name: $Backupfilename"
Get-AzStorageBlobContent -Blob $Backupfilename -Container $ContainerName -Destination $PSScriptRoot -Context $ctx
Check name database
Invoke-Sqlcmd -ServerInstance tcp:$DBServer -Username $DbUser -Password "$DBPassword" -Query "Drop database [$DBNameimport]"
Invoke-Sqlcmd -ServerInstance tcp:$DBServer -Username $DbUser -Password "$DBPassword" -Query "Create database [$DBNameimport]"
Restore file to SQL Server
sqlpackage.exe /Action:Import /tsn:tcp:$DBServer,1433 /tdn:$DBNameimport /tu:$DbUser /tp:"$DBPassword" /sf:$PSScriptRoot\$Backupfilename /p:Storage=File
Delete older user when finish import
$query = "
DECLARE @commandcommand nvarchar(max) = ''
SELECT @commandcommand += 'DROP USER ['+name+'];'
FROM sys.sysusers
WHERE name like 'au%'
EXEC (@commandcommand );
"
Invoke-Sqlcmd -ServerInstance tcp:$DBServer -Username $DbUser -Password "$DBPassword" -Database $DBNameimport -Query $query