SQL Server Management Studio

Microsoft.SQLServer.Core.ConsoleTask.AvailabilityGroup.RunSSMS (ConsoleTask)

Open SQL Server Management Studio and connect to Primary Replica of target Availability Group.

Element properties:

TargetMicrosoft.SQLServer.Core.AvailabilityGroup
Applicationpowershell.exe
WorkingDirectory
isRequireOutputFalse
AccessibilityInternal
EnabledTrue

Source Code:

<ConsoleTask ID="Microsoft.SQLServer.Core.ConsoleTask.AvailabilityGroup.RunSSMS" Accessibility="Internal" Target="Microsoft.SQLServer.Core.AvailabilityGroup" RequireOutput="false">
<Assembly>Res.Microsoft.SQLServer.Core.ConsoleTask.AvailabilityGroup.RunSSMS</Assembly>
<Handler>ShellHandler</Handler>
<Parameters>
<Argument Name="Application">powershell.exe</Argument>
<Argument Name="WorkingDirectory"/>
<Argument>-NoLogo -NoProfile -Command Invoke-Command {
$p = 0
$serverConnectionStringParam = @'
$Target/Property[Type="Microsoft.SQLServer.Core.AvailabilityGroup"]/PrimaryReplicaServerName$
'@

function GetBaseRegPath(){
if([Environment]::Is64BitOperatingSystem){
$regBasePath = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\'
}
else{
$regBasePath = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\'
}
return $regBasePath
}

function GetToolInfoPre18($toolName){

$sqlToolsVersions = @(140, 130, 120, 110)

$toolsRelPaths = @{
'ssms' = 'ManagementStudio\ssms.exe'
'profiler' = 'profiler.exe'
}

$basePath = GetBaseRegPath

$foundTools = @()

foreach($toolVersion in $sqlToolsVersions){
$regPath = Join-Path -Path $basePath -ChildPath ('Microsoft SQL Server\' + $toolVersion + '\Tools\ClientSetup')
$regKey = $null
$regKeyVersion = $null
$regKey = Get-Item $regPath -ErrorAction Ignore
$regKeyVersion = Get-Item ($regPath + '\CurrentVersion') -ErrorAction Ignore
if($regKey -eq $null -or $regKeyVersion -eq $null){
continue
}

$toolRootPath = $regKey.GetValue('Path')
$toolVersion = $regKeyVersion.GetValue('CurrentVersion')

if($toolRootPath -eq $null -or [string]::IsNullOrEmpty($toolVersion)){
continue
}

$toolPath = Join-Path -Path $toolRootPath -ChildPath $toolsRelPaths[$toolName]

if(![System.IO.File]::Exists($toolPath)){
continue
}

$foundTools += new-object PSObject -Property @{'Path' = $toolPath; 'Version' = $toolVersion}
}

return (,$foundTools)
}

function GetToolInfoPost18($toolName){
$toolsRelPaths = @{
'ssms' = 'Common7\IDE\Ssms.exe'
'profiler' = 'Common7\PROFILER.EXE'
}

$basePath = GetBaseRegPath

$foundTools = @()

$toolVersionRegKeys = Get-Item -Path ($basePath + 'Microsoft SQL Server Management Studio\') -ErrorAction Ignore | Get-ChildItem

$toolFullPath = $null
if($toolVersionRegKeys -ne $null){
foreach($regKey in $toolVersionRegKeys){
$toolTempPath = Join-Path -Path $regKey.GetValue('SSMSInstallRoot') -ChildPath $toolsRelPaths[$toolName]
$toolVersion = $regKey.GetValue('Version')

if(![System.IO.File]::Exists($toolTempPath) -or [string]::IsNullOrEmpty($toolVersion)){
continue
}
$foundTools += new-object PSObject -Property @{'Path' = $toolTempPath; 'Version'= $toolVersion}
}
}
return (,$foundTools)
}

function GetSqlToolExePath($toolName, $toolExeName){

$allTools = @()
$allTools += GetToolInfoPost18 $toolName
$allTools += GetToolInfoPre18 $toolName

$allTools = (,$allTools | Sort-Object -Property Version -Descending)

if($allTools.Length -gt 0){
$toolFullPath = $allTools[0].Path
}

if($toolFullPath -eq $null){
$toolFullPath = Get-Command $toolExeName -ErrorAction SilentlyContinue | Select -ExpandProperty Source
}

return $toolFullPath
}

function GetSsmsPath(){
return GetSqlToolExePath 'ssms' 'ssms.exe'
}

function GetProfilerPath(){
return GetSqlToolExePath 'profiler' 'profiler.exe'
}

$ssmsPath = GetSsmsPath

&amp; $ssmsPath @('-S', $serverConnectionStringParam, '-E')
}</Argument>
</Parameters>
</ConsoleTask>