Harry's profileBlue Blaze Irregular Har...PhotosBlogListsMore Tools Help
    February 08

    Ignore THis Post

    protected override void ProcessRecord()
    {
        //Make sure both -Name and -Default aren't specified
        if (!string.IsNullOrEmpty(_Name) && _Default.IsPresent)
        {
            WriteError(new ErrorRecord(
                new ArgumentException(
                    "Default and Name parameters can't both be specified"),
                "DefaultAndName",
                ErrorCategory.InvalidArgument,
                null));
    
            return;
        }
    
        //If the machine name is not specified, assume the local machine 
        //(via the "." value)
        string machine = string.IsNullOrEmpty(_MachineName) ? "." : _MachineName;
    
        //Connect to the specified machine via the SMO WMI ManagedComputer object
        SmoWmi.ManagedComputer mc = new SmoWmi.ManagedComputer(machine);
    
        if (string.IsNullOrEmpty(_Name) && !_Default.IsPresent)
        {
            //If neither Name or Default are specified, write all the 
            //server instances on specified machine
            foreach (SmoWmi.ServerInstance si in mc.ServerInstances)
                WriteServerObject(si);
    
            return;
        }
    
        string instanceName = _Default.IsPresent ? "MSSQLSERVER" : _Name;
    
        if (mc.ServerInstances.Contains(instanceName))
            WriteServerObject(mc.ServerInstances[instanceName]);
        else
            WriteDebug("The specified SQL instance does not exist");
    }
    
    //Helper method to create a SMO Server object from a 
    //SMO WMI ServerInstance object and write it to the pipeline
    private void WriteServerObject(SmoWmi.ServerInstance si)
    {
        if (si.Name == "MSSQLSERVER")
            WriteObject(new Smo.Server(si.Parent.Name));
        else
            WriteObject(new Smo.Server(si.Parent.Name + "\\" + si.Name));
    }
    

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://devhawk.spaces.live.com/blog/cns!D9BC809858885A4!852.trak
    Weblogs that reference this entry
    • None