Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -Path (Get-Command 'Microsoft.SqlServer.Management.UserSettings.dll').Source
- [bool]$loaded = $false
- Get-Item -Path 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.ConnectionInfo\*\Microsoft.SqlServer.ConnectionInfo.dll' | Select-Object -ExpandProperty FullName | Sort-Object -Descending | ForEach-Object {if (!$loaded ){try {Add-Type -Path $_;Write-Verbose "Successfully loaded $_";$loaded=$true}catch{Write-Warning "Failed to load $_"}}}
- [string]$settingsFilePath = (Resolve-Path (Join-Path -Path $env:APPDATA -ChildPath 'Microsoft/SQL Server Management Studio/*/SqlStudio.bin')).Path | Sort-Object -Descending | Select-Object -First 1
- if ($settingsFilePath) {
- try {
- [System.IO.MemoryStream]$ms = [System.IO.File]::ReadAllBytes($settingsFilePath)
- [System.Runtime.Serialization.Formatters.Binary.BinaryFormatter]$formatter = New-Object -TypeName 'System.Runtime.Serialization.Formatters.Binary.BinaryFormatter'
- [Microsoft.SqlServer.Management.UserSettings.SqlStudio]$sqlStudio = $formatter.Deserialize($ms) #[Microsoft.SqlServer.Management.UserSettings.SettingsDictionary[[System.Guid],[Microsoft.SqlServer.Management.UserSettings.ServerTypeItem]]]
- foreach ($serverTypeItem in $sqlStudio.SSMS.ConnectionOptions.ServerTypes.Values) { #[Microsoft.SqlServer.Management.UserSettings.ServerTypeItem]
- foreach ($server in $serverTypeItem.Servers) { # [Microsoft.SqlServer.Management.UserSettings.ServerConnectionItem]
- ([PSCustomObject][Ordered]@{
- Instance = $server.Instance
- AuthMeth = ([Microsoft.SqlServer.Management.Common.SqlConnectionInfo+AuthenticationMethod]$server.AuthenticationMethod)
- Connections = $server.Connections
- })
- }
- }
- } finally {
- $ms.Dispose()
- }
- } else {
- throw 'Could not find SqlStudio.bin. Older versions used MRU.dat... but I''ve not coded a solution for that, sorry!'
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement