Skip to content

Get-Instance

DBSystems: Gets SQL Server instance information

#Requires -Version 5.0
#Requires -Modules SQLServer

[CmdLetBinding()]
Param(
    [Parameter(Mandatory = $true)]   
    [string]$ServerInstance,    
    [pscredential]$ServerCredential,
    [int]$ConnectionTimeout = 30,
    [Validateset('*','DisplayNameOrName','Status','Edition','InstanceName','DomainInstanceName','LoginMode','ServerType','ServiceStartMode','ComputerNamePhysicalNetBIOS')]
    [string[]]$Properties = @('DisplayNameOrName','Status','Edition','InstanceName','DomainInstanceName','LoginMode','ServerType','ServiceStartMode','ComputerNamePhysicalNetBIOS')
)

Import-Module SQLServer

try {
    if ($Properties -contains '*') {
        $Properties = @('*')
    }
    
    [hashtable]$cmdArgs = @{
        'ErrorAction' = 'Stop'
        'Confirm' = $false
        'ServerInstance' = $ServerInstance
        'ConnectionTimeout' = $ConnectionTimeout
    }

    if ($null -ne $ServerCredential) {
        $cmdArgs.Add('Credential', $ServerCredential)
    }
    
    $result = Get-SqlInstance @cmdArgs | Select-Object $Properties
    Write-Output $result
} catch {
    throw
}

Specifies the name of the target computer including the instance name, e.g. MyServer\Instance

Specifies a PSCredential object for the connection to the SQL Server. ServerCredential is ONLY used for SQL Logins. When you are using Windows Authentication you don't specify -Credential. It is picked up from your current login.

Specifies the time period to retry the command on the target server

List of properties to expand, comma separated e.g. Edition,Status. Use * for all properties

An interactive directory of PowerShell scripts.