From 2e1d4561a745a0665071edcc7020154fd44b394c Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Sun, 5 Apr 2026 08:34:39 +0000 Subject: [PATCH] New-DbaDatabase - Support Azure Blob Storage paths for data and log files Skip filesystem directory creation and use forward-slash separator when DataFilePath or LogFilePath is an Azure Blob Storage URL (https://...). Also trim trailing separators before concatenating file names to prevent double-separator paths. Fixes #6061 (do New-DbaDatabase) Co-authored-by: Andreas Jordan --- public/New-DbaDatabase.ps1 | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/public/New-DbaDatabase.ps1 b/public/New-DbaDatabase.ps1 index 3f3563c26d98..43aa5215c888 100644 --- a/public/New-DbaDatabase.ps1 +++ b/public/New-DbaDatabase.ps1 @@ -252,7 +252,19 @@ function New-DbaDatabase { $LogFilePath = (Get-DbaDefaultPath -SqlInstance $server).Log } - if (-not (Test-DbaPath -SqlInstance $server -Path $LogFilePath)) { + # Detect Azure Blob Storage URLs to skip filesystem directory operations + $dataPathIsAzure = $DataFilePath -like "https://*" + $logPathIsAzure = $LogFilePath -like "https://*" + + # Trim trailing separators to avoid double-separators when concatenating file names + $DataFilePath = $DataFilePath.TrimEnd('\', '/') + $LogFilePath = $LogFilePath.TrimEnd('\', '/') + + # Choose the path separator based on whether the path is an Azure Blob Storage URL + $dataPathSeparator = if ($dataPathIsAzure) { "/" } else { "\" } + $logPathSeparator = if ($logPathIsAzure) { "/" } else { "\" } + + if (-not $logPathIsAzure -and -not (Test-DbaPath -SqlInstance $server -Path $LogFilePath)) { try { Write-Message -Message "Creating directory $LogFilePath" -Level Verbose $null = New-DbaDirectory -SqlInstance $server -Path $LogFilePath -EnableException @@ -261,7 +273,7 @@ function New-DbaDatabase { } } - if (-not (Test-DbaPath -SqlInstance $server -Path $DataFilePath)) { + if (-not $dataPathIsAzure -and -not (Test-DbaPath -SqlInstance $server -Path $DataFilePath)) { try { Write-Message -Message "Creating directory $DataFilePath" -Level Verbose $null = New-DbaDirectory -SqlInstance $server -Path $DataFilePath -EnableException @@ -320,7 +332,7 @@ function New-DbaDatabase { #create the primary file $primaryfile = New-Object Microsoft.SqlServer.Management.Smo.DataFile($primaryfg, $primaryfilename) - $primaryfile.FileName = $DataFilePath + "\" + $primaryfilename + ".mdf" + $primaryfile.FileName = $DataFilePath + $dataPathSeparator + $primaryfilename + ".mdf" $primaryfile.IsPrimaryFile = $true if (Test-Bound -ParameterName PrimaryFilesize) { @@ -362,7 +374,7 @@ function New-DbaDatabase { } $tlog = New-Object Microsoft.SqlServer.Management.Smo.LogFile($newdb, $logname) - $tlog.FileName = $LogFilePath + "\" + $logname + ".ldf" + $tlog.FileName = $LogFilePath + $logPathSeparator + $logname + ".ldf" if (Test-Bound -ParameterName LogSize) { $tlog.Size = ($LogSize * 1024) @@ -420,7 +432,7 @@ function New-DbaDatabase { $secondaryfilename = "$($secondaryfilegroupname)_$($secondaryfgcount)" Write-Message -Message "Creating file name $secondaryfilename in filegroup $secondaryfilegroupname" -Level Verbose $secondaryfile = New-Object Microsoft.SQLServer.Management.Smo.Datafile($secondaryfg, $secondaryfilename) - $secondaryfile.FileName = $DataFilePath + "\" + $secondaryfilename + ".ndf" + $secondaryfile.FileName = $DataFilePath + $dataPathSeparator + $secondaryfilename + ".ndf" if (Test-Bound -ParameterName SecondaryFilesize) { $secondaryfile.Size = ($SecondaryFilesize * 1024)