Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more Linux support #7584

Merged
merged 19 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 30 additions & 29 deletions dbatools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ if (($PSVersionTable.PSVersion.Major -le 5) -or $script:isWindows) {
Write-ImportTime -Text "Unblocking Files"
}


$script:DllRoot = (Resolve-Path -Path "$script:PSModuleRoot\bin\").ProviderPath

<#
Expand Down Expand Up @@ -805,14 +804,24 @@ $script:xplat = @(
'Remove-DbaDbFileGroup',
'Set-DbaDbFileGroup',
'Remove-DbaLinkedServer',
'Test-DbaAvailabilityGroup'
'Test-DbaAvailabilityGroup',
'Export-DbaUser',
'Get-DbaSsisExecutionHistory',
'New-DbaConnectionStringBuilder',
'New-DbatoolsSupportPackage',
'Export-DbaScript',
'Get-DbaAgentJobOutputFile',
'Set-DbaAgentJobOutputFile',
'Import-DbaXESessionTemplate',
'Export-DbaXESessionTemplate',
'Import-DbaSpConfigure',
'Export-DbaSpConfigure',
'Test-DbaMaxMemory',
'Install-DbaMaintenanceSolution'
)

$script:noncoresmo = @(
# SMO issues
'New-DbaConnectionStringBuilder',
'Export-DbaUser',
'Get-DbaSsisExecutionHistory',
'Get-DbaRepDistributor',
'Copy-DbaPolicyManagement',
'Copy-DbaDataCollector',
Expand All @@ -825,30 +834,18 @@ $script:noncoresmo = @(
'Get-DbaRepPublication',
'Test-DbaRepLatency',
'Export-DbaRepServerSetting',
'Get-DbaRepServer',
'Move-DbaDbFile'
'Get-DbaRepServer'
)
$script:windowsonly = @(
# solvable filesystem issues or other workarounds
# filesystem (\\ related),
'Move-DbaDbFile'
'Copy-DbaBackupDevice',
'Install-DbaSqlWatch',
'Uninstall-DbaSqlWatch',
'Get-DbaRegistryRoot',
'Install-DbaMaintenanceSolution',
'New-DbatoolsSupportPackage',
'Export-DbaScript',
'Get-DbaAgentJobOutputFile',
'Set-DbaAgentJobOutputFile',
'New-DbaDacProfile',
'Import-DbaXESessionTemplate',
'Export-DbaXESessionTemplate',
'Import-DbaSpConfigure',
'Export-DbaSpConfigure',
'Read-DbaXEFile',
'Watch-DbaXESession',
'Test-DbaMaxMemory', # can be fixed by not testing remote when linux is detected
'Rename-DbaDatabase', # can maybebe fixed by not remoting when linux is detected
# Registry
'Get-DbaRegistryRoot',
# CM and Windows functions
'Rename-DbaDatabase',
'Get-DbaNetworkConfiguration',
'Set-DbaNetworkConfiguration',
'Get-DbaExtendedProtection',
Expand Down Expand Up @@ -962,15 +959,19 @@ $script:windowsonly = @(
# WPF
'Show-DbaInstanceFileSystem',
'Show-DbaDbList',
# AD?
# AD
'Test-DbaWindowsLogin',
'Find-DbaLoginInGroup',
# 3rd party non-core DLL or exe
'Export-DbaDacPackage', # relies on sqlpackage.exe
# Unknown
'Get-DbaErrorLog',
# 3rd party non-core DLL or sqlpackage.exe
'Install-DbaSqlWatch',
'Uninstall-DbaSqlWatch',
'New-DbaDacProfile',
'Export-DbaDacPackage'
# No GAC
'Get-DbaManagementObject',
'Test-DbaManagementObject'
'Test-DbaManagementObject',
# Unknown
'Get-DbaErrorLog'
)

# If a developer or appveyor calls the psm1 directly, they want all functions
Expand Down
2 changes: 1 addition & 1 deletion functions/Backup-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ function Backup-DbaDatabase {
}
}

if ($AzureBaseUrl -or $AzureCredential) {
if ($AzureBaseUrl -or $AzureCredential -or $IsLinux -or $IsMacOs) {
$slash = "/"
} else {
$slash = "\"
Expand Down
4 changes: 2 additions & 2 deletions functions/Backup-DbaDbCertificate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ function Backup-DbaDbCertificate {
$actualPath = Get-SqlDefaultPaths -SqlInstance $server -filetype Data
}

$actualPath = "$actualPath".TrimEnd('\')
$fullCertName = "$actualPath\$certName$Suffix"
$actualPath = "$actualPath".TrimEnd('\').TrimEnd('/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think .TrimEnd('/\')should also work:
https://docs.microsoft.com/de-de/dotnet/api/system.string.trimend
But not sure if back to PS3.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it then a part failed, which may have been unrelated, so then I went back to the regular way

$fullCertName = [IO.Path]::Combine($actualPath, "$certName$Suffix")
$exportPathKey = "$fullCertName.pvk"

if (!(Test-DbaPath -SqlInstance $server -Path $actualPath)) {
Expand Down
4 changes: 2 additions & 2 deletions functions/Backup-DbaDbMasterKey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ function Backup-DbaDbMasterKey {

$time = (Get-Date -Format yyyMMddHHmmss)
$dbName = $db.name
$Path = $Path.TrimEnd("\")
$Path = $Path.TrimEnd("\").TrimEnd("/")
$fileinstance = $instance.ToString().Replace('\', '$')
$filename = "$Path\$fileinstance-$dbName-$time.key"
$filename = [IO.Path]::Combine($Path, "$fileinstance-$dbName-$time.key")

if ($Pscmdlet.ShouldProcess($instance, "Backing up master key to $filename")) {
try {
Expand Down
2 changes: 1 addition & 1 deletion functions/Backup-DbaServiceMasterKey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function Backup-DbaServiceMasterKey {
$time = (Get-Date -Format yyyMMddHHmmss)
$Path = $Path.TrimEnd("\")
$fileinstance = $instance.ToString().Replace('\', '$')
$filename = "$Path\$fileinstance-SMK-$time.key"
$filename = [IO.Path]::Combine($Path, "$fileinstance-SMK-$time.key")

if ($Pscmdlet.ShouldProcess($instance, "Backing up service master key to $filename")) {
try {
Expand Down
3 changes: 2 additions & 1 deletion functions/ConvertTo-DbaXESession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function ConvertTo-DbaXESession {
[switch]$EnableException
)
begin {
$rawsql = Get-Content "$script:PSModuleRoot\bin\sp_SQLskills_ConvertTraceToEEs.sql" -Raw
$rawpath = [IO.Path]::Combine($script:PSModuleRoot, "bin", "sp_SQLskills_ConvertTraceToEEs.sql")
$rawsql = Get-Content $rawpath -Raw
}
process {
foreach ($trace in $InputObject) {
Expand Down
5 changes: 5 additions & 0 deletions functions/Export-DbaCredential.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ function Export-DbaCredential {
process {
if (Test-FunctionInterrupt) { return }

if ($IsLinux -or $IsMacOS) {
Stop-Function -Message "This command is not supported on Linux or macOS"
return
}

if (-not $InputObject -and -not $SqlInstance) {
Stop-Function -Message "You must pipe in a Credential or specify a SqlInstance"
return
Expand Down
6 changes: 5 additions & 1 deletion functions/Export-DbaDbRole.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ function Export-DbaDbRole {
$outsql = @()
$outputFileArray = @()
$roleCollection = New-Object System.Collections.ArrayList
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
if ($IsLinux -or $IsMacOs) {
$executingUser = $env:USER
} else {
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
}
$commandName = $MyInvocation.MyCommand.Name

$roleSQL = "SELECT
Expand Down
8 changes: 6 additions & 2 deletions functions/Export-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function Export-DbaInstance {
# Ensure the export dir exists.
if (-not (Test-Path $exportPath)) {
try {
$null = New-Item -ItemType Directory -Path $exportPath -ErrorAction Stop
$null = New-Item -ItemType Directory -Path $exportPath -Force -ErrorAction Stop
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_
return
Expand Down Expand Up @@ -311,7 +311,10 @@ function Export-DbaInstance {
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\endpoints.sql"
}

if ($Exclude -notcontains 'PolicyManagement') {
if ($Exclude -notcontains 'PolicyManagement' -and $PSVersionTable.PSEdition -eq "Core") {
Write-Message -Level Verbose -Message "Skipping Policy Management -- not supported by PowerShell Core"
}
if ($Exclude -notcontains 'PolicyManagement' -and $PSVersionTable.PSEdition -ne "Core") {
Write-Message -Level Verbose -Message "Exporting Policy Management"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Policy Management"

Expand All @@ -320,6 +323,7 @@ function Export-DbaInstance {
$policyObjects = @()

# the policy objects are a different set of classes and are not compatible with the SMO object usage in Export-DbaScript

$policyObjects += Get-DbaPbmCondition -SqlInstance $server
$policyObjects += Get-DbaPbmObjectSet -SqlInstance $server
$policyObjects += Get-DbaPbmPolicy -SqlInstance $server
Expand Down
6 changes: 6 additions & 0 deletions functions/Export-DbaLinkedServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ function Export-DbaLinkedServer {
}
process {
if (Test-FunctionInterrupt) { return }

if ($IsLinux -or $IsMacOS) {
Stop-Function -Message "This command is not supported on Linux or macOS"
return
}

foreach ($instance in $SqlInstance) {
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
Expand Down
6 changes: 5 additions & 1 deletion functions/Export-DbaLogin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ function Export-DbaLogin {
$outsql = @()
$instanceArray = @()
$logonCollection = New-Object System.Collections.ArrayList
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
if ($IsLinux -or $IsMacOs) {
$executingUser = $env:USER
} else {
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
}
$commandName = $MyInvocation.MyCommand.Name

}
Expand Down
5 changes: 3 additions & 2 deletions functions/Export-DbaRegServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ function Export-DbaRegServer {
}

if (-not (Test-Path $FilePath) ) {
New-Item -Path $FilePath.DirectoryName -ItemType "directory" -Force | Out-Null # make sure the parent dir exists
$null = Test-ExportDirectory -Path (Split-Path -Path $FilePath)
"" | Set-Content -Path $FilePath
} elseif (-not $Overwrite.IsPresent) {
Stop-Function -Message "Use the -Overwrite parameter if the file $FilePath should be overwritten."
return
Expand Down Expand Up @@ -149,7 +150,7 @@ function Export-DbaRegServer {

if (-not $PSBoundParameters.ContainsKey("FilePath")) {
$ExportFileName = $null
$serverName = $object.SqlInstance.Replace('\', '$');
$serverName = $object.SqlInstance.Replace('\', '$')

if ($object -is [Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer]) {
$ExportFileName = "$serverName-regserver-$regname-$timeNow.xml"
Expand Down
6 changes: 4 additions & 2 deletions functions/Export-DbaScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ function Export-DbaScript {
)
begin {
$null = Test-ExportDirectory -Path $Path
if ($IsWindows -ne $false) {
if ($IsLinux -or $IsMacOs) {
$executingUser = $env:USER
} else {
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
} else { $executingUser = $env:USER }
}
$commandName = $MyInvocation.MyCommand.Name
$prefixArray = @()

Expand Down
6 changes: 5 additions & 1 deletion functions/Export-DbaServerRole.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ function Export-DbaServerRole {
$outsql = @()
$outputFileArray = @()
$roleCollection = New-Object System.Collections.ArrayList
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
if ($IsLinux -or $IsMacOs) {
$executingUser = $env:USER
} else {
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
}
$commandName = $MyInvocation.MyCommand.Name

$roleSQL = "SELECT
Expand Down
6 changes: 5 additions & 1 deletion functions/Export-DbaXESession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ function Export-DbaXESession {
$null = Test-ExportDirectory -Path $Path
$instanceArray = @()
$SessionCollection = New-Object System.Collections.ArrayList
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
if ($IsLinux -or $IsMacOs) {
$executingUser = $env:USER
} else {
$executingUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
}
$commandName = $MyInvocation.MyCommand.Name
}
process {
Expand Down
3 changes: 2 additions & 1 deletion functions/Export-DbaXESessionTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function Export-DbaXESessionTemplate {
}
process {
if (Test-FunctionInterrupt) { return }

foreach ($instance in $SqlInstance) {
try {
$InputObject += Get-DbaXESession -SqlInstance $instance -SqlCredential $SqlCredential -Session $Session -EnableException
Expand All @@ -89,7 +90,7 @@ function Export-DbaXESessionTemplate {
}

if (-not $PSBoundParameters.FilePath) {
$FilePath = "$Path\$xesname.xml"
$FilePath = Join-DbaPath $Path "$xesname.xml"
}
Write-Message -Level Verbose -Message "Wrote $xesname to $FilePath"
[Microsoft.SqlServer.Management.XEvent.XEStore]::SaveSessionToTemplate($xes, $FilePath, $true)
Expand Down
4 changes: 4 additions & 0 deletions functions/Get-DbaPbmCondition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ function Get-DbaPbmCondition {
[switch]$EnableException
)
process {
if ($PSVersionTable.PSEdition -eq "Core") {
Stop-Function -Message "This command is not yet supported in PowerShell Core"
return
}
foreach ($instance in $SqlInstance) {
$InputObject += Get-DbaPbmStore -SqlInstance $instance -SqlCredential $SqlCredential
}
Expand Down
4 changes: 4 additions & 0 deletions functions/Get-DbaPbmStore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ function Get-DbaPbmStore {
)

process {
if ($PSVersionTable.PSEdition -eq "Core") {
Stop-Function -Message "This command is not supported on Linux or macOS"
return
}
foreach ($instance in $SqlInstance) {

try {
Expand Down
4 changes: 4 additions & 0 deletions functions/Get-DbaRepServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function Get-DbaRepServer {
[switch]$EnableException
)
begin {
if ($PSVersionTable.PSEdition -eq "Core") {
Stop-Function -Message "This command is not yet supported in PowerShell Core"
return
}
try {
Add-Type -Path "$script:PSModuleRoot\bin\smo\Microsoft.SqlServer.Replication.dll" -ErrorAction Stop
Add-Type -Path "$script:PSModuleRoot\bin\smo\Microsoft.SqlServer.Rmo.dll" -ErrorAction Stop
Expand Down
5 changes: 3 additions & 2 deletions functions/Get-DbaXESessionTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ function Get-DbaXESessionTemplate {
[switch]$EnableException
)
begin {
$metadata = Import-Clixml "$script:PSModuleRoot\bin\xetemplates-metadata.xml"
$xmlpath = Join-DbaPath $script:PSModuleRoot "bin" "xetemplates-metadata.xml"
$metadata = Import-Clixml $xmlpath
# In case people really want a "like" search, which is slower
$Pattern = $Pattern.Replace("*", ".*").Replace("..*", ".*")
}
process {
foreach ($directory in $Path) {
$files = Get-ChildItem "$directory\*.xml"
$files = Get-ChildItem "$(Join-DbaPath $directory *.xml)"

if ($Template) {
$files = $files | Where-Object BaseName -in $Template
Expand Down