diff --git a/lib/mssql/Get-IcingaMSSQLInstanceName.psm1 b/lib/mssql/Get-IcingaMSSQLInstanceName.psm1 new file mode 100644 index 0000000..11473b3 --- /dev/null +++ b/lib/mssql/Get-IcingaMSSQLInstanceName.psm1 @@ -0,0 +1,34 @@ +function Get-IcingaMSSQLInstanceName() +{ + param ( + $SqlConnection = $null, + [string]$Username, + [securestring]$Password, + [string]$Address = "localhost", + [int]$Port = 1433, + [switch]$IntegratedSecurity = $FALSE, + [switch]$TestConnection = $FALSE + ); + + [bool]$NewSqlConnection = $FALSE; + + if ($null -eq $SqlConnection) { + $SqlConnection = Open-IcingaMSSQLConnection -Username $Username -Password $Password -Address $Address -IntegratedSecurity:$IntegratedSecurity -Port $Port -TestConnection:$TestConnection; + + if ($null -eq $SqlConnection) { + return 'Unknown'; + } + + $NewSqlConnection = $TRUE; + } + + $Query = 'SELECT @@servicename' + $SqlCommand = New-IcingaMSSQLCommand -SqlConnection $SqlConnection -SqlQuery $Query; + $InstanceName = (Send-IcingaMSSQLCommand -SqlCommand $SqlCommand).Column1; + + if ($NewSqlConnection -eq $TRUE) { + Close-IcingaMSSQLConnection -SqlConnection $SqlConnection; + } + + return $InstanceName; +}