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

code is now working for clusters also #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Get-MSSQLLinkPasswords.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ function Get-MSSQLLinkPasswords{
Add-Type -assembly System.Security
Add-Type -assembly System.Core

# Set local computername and get all SQL Server instances
$ComputerName = $Env:computername
# get all SQL Server instances
$SqlInstances = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' -Name InstalledInstances).InstalledInstances

$Results = New-Object "System.Data.DataTable"
Expand All @@ -52,7 +51,15 @@ function Get-MSSQLLinkPasswords{
$Results.Columns.Add("Password") | Out-Null

foreach ($InstanceName in $SqlInstances) {

$instance_name_entry = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL' -Name $InstanceName).$InstanceName
# we can only get the cluster name if this instance is clustered
$cluster = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instance_name_entry\Cluster" -Name 'ClusterName').ClusterName 2> $null
if ($cluster) {
$ComputerName = $cluster
} else {
$ComputerName = $env:COMPUTERNAME
}

# Start DAC connection to SQL Server
# Default instance MSSQLSERVER -> instance name cannot be used in connection string
if ($InstanceName -eq "MSSQLSERVER") {
Expand All @@ -71,7 +78,7 @@ function Get-MSSQLLinkPasswords{
if ($Conn.State -eq "Open"){
# Query Service Master Key from the database - remove padding from the key
# key_id 102 eq service master key, thumbprint 3 means encrypted with machinekey
$SqlCmd="SELECT substring(crypt_property,9,len(crypt_property)-8) FROM sys.key_encryptions WHERE key_id=102 and (thumbprint=0x03 or thumbprint=0x0300000001)"
$SqlCmd="SELECT substring(crypt_property,9,len(crypt_property)-8) FROM master.sys.key_encryptions WHERE key_id=102 and (thumbprint=0x03 or thumbprint=0x0300000001)"
$Cmd = New-Object System.Data.SqlClient.SqlCommand($SqlCmd,$Conn);
$SmkBytes=$Cmd.ExecuteScalar()

Expand Down