Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

powershell to get instance name

$
0
0

Hi,

Not sure whether this is the forum to ask.I have a list of servers names and i need to get the instance names in below format.

SERVERNAME\INSTANCE1

The o/p of the script actually shows Servername,MSSQL$Instance name..Anyway to tweak this to SERVERNAME\INSTANCE1?

$servers = get-content "C:\SQLCheck\machinelist.txt"
$logfile = "C:\SQLCheck\sql-instances.txt"
$logerrs = "C:\SQLCheck\sql-failures.txt"

Echo "Server, Instance" >> $logfile

ForEach ($server in $servers) { 
   $instances = Get-WmiObject -ComputerName $server win32_service | where {$_.name -like "MSSQL*"}

   if (!$?) {
      Echo "$server - No SQL instance found" >> $logerrs
      Echo "$server - No SQL instance found"
   }
   Else {
      ForEach ($instance in $instances) {
         if (($instance.name -eq "MSSQLSERVER") -or ($instance.name -like "MSSQL$*")) {
            Echo "$server\$instance.name" >> $logfile
            Echo "$server, $($instance.name)"
         }
      }  
   }
}



Best Regards, Arun http://whynotsql.blogspot.com/


Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>