Skip to content

Instantly share code, notes, and snippets.

View nvarscar's full-sized avatar

Kirill Kravtsov nvarscar

  • Victoria, BC
View GitHub Profile
@nvarscar
nvarscar / extract_dacpac.ps1
Created August 24, 2018 19:45
Extract dacpac package
Param (
$Server,
$Database,
$Path,
[switch]$IncludeData
)
# Prepare extraction properties string
$exportProperties = "/p:IgnorePermissions=True /p:IgnoreUserLoginMappings=True" # Ignore permissions definition
if ($IncludeData) {
$exportProperties += " /p:ExtractAllTableData=True" #Extract data
[Cmdletbinding()]
Param (
$SourceServer,
$TargetServer = $SourceServer,
$SourceDatabaseName,
$TargetDatabaseName,
$Path,
$PublishXml = '.\publish.xml',
[switch]$IncludeData
)
@nvarscar
nvarscar / publish.xml
Last active August 27, 2018 16:39
dacpac deployment Publish profile
<?xml version="1.0" ?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetConnectionString></TargetConnectionString>
<ProfileVersionNumber>1</ProfileVersionNumber>
<AllowIncompatiblePlatform>True</AllowIncompatiblePlatform>
<CreateNewDatabase>False</CreateNewDatabase>
<ExcludeObjectTypes>Permissions;RoleMembership</ExcludeObjectTypes>
<ExcludeLogins>True</ExcludeLogins>
<IgnorePermissions>True</IgnorePermissions>
@nvarscar
nvarscar / dbops_exports.ps1
Created October 23, 2018 18:59
dbops configuration examples
# Generating config files and using it later as a deployment template
New-DBOConfig -Configuration @{ DeploymentMethod = 'SingleTransaction'; SqlInstance = 'devInstance'; Database = 'MyDB' } | Export-DBOConfig '.\dev.json'
Get-DBOConfig -Path '.\dev.json' -Configuration @{ SqlInstance = 'prodInstance' } | Export-DBOConfig '.\prod.json'
Install-DBOPackage Deploy.zip -ConfigurationFile .\dev.json
Install-DBOPackage Deploy.zip -ConfigurationFile .\prod.json
# Extract current config from a package
Get-DBOPackage Deploy.zip | Export-DBOConfig '.\prod.json'
# Use existing configuration from a package and apply it to a different package
@nvarscar
nvarscar / examples_invoke-dboquery.ps1
Last active January 24, 2019 17:52
Examples for Invoke-DBOQuery
# Get current database from a PostgreSQL server
Invoke-DBOQuery -Type PostgreSQL -Server localhost:5432 -Database postgres -Query 'SELECT current_database()' -As SingleValue
# Run a query against MySQL database
Invoke-DBOQuery -Type MySQL -Server localhost -Database mysql -Query 'SELECT Name from Users'
# Run multiple files from a folder in a specific SQL Server database
Get-ChildItem .\Scripts | Invoke-DBOQuery -SqlInstance 'SqlServer1:1443' -Database MyDB
# Connect to an Oracle database interactively as SYSDBA
# install a DBOps package to the MySQL server
Install-DBOPackage -Type MySQL -Server localhost -Database mydb -Path .\package.zip
# invoke a query against a remote PostgreSQL server
Invoke-DBOQuery -Type PostgreSQL -SqlInstance pgsql1:5432 -Database mydb -Query 'SELECT current_database();'
# change the default connection -Type (SQLServer) to PostgreSQL:
Set-DBODefaultSetting -Name rdbms.type -Value 'PostgreSQL'
@nvarscar
nvarscar / prompt.ps1
Created February 15, 2019 18:14
prompt function for Powershell
function Prompt {
try {
$history = Get-History -ErrorAction Ignore -Count 1
if ($history) {
$ts = New-TimeSpan $history.StartExecutionTime $history.EndExecutionTime
switch ($ts) {
{$_.TotalMinutes -ge 1 } {
'[{0,5:f1} m]' -f $_.TotalMinutes | Write-Host -ForegroundColor Red -NoNewline
}
{$_.TotalMinutes -lt 1 -and $_.TotalSeconds -ge 1} {
@nvarscar
nvarscar / clone_user_permissions.ps1
Created January 17, 2020 21:21
Clone user permissions with dbatools
Param (
$SourceServer,
$TargetServer,
$SourceDatabase,
$TargetDatabase
)
$permissions = Export-DbaUser -SqlInstance $SourceServer -Database $SourceDatabase
Invoke-DbaQuery -SqlInstance $TargetServer -Database $TargetDatabase -Query $permissions
@nvarscar
nvarscar / clone_login_permissions.ps1
Created January 17, 2020 21:38
Clone login and apply permissions
Param (
$SourceServer,
$TargetServer,
$Login
)
$loginObject = Get-DbaLogin -SqlInstance $SourceServer -Login $Login
Copy-DbaLogin -Destination $TargetServer -Login $loginObject
foreach ($dbName in $login.EnumDatabaseMappings()) {
$db = Get-DbaDatabase -SqlInstance $SourceServer -Database $dbName
class testclass {
[string]$foo
testclass($input) {
$this.foo = $input
}
}
enum testenum {
foo
bar
}