I have written a Windows service to collect information from all of our SQL servers. The service gets installed onto each server, and utilizing WMI and SMO, inserts relevant system information back to a central database. In order to get the SQL information, I use the following c# code:
List<Server> sqlServers = new List<Server>(); //List of Smo.Server objects
string registrySubKey = #"SOFTWARE\Microsoft\Microsoft SQL Server";
string registryValue = "InstalledInstances";
try
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(registrySubKey);
string[] instances = (string[])rk.GetValue(registryValue);
if (instances.Length > 0)
{
foreach (string element in instances)
{
Server s;
string serverInstance;
if (element == "MSSQLSERVER") //If default instance
{
serverInstance = System.Environment.MachineName;
}
else
{
serverInstance = System.Environment.MachineName + #"\" + element;
}
s = new Server(serverInstance);
if (s != null)
{
sqlServers.Add(s);
}
}
}
}
The only problem I'm having is on our SQL clusters.
For those of you unfamiliar with active/passive sql clustering, you cannot connect directly to one of the nodes. Instead the sql cluster gets a virtual name and another ip address that clients would then connect to.
The current code I have will try to connect to NodeName\instanceName which obviously will not work on the cluster. Instead I need to programmatically find the SQL cluster virtual name that this node belongs to and connect to that instead.
I thought I might be able to get this information from the MSCluster_Cluster WMI class, but that will only get me the cluster virtual name, not the SQL cluster virtual name.
With this code:
using System.Management;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\MSCluster", "SELECT * FROM MSCluster_Resource");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("Name: {0}", queryObj["Name"]);
}
I can see my SQL Cluster Name (2008CLUSTERSQL) as two of the outputs:
Name: SQL IP Address i (2008CLUSTERSQL)
Name: SQL Network Name (2008CLUSTERSQL)
Will this help? I guess you can extract the name of out it?
I got this code from WMI Code Creator (http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en), a very useful tool to browse different WMI namespaces/classes/properties.
Could you use a WMI query with the mscluster WMI classes?
http://technet.microsoft.com/en-us/library/cc780572(WS.10).aspx
http://blogs.msdn.com/clustering/archive/2008/01/08/7024031.aspx
By query I mean interrogate all the cluster's resources/groups to locate the SQL Server network name.
Related
Please Help me.
How to Get Server name and instance name in c# and show in combobox.
If you are interested in Sql Server you can use something like this:
using System.Data;
using System.Data.Sql;
var instances = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow instance in instances.AsEnumerable())
{
Console.WriteLine($"ServerName: {instance["ServerName"]}; "+
" Instance: {instance["InstanceName"]}");
}
More information about the SqlDataSourceEnumerator class you can find on MSDN.
Note:
This class will look into the local network for servers, if your network is large then there might be a delay in acquiring the response. Also for the empty string instance name, that should be the default instance for that SQL Server.
You can get this information using the SMO too, if you want.
Similar to the accepted answer but for those who have a SqlClient.SqlConnection already opened, you can retrieve the instance name from
Dim c As New SqlClient.SqlConnection(sConnectionString)
' Get the database name and server
SourceDatabase = c.Database
SourceServer = c.DataSource
Sorry for the VB ;)
Get Server name and instance name Add in combobox. This will take less time try it
string ServerName = Environment.MachineName;
Microsoft.Win32.RegistryView registryView = Environment.Is64BitOperatingSystem ? Microsoft.Win32.RegistryView.Registry64 : Microsoft.Win32.RegistryView.Registry32;
using (Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, registryView))
{
Microsoft.Win32.RegistryKey instanceKey = hklm.OpenSubKey(#"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
if (instanceKey != null)
{
foreach (var instanceName in instanceKey.GetValueNames())
{
if (instanceName == "MSSQLSERVER")
{
cmbServerName.Items.Add(ServerName);
}
else
{
cmbServerName.Items.Add(ServerName + "\\" + instanceName);
}
}
}
}
I want to find out if the remote host has r/w access to a network share. To start out I wanted to see if I could query the target host's ability to query the UNC path for info, ala
var query = string.Format("select * from CIM_Directory where name = '{0}'", path);
This works fine for local files, e.g.
var path = #"c:\\Windows";
However, I can't figure out an appropriate way of querying a UNC path (e.g. \\foo\bar). The query always returns a blank set. I saw a related question about executing remote files and the solution for that one ended up being PsExec. I was hoping to ideally solve this problem entirely using WMI without having to rely on 3rd party execs, or uploading my own tool to the remote host.
Cheers
Here's a little usage sample of what I am trying to do right now (var values taken out):
using System;
using System.Linq;
using System.Management;
namespace netie
{
class Program
{
static void Main()
{
var connection = new ConnectionOptions
{
Username = "user",
Password = "pass",
Authority = "domain",
Impersonation = ImpersonationLevel.Impersonate,
EnablePrivileges = true
};
var scope = new ManagementScope("\\\\remote\\root\\CIMV2", connection);
scope.Connect();
var path = #"\\\\foo\\bar\\";
var queryString = string.Format("select * from CIM_Directory where name = '{0}'", path);
try
{
var query = new ObjectQuery(queryString);
var searcher = new ManagementObjectSearcher(scope, query);
foreach (var queryObj in searcher.Get().Cast<ManagementObject>())
{
Console.WriteLine("Number of properties: {0}", queryObj.Properties.Count);
foreach (var prop in queryObj.Properties)
{
Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
}
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
}
So it looks like this is basically impossible as WMI locks you out of network access for security reasons. Looks like your best bet is WinRM or PsExec for one-offs. You can potentially enable WinRM through WMI if that's your only path of access, but I imagine that ability can be blocked by group policies. The third option is to write your own Windows Service that will respond to requests and installing that through WMI if you have the access.
In short: the answer to my question is a No. Use WinRm, PsExec, or a custom win-service solution.
I know this is an old question, but for anyone looking to do this, the following code works. (I know that it's not WMI. Given the OP's answer I didn't even try it with WMI, but I shudder to think that people may write a service for something like this.)
if (System.IO.Directory.Exists(#"[SOME UNC PATH]"))
{
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(#"[SOME UNC PATH]");
var securityInfo = info.GetAccessControl();
var rules = securityInfo.GetAccessRules(
true,
true,
typeof(System.Security.Principal.SecurityIdentifier));
foreach (var rule in rules)
{
var fileSystemRule = rule as System.Security.AccessControl.FileSystemAccessRule;
if (ruleastype != null)
{
string user = fileSystemRule.IdentityReference.Translate(
typeof(System.Security.Principal.NTAccount)).Value;
System.Diagnostics.Debug.Print("{0} User: {1} Permissions: {2}",
fileSystemRule.AccessControlType.ToString(),
user,
fileSystemRule.FileSystemRights.ToString());
}
}
}
When run it produces the following output:
Allow User: Everyone Permissions: ReadAndExecute, Synchronize
Allow User: CREATOR OWNER Permissions: FullControl
Allow User: NT AUTHORITY\SYSTEM Permissions: FullControl
Allow User: BUILTIN\Administrators Permissions: FullControl
Allow User: BUILTIN\Users Permissions: ReadAndExecute, Synchronize
From code I want to force a Windows machine to use a specific network adapter for all connections to a specific IP address.
I plan to do so by using the ROUTE ADD command line tool, but this requires that I know in advance the network adapters' index number (as it must be given to the ROUTE ADD command).
QUESTION: How can I programmatically retrieve a network adapter's index, given I know its name?
I'm aware that ROUTE PRINT shows me the information I need (the index numbers of all network adapters present), but there must be a way to get that information programmatically too (C#)?
Note, that I don't like parsing the text output from ROUTE PRINT, as the text format may change with different Windows versions.
You can obtain the interface index of your network adapter
by using the .Net NetworkInterface (and related) classes.
Here is a code example:
static void PrintInterfaceIndex(string adapterName)
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine("IPv4 interface information for {0}.{1}",
properties.HostName, properties.DomainName);
foreach (NetworkInterface adapter in nics)
{
if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)
{
continue;
}
if (!adapter.Description.Equals(adapterName, StringComparison.OrdinalIgnoreCase))
{
continue;
}
Console.WriteLine(adapter.Description);
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
if (p == null)
{
Console.WriteLine("No information is available for this interface.");
continue;
}
Console.WriteLine(" Index : {0}", p.Index);
}
}
Then just call this function with the name of your network adapter:
PrintInterfaceIndex("your network adapter name");
You can also obtain the InterfaceIndex of your network adapter
by using the Win32_NetworkAdapter WMI class. The Win32_NetworkAdapter class
contains a property called InterfaceIndex.
So, to retrieve the InterfaceIndex for a network adapter with a given
name, use the following code:
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Description='<Your Network Adapter name goes here>'");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
using (ManagementObjectCollection queryCollection = searcher.Get())
{
foreach (ManagementObject mo in queryCollection)
{
Console.WriteLine("InterfaceIndex : {0}, name {1}", mo["InterfaceIndex"], mo["Description"]);
}
}
}
If you do not want to use WMI you could also use the Win32 API function
GetAdaptersInfo in combination with the IP_ADAPTER_INFO struct.
You will find an example here pinvoke.net.
have you looked into using C#'s system.net.networkinformation interface?
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx
I'm not familiar with ROUTE ADD, but you can theoretically marry up information from one with the other.
Given a service name, I would like to retrieve the username that it runs under (i.e. the username shown in the 'Log On' tab of a service's properties window).
There doesn't appear to be anything in the ServiceController class to retrieve this basic information. Nothing else in System.ServiceProcess looks like it exposes this information either.
Is there a managed solution to this, or am I going to have to drop down into something lower-level?
Using WMI, with the System.Management you can try the following code:
using System;
namespace WindowsServiceTest
{
class Program
{
static void Main(string[] args)
{
System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select name, startname from Win32_Service")); // where name = '{0}'", "MCShield.exe"));
using (System.Management.ManagementObjectSearcher mgmtSearcher = new System.Management.ManagementObjectSearcher(sQuery))
{
foreach (System.Management.ManagementObject service in mgmtSearcher.Get())
{
string servicelogondetails =
string.Format("Name: {0} , Logon : {1} ", service["Name"].ToString(), service["startname"]).ToString();
Console.WriteLine(servicelogondetails);
}
}
Console.ReadLine();
}
}
}
You can then later substitute the commented code with your service name, and it should only return the instances of your service process that is running.
WMI is your friend. Look at Win32_Service, specifically the StartName property. You can access WMI from C# via the System.Management.ManagementClass.
If you've not used WMI before, this article seems to be quite a good tutorial.
You can find this using the Windows Registry, reading the following string value, replacing [SERVICE_NAME] with the name of the Windows Service:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[SERVICE_NAME]\ObjectName
Try this:
System.Security.Principal.WindowsIdentity.GetCurrent();
but the most obvious you will get LOCAL SYSTEM or NETWORK. The reason that you cannot show this user - that service can manage multiple users (shared by desktop, attached to current windows session, using shared resource ...)
System starts service, but any user can use it.
This solution works fine for me:
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + this.ServiceName + "'");
wmiService.Get();
string user = wmiService["startname"].ToString();
public String getUsername() {
string username = null;
try {
ManagementScope ms = new ManagementScope("\\\\.\\root\\cimv2");
ms.Connect();
ObjectQuery query = new ObjectQuery
("SELECT * FROM Win32_ComputerSystem");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(ms, query);
foreach (ManagementObject mo in searcher.Get()) {
username = mo["UserName"].ToString();
}
string[] usernameParts = username.Split('\\');
username = usernameParts[usernameParts.Length - 1];
} catch (Exception) {
username = "SYSTEM";
}
return username;
}
I want to programatically enable TCP connections on SQL Server. I believe we can achieve this by modifying registry entries and restarting SQL Server service. What registry should I edit?
Unless you have a good reason for modifying the registry directly, I suggest you consider using WMI. WMI will provide you with a more version agnostic implementation. WMI can be accessed through the System.Management namespace. You could have code that looks something like this.
public void EnableSqlServerTcp(string serverName, string instanceName)
{
ManagementScope scope =
new ManagementScope(#"\\" + serverName +
#"\root\Microsoft\SqlServer\ComputerManagement");
ManagementClass sqlService =
new ManagementClass(scope,
new ManagementPath("SqlService"), null);
ManagementClass serverProtocol =
new ManagementClass(scope,
new ManagementPath("ServerNetworkProtocol"), null);
sqlService.Get();
serverProtocol.Get();
foreach (ManagementObject prot in serverProtocol.GetInstances())
{
prot.Get();
if ((string)prot.GetPropertyValue("ProtocolName") == "Tcp" &&
(string)prot.GetPropertyValue("InstanceName") == instanceName)
{
prot.InvokeMethod("SetEnable", null);
}
}
uint sqlServerService = 1;
uint sqlServiceStopped = 1;
foreach (ManagementObject instance in sqlService.GetInstances())
{
if ((uint)instance.GetPropertyValue("SqlServiceType") == sqlServerService &&
(string)instance.GetPropertyValue("ServiceName") == instanceName)
{
instance.Get();
if ((uint)instance.GetPropertyValue("State") != sqlServiceStopped)
{
instance.InvokeMethod("StopService", null);
}
instance.InvokeMethod("StartService", null);
}
}
}
This code assumes a project reference to System.Management.dll and the following using statement:
using System.Management;
The Sql Protocols blog has an article that goes into some detail as to what the above code is doing.
Note: If a firewall is blocking the port(s) you will still be unable to access the server via TCP.
Take a look at HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp hive. There are keys like Enabled, ListenOnAllIPs and a list of IP addresses to listen on.