Solved

powershell script to connect standalone or cluster SQL

Posted on 2013-01-22
2
705 Views
Last Modified: 2013-01-23
Hello,

How can I use this script for connect to standalone or cluster SQL :
$list_inst = (get-service -Name "MSSQL*" -exclude "*Launcher*" | Where-Object {$_.status -eq "Running"})

foreach($inst in $list_inst) {
	$inst_name = $inst.name.replace("MSSQL$","")
	try
	{
	$cluster=get-wmiobject -class "MSCluster_Cluster" -namespace "root\mscluster" -computername "$mach_name" -ErrorAction Stop | select -ExpandProperty Name    
	}
	catch [Exception]
	{ 
    	echo "$cluster" | out-file $LOG_FILE -ErrorVariable ERR1 -encoding "default" -append -ErrorAction silentlycontinue 
	}
	function GetClusterSQL
	{
    	param($cluster)
   
    	gwmi -class "MSCluster_Resource" -namespace "root\mscluster" -computername $cluster  | where {$_.type -eq "SQL Server"} | Select `
    	@{n='ServerInstance';e={("{0}\{1}" -f $_.PrivateProperties.VirtualServerName,$_.PrivateProperties.InstanceName).TrimEnd('\')}} | 
    	Select -ExpandProperty ServerInstance
   
	}
	if($cluster -ne $null) {
	$list_vsql = GetClusterSQL $cluster
	}
	if($cluster -eq $null) {
		$ser_name = (Get-WmiObject Win32_Computersystem).Name
	}

	if($inst_name -eq "MSSQLSERVER") {
		$server = $ser_name
	}
	if($cluster -ne $null) {
	foreach($vsql in $list_vsql) {
		$server = $vsql
		[string] $ser_name =  $server.split("\")[0]
	}
	}
	$server = $ser_name + '\'+$inst_name
	function IsMSSQLUp([string] $server)
	{
		trap { 
			write-host "Message: " $_.Exception.Message ; 
			return $false ; 
		} ;

		$Connection = New-Object System.Data.SQLClient.SQLConnection
		$Connection.ConnectionString = "server=$server;Database=master;trusted_connection=true;"
		$Connection.Open()
		return $true;
		$Connection.close()
	}


	$IsMSSQLUp = IsMSSQLUp([string] $server)
}

Open in new window


Thanks

bibi
0
Comment
Question by:bibi92
2 Comments
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 38806443
0
 

Author Closing Comment

by:bibi92
ID: 38809247
Thanks bibi
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question