Skip to content

Commit 0739572

Browse files
ConvertTo-DbaXESession - Add SqlCredential parameter for cross-domain support (#9953)
1 parent 8c350e0 commit 0739572

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

public/ConvertTo-DbaXESession.ps1

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ function ConvertTo-DbaXESession {
2121
Specifies the name for the new Extended Events session. If a session with this name already exists, the function automatically appends the trace ID or a random number to avoid conflicts.
2222
Choose a descriptive name that identifies the monitoring purpose, as this becomes the session name visible in SQL Server Management Studio and sys.server_event_sessions.
2323
24+
.PARAMETER SqlCredential
25+
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
26+
27+
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
28+
29+
For MFA support, please use Connect-DbaInstance.
30+
2431
.PARAMETER OutputScriptOnly
2532
Returns the T-SQL CREATE EVENT SESSION script without executing it on the server. Use this when you need to review the generated script before deployment or save it for later execution.
2633
Particularly useful in compliance environments where all scripts require approval before running against production databases.
@@ -63,6 +70,7 @@ function ConvertTo-DbaXESession {
6370
[object[]]$InputObject,
6471
[parameter(Mandatory)]
6572
[string]$Name,
73+
[PSCredential]$SqlCredential,
6674
[switch]$OutputScriptOnly,
6775
[switch]$EnableException
6876
)
@@ -87,13 +95,22 @@ function ConvertTo-DbaXESession {
8795
$tempdb = $server.Databases['tempdb']
8896
$traceid = $trace.id
8997

90-
if ((Get-DbaXESession -SqlInstance $server -Session $PSBoundParameters.Name)) {
98+
$splatXESession = @{
99+
SqlInstance = $server
100+
Session = $PSBoundParameters.Name
101+
}
102+
if ($SqlCredential) {
103+
$splatXESession["SqlCredential"] = $SqlCredential
104+
}
105+
106+
if ((Get-DbaXESession @splatXESession)) {
91107
$oldname = $name
92108
$Name = "$name-$traceid"
93109
Write-Message -Level Output -Message "XE Session $oldname already exists on $server, trying $name."
94110
}
95111

96-
if ((Get-DbaXESession -SqlInstance $server -Session $Name)) {
112+
$splatXESession["Session"] = $Name
113+
if ((Get-DbaXESession @splatXESession)) {
97114
$oldname = $name
98115
$Name = "$name-$(Get-Random)"
99116
Write-Message -Level Output -Message "XE Session $oldname already exists on $server, trying $name."
@@ -120,7 +137,14 @@ function ConvertTo-DbaXESession {
120137
} catch {
121138
Stop-Function -Message "Issue creating extended event $name on $server." -Target $server -ErrorRecord $_
122139
}
123-
Get-DbaXESession -SqlInstance $server -Session $name
140+
$splatGetSession = @{
141+
SqlInstance = $server
142+
Session = $name
143+
}
144+
if ($SqlCredential) {
145+
$splatGetSession["SqlCredential"] = $SqlCredential
146+
}
147+
Get-DbaXESession @splatGetSession
124148
}
125149
}
126150
}

tests/ConvertTo-DbaXESession.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Describe $CommandName -Tag UnitTests {
1313
$expectedParameters += @(
1414
"InputObject",
1515
"Name",
16+
"SqlCredential",
1617
"OutputScriptOnly",
1718
"EnableException"
1819
)

0 commit comments

Comments
 (0)