<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">

<channel>
	<title>Sev17</title>
	
	<link>http://sev17.com</link>
	<description>SQL Server, PowerShell and so on</description>
	<lastBuildDate>Sat, 04 Feb 2012 16:24:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Sev17" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="sev17" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Sarasota IT Pro Camp</title>
		<link>http://sev17.com/2012/02/sarasota-it-pro-camp/</link>
		<comments>http://sev17.com/2012/02/sarasota-it-pro-camp/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 16:24:10 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10811</guid>
		<description><![CDATA[Join system administrators IT professionals and database professionals in addition to managers at all levels who work with Microsoft technologies for the Sarasota IT Pro Camp on Saturday, February 18th, 2012. IT Pro Camps are focused on serving the needs of  IT Pro&#8217;s (Windows system administrators). IT Pro&#8217;s haven&#8217;t had many events like our developer counterpart&#8217;s...]]></description>
			<content:encoded><![CDATA[<p>Join system administrators IT professionals and database professionals in addition to managers at all levels who work with Microsoft technologies for the <a href="http://itprocamp.com/sarasota" target="_blank">Sarasota IT Pro Camp</a> on <strong>Saturday, February 18th, 2012</strong>.</p>
<p><a href="http://itprocamp.com/" target="_blank">IT Pro Camps</a> are focused on serving the needs of  IT Pro&#8217;s (Windows system administrators). IT Pro&#8217;s haven&#8217;t had many events like our developer counterpart&#8217;s code camps and this is why I&#8217;m excited to be a part of IT Pro Camp. There&#8217;s a definite need for events which serve the IT Pro community. We&#8217;ve done Tampa, South Florida, Orlando and in two weeks we&#8217;ll add Sarasota to our growing list of cities. We&#8217;re also planning on Jacksonville, Pensacola and for the second year we&#8217;ll be returning to Tampa and South Florida. We hope to have dates for the remainder of 2012 finalized by the end of March. I&#8217;ll be sure to post an update once we work out scheduling and venues.</p>
<p>The <a href="http://itprocamp.com/sarasota" target="_blank">Sarasota IT Pro Camp</a> will feature 24 one-hour sessions on topics covering Powershell, BI, SQL Server, Cloud, Active Directory, System Center and Data Security. I&#8217;ll be presenting on <a href="http://itprocamp.com/sarasota/sessions/?session=mp5gmb" target="_blank">Storing Powershell Output</a>. Although there are many good sessions to choose from, I&#8217;d like to highlight a few sessions which peaked my interest:</p>
<p>We&#8221;ll have three sessions, on a topic we haven&#8217;t had at previous camps, Data Security.</p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=1z7lg3">Adam Malone &#8211; Cyber Crime and the FBI</a></p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=rglpy3">Joseph Schorr &#8211; Rule 1: Cardio</a></p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=gky8ff">Jeff Wolach - Introduction to Next Generation Firewalls</a></p>
<p>I think this this is one of the interesting things about having IT Pro Camps in different cities, there tends to be a strong technical community around particular disciplines and for Sarasota I&#8217;d say its Data Security.</p>
<p>Jose Chinchilla  (<a href="http://www.sqljoe.com" target="_blank">blog</a>|<a href="http://twitter.com/SQLJoe" target="_blank">twitter</a>) is doing a two-part session on Business Intelligence.</p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=7c3ddk">Jose Chinchilla - Introduction to Microsoft Business Intelligence</a></p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=mrrbry">Jose Chinchilla - Taking Business Intelligence to the next level with SharePoint 2010!</a></p>
<p>This is a good opportunity for attendees to get both an introduction and more advanced overview on business intelligence. Two-part series are kind of unique to community events and you tend not to see a two-part series at paid conferences.</p>
<p>Be sure to <a href="http://itprocamp.com/sarasota/register-now/" target="_blank">register</a> to attend the free <a href="http://itprocamp.com/sarasota" target="_blank">Sarasota IT Pro Camp</a> event. A continental breakfast and lunch will be provided. Please tell your colleagues about <a href="http://itprocamp.com/" target="_blank">IT Pro Camps</a>. I look forward to seeing you there!</p>
<p>&#8211;Chad Miller</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/02/sarasota-it-pro-camp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Orlando IT Pro Camp 2012 Presentation</title>
		<link>http://sev17.com/2012/01/orlando-it-pro-camp-2012-presentation/</link>
		<comments>http://sev17.com/2012/01/orlando-it-pro-camp-2012-presentation/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 02:29:00 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Presentations]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10806</guid>
		<description><![CDATA[Presentation and scripts from my Storing Powershell Output session at Orlando IT Pro Camp 2012.]]></description>
			<content:encoded><![CDATA[<p><a title="Presentation and scripts" href="https://skydrive.live.com/redir.aspx?cid=ea42395138308430&amp;resid=EA42395138308430!1011&amp;parid=EA42395138308430!113" target="_blank">Presentation and scripts</a> from my <a title="Storing Powershell Output" href="http://itprocamp.com/orlando/sessions/?session=yyxxk4" target="_blank">Storing Powershell Output</a> session at Orlando IT Pro Camp 2012.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/01/orlando-it-pro-camp-2012-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Orlando IT Pro Camp</title>
		<link>http://sev17.com/2012/01/orlando-it-pro-camp/</link>
		<comments>http://sev17.com/2012/01/orlando-it-pro-camp/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 03:22:23 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[IT Pro Camp]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10799</guid>
		<description><![CDATA[The Orlando IT Pro Camp marks our third event after Tampa and South Florida. As with the prior IT Pro Camps I’m helping put together the Orlando IT Pro Camp. Register to attend this free event on Saturday, January 21st. Orlando will be even bigger than Tampa or South Florida with 30 sessions on topics...]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://itprocamp.com/orlando/" target="_blank">Orlando IT Pro Camp</a> marks our third event after Tampa and South Florida. As with the prior IT Pro Camps I’m helping put together the Orlando IT Pro Camp. <a href="http://orlandoitprocamp.eventbrite.com/" target="_blank">Register to attend</a> this free event on <strong>Saturday, January 21st</strong>. Orlando will be even bigger than Tampa or South Florida with 30 sessions on topics covering Hyper-v, Private Cloud, SharePoint, System Center Products, SQL Server, PowerShell, Active Directory, Windows 2008 R2, and Windows 7 . I’ll be presenting a PowerShell session on <a href="http://itprocamp.com/orlando/sessions/?session=yyxxk4" target="_blank">Storing Powershell Output</a>.  A few sessions I’ve seen previously and highly recommend:</p>
<ul>
<li><a href="http://itprocamp.com/orlando/sessions/?session=prbvcd" target="_blank">Intro to SQL Server for IT Professionals</a> by Michael Wells (<a href="http://sqltechmike.com/" target="_blank">Blog</a>|<a href="http://twitter.com/SqlTechMike" target="_blank">Twitter</a>)—An excellent overview of SQL Server for the system admin SQL noob.</li>
<li><a href="http://itprocamp.com/orlando/sessions/?session=oh126k2" target="_blank">Version Control for IT Professionals</a> by Jason Hofferle (<a href="http://www.hofferle.com/" target="_blank">Blog</a>|<a href="http://twitter.com/jhofferle" target="_blank">Twitter</a>) – A good overview of using Mercurial source control for sys admin scripts.</li>
<li><a href="http://itprocamp.com/orlando/sessions/?session=oh126k" target="_blank">Introduction to PowerShell Remoting</a> also by Jason Hofferle (<a href="http://www.hofferle.com/" target="_blank">Blog</a>|<a href="http://twitter.com/jhofferle" target="_blank">Twitter</a>)  —An eye opening look at the benefits of using Powershell remoting in a large enterprise based on real-world benchmarks.</li>
</ul>
<p>If you can’t make Orlando, the Sarasota IT Pro Camp will be held on Saturday, February 18th. 2012. See the <a href="http://itprocamp.com/" target="_blank">IT Pro Camp site</a> for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/01/orlando-it-pro-camp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Importing CSV Files to SQL Server with PowerShell</title>
		<link>http://sev17.com/2011/11/importing-csv-files-to-sql-server-with-powershell/</link>
		<comments>http://sev17.com/2011/11/importing-csv-files-to-sql-server-with-powershell/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 13:02:51 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10792</guid>
		<description><![CDATA[Ed Wilson (Blog&#124;Twitter) aka Scripting Guy is kicking off another guest blogger week  (Nov 28th 2011) with my guest blog post, Four Easy Ways to Import CSV Files to SQL Server with PowerShell. The post demonstrates the following approaches to importing CSVs into a SQL Server table: T-SQL BULK INSERT command LogParser command-line LogParser COM-based scripting...]]></description>
			<content:encoded><![CDATA[<p>Ed Wilson (<a href="http://technet.microsoft.com/en-us/scriptcenter/default.aspx">Blog</a>|<a href="http://twitter.com/scriptingguys/">Twitter</a>) aka Scripting Guy is kicking off another guest blogger week  (Nov 28th 2011) with my guest blog post, <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/28/four-easy-ways-to-import-csv-files-to-sql-server-with-powershell.aspx" target="_blank">Four Easy Ways to Import CSV Files to SQL Server with PowerShell</a>. The post demonstrates the following approaches to importing CSVs into a SQL Server table:</p>
<ul>
<li>T-SQL BULK INSERT command</li>
<li>LogParser command-line</li>
<li>LogParser COM-based scripting</li>
<li>A Windows Powershell-based approach using several functions</li>
</ul>
<div>Most of the time I&#8217;ll I use BULK-INSERT or the Windows Powershell-based approach, although as explained in the post the ability of LogParser to automatically create a SQL table based on a CSV is pretty handy.</div>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/11/importing-csv-files-to-sql-server-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Some SQL Server Security Housekeeping</title>
		<link>http://sev17.com/2011/11/some-sql-server-security-housekeeping/</link>
		<comments>http://sev17.com/2011/11/some-sql-server-security-housekeeping/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 12:49:45 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[CMS]]></category>
		<category><![CDATA[LinkedServer]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10768</guid>
		<description><![CDATA[Managing SQL Server security changes in mass is something which screams automate it. Let&#8217;s look a at few examples using either T-SQL, a Centeral Management Server and Powershell. Task #1 Remove a SQL Server Login from the Sysadmin Role Let&#8217;s say you&#8217;re given the task to remove a login from the sysadmin role on 50...]]></description>
			<content:encoded><![CDATA[<p>Managing SQL Server security changes in mass is something which screams automate it. Let&#8217;s look a at few examples using either T-SQL, a <a href="http://msdn.microsoft.com/en-us/library/bb895144.aspx" target="_blank">Centeral Management Server</a> and Powershell.</p>
<h2>Task #1 Remove a SQL Server Login from the Sysadmin Role</h2>
<p>Let&#8217;s say you&#8217;re given the task to remove a login from the sysadmin role on 50 servers.  For this task we can use the built-in system stored procedures <a href="http://msdn.microsoft.com/en-us/library/ms186270.aspx" target="_blank">sp_dropsrvrolemember</a>  and <a href="http://msdn.microsoft.com/en-us/library/ms188772.aspx" target="_blank">sp_helpsrvrolemember</a>.</p>
<p>Since we want to do this across multiple servers, we&#8217;ll use a multiserver query from the registered central management:</p>
<p><a href="http://sev17.com/wp-content/uploads/multiquery.jpg"><img class="alignnone size-medium wp-image-10771" title="multiquery" src="http://sev17.com/wp-content/uploads/multiquery-279x300.jpg" alt="" width="279" height="300" /></a></p>
<p>1. First create a before removal &#8220;backup&#8221; using sp_helpsrvrolemember:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #AF0000;">sp_helpsrvrolemember</span> <span style="color: #FF0000;">'sysadmin'</span></pre></div></div>

<p>2. Save the output<br />
3. Next run</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">sp_dropsrvrolemember</span> <span style="color: #FF0000;">'CONTOSO\SQL_SecurityAdmin'</span>, <span style="color: #FF0000;">'sysadmin'</span></pre></div></div>

<p>Error handling is generally good thing, but there are cases were can do a task without much error handling especially if the task is interactive and you have backups. Although you can wrap some T-SQL code to check if the login exists on the server or if the login is member of the server before attempting to removing from the sysadmin, its not necessary any errors can safely be ignored. A Central Management Server is great at one off commands with good T-SQL coverage</p>
<h2>Task #2 Remove Linked Server Login Mappings</h2>
<p>After removing the login from the sysadmin you discover they are also mapped to sa on over 100  Linked Servers. Since you can&#8217;t  remove the login from the server because they still need non-administration access, your task to to remove the linked server login mapping. Changing linked server security  in mass is something where Powershell provides a good solution. You&#8217;ll probably want to backup the linked server by scripting out the linked server before any changes which is difficult to do in T-SQL, but easy with Powershell. Also removing a login mappings from all linked servers  is very procedural which is awkward in T-SQL. So here&#8217;s a Powershell solution I created in the form of a few Powershell filters and functions called <a href="http://poshcode.org/3048" target="_blank">LibraryLinkedServer</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">. .\LibraryLinkedServer.ps1
<span style="color: #800080;">$logins</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span>
<span style="color: #800000;">'Contoso\Bill'</span>
<span style="color: #800000;">'Contoso\John'</span>
<span style="color: #800000;">'Contoso\Jill'</span>
<span style="color: #000000;">&#41;</span>
Get<span style="color: pink;">-</span>CMRegisteredServer <span style="color: #800000;">&quot;Z001\SQL1&quot;</span> <span style="color: #800000;">&quot;PRD&quot;</span> <span style="color: pink;">|</span> Backup<span style="color: pink;">-</span>LinkedServer <span style="color: pink;">-</span>LinkedServerLogins <span style="color: #800080;">$logins</span>
Get<span style="color: pink;">-</span>CMRegisteredServer <span style="color: #800000;">&quot;Z00\SQL1&quot;</span> <span style="color: #800000;">&quot;PRD&quot;</span> <span style="color: pink;">|</span> Remove<span style="color: pink;">-</span>LinkedServerLogin <span style="color: pink;">-</span>LinkedServerLogins $logins</pre></div></div>

<p>I would suggest running the code from Powershell ISE so you can step through the code by highlighting and running each line.</p>
<p><a href="http://sev17.com/wp-content/uploads/powershell_ise_ls.jpg"><img class="alignnone size-medium wp-image-10785" title="powershell_ise_ls" src="http://sev17.com/wp-content/uploads/powershell_ise_ls-300x105.jpg" alt="" width="300" height="105" /></a></p>
<p>The solution first sources the LibraryLinkedServer functions and filters i.e. loads the functions into our current Powershell session. Next we define our list of logins t removing from linked server mappings in an array called $logins. The final two steps involve obtaining a list of SQL Servers from the Central Managment Server Z001\SQL1 and the server group PRD. Next we&#8217;ll script out the linked servers using the Backup-LinkedServer function. At this stage I would manually verify the backup files before proceeding with removing the login mappings using Remove-LinkedServerLogin.</p>
<h2>Task #3 Remove a Linked Server</h2>
<p>You discover linked servers in development which point to production. The linked server should be removed entirely. Your task is to remove a specific linked server named PROD1  from all development servers.  Here again we&#8217;ll use the <a href="http://poshcode.org/3048" target="_blank">LibraryLinkedServer</a> functions:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">. .\LibraryLinkedServer.ps1
Get<span style="color: pink;">-</span>CMRegisteredServer <span style="color: #800000;">&quot;Z001\SQL1&quot;</span> <span style="color: #800000;">&quot;DEV&quot;</span> <span style="color: pink;">|</span> Backup<span style="color: pink;">-</span>LinkedServer <span style="color: pink;">-</span>LinkedServer <span style="color: #800000;">&quot;PROD1&quot;</span>
Get<span style="color: pink;">-</span>CMRegisteredServer <span style="color: #800000;">&quot;Z001\SQL1&quot;</span> <span style="color: #800000;">&quot;DEV&quot;</span> <span style="color: pink;">|</span> Remove<span style="color: pink;">-</span>LinkedServer <span style="color: pink;">-</span>LinkedServer <span style="color: #800000;">&quot;PROD1&quot;</span></pre></div></div>

<p>This task is very similar to task #2 and much of the code is the same, except here we&#8217;re specifying a linked server name rather than an array of logins. Again I would suggest manually verifying the backup files before proceeding with removing the linked server.</p>
<p><em>Note: If you use this solution make sure you test, make backups and verify.</em></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/11/some-sql-server-security-housekeeping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Delegated SQL Server Administration with Powershell</title>
		<link>http://sev17.com/2011/11/delegated-sql-server-administration-with-powershell/</link>
		<comments>http://sev17.com/2011/11/delegated-sql-server-administration-with-powershell/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 00:16:48 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[PowerGUI]]></category>
		<category><![CDATA[SqlProxy]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10757</guid>
		<description><![CDATA[Providing delegated administration to groups that need to perform various security functions has always been a difficult task, but thanks to Powershell V3 (currently in CTP 1 as of this blog post) and PowerGUI we have new tools to provide a solution. The Problem You have groups outside of database administration that need to have...]]></description>
			<content:encoded><![CDATA[<p>Providing delegated administration to groups that need to perform various security functions has always been a difficult task, but thanks to Powershell V3 (currently in CTP 1 as of this blog post) and PowerGUI we have new tools to provide a solution.</p>
<h2>The Problem</h2>
<p>You have groups outside of database administration that need to have the ability to manage security of SQL Server logins, users, and roles. Using the out-of-the-box SQL Server roles or permissions doesn’t quite handle all of these use cases and you want to avoid&#160; putting these groups into the sysadmin server role on SQL Server for obvious reasons. One thing you may consider is placing these groups into the&#160; securityadmin server role, but the problem with the securityadmin role is the lack of certain rights. The securityadmin role can only add logins to the instance and not users to databases or in turn users to database roles.&#160; You could add your delegated security&#160; group to every database as db_securityadmin, but this would be difficult to maintain in a highly dynamic environment with thousands of databases. There’s also a larger problem of auditing the actions your security administrators perform. Ideally you want to show every security change is related to specific documented change order.&#160; </p>
<p>You could use the various server and database level permissions over the fixed role approach and in fact that is what Books Online <a href="http://msdn.microsoft.com/en-us/library/ms175892(v=SQL.105).aspx" target="_blank">recommends</a>, however you’ll have the same issues of per database permissions and auditable actions to overcome. If this problem statement sounds unfamiliar to you then you probably haven’t had deal with segregation of duties and reducing administration access that has become prevalent in our post-SOX IT world.</p>
<h2>A Solution</h2>
<p>Looking at web-based applications which run under a service account , you’ll notice&#160; normal users and administrators users as part of almost any applications. Neither group has direct access to the database systems they touch instead a service account is used. A simple idea which can easily be applied to administration functions all we need to do is create a web-based application, assign a service account the necessary rights to perform administration tasks and restrict access to the web-based application. What’s that? You’re not web developer? Well, neither am I. Here’s where Powershell V3 and PowerGUI can help.</p>
<h3>Powershell V3 Delegated Administration</h3>
<p>One of the simple, but really useful changes made to the Powershell V3 is the ability to delegate administration by setting up&#160; runas credentials for a remoting configuration. In the CTP 1 download there’s an example script called runas.ps1 located under <strong>Samples\WindowsPowerShell\DelegatedAdmin</strong> which demonstrates the functionality. This presents an interesting an idea, instead of having your security users connect to each SQL Server they could connect to a single “Proxy” server. The proxy server would then connect to various SQL Servers on their behalf.&#160; Of course the account used for the delegated administration will need to have the necessary rights perhaps even sysadmin access. As an added bonus by using Powershell remoting the security administrator’s machine doesn’t need SQL Server tools or SMO installed. The only thing they needed is Powershell.</p>
<p>If you go this route you’ll want to create a distinct Session Configuration (endpoint) and ACL the configuration to the appropriate groups. You’ll l also need to create the various Powershell functions for the administration tasks on the remote server, here’s where I created a module called SqlProxy.</p>
<h3>SQLProxy Module</h3>
<p>SQL Server lacks Powershell coverage for security administration, so I created <a href="http://poshcode.org/3040" target="_blank">SqlProxy module</a> which provides various functions for managing SQL Server logins, users and roles. In addition, because auditability is a key requirement I’ve added logging to a custom Windows Eventlog called “SqlProxy” for every function which change security settings. The module can be used in Powershell V2 or V3 with or without remoting. When used within a delegated administration setting the module will be loaded into the remote session and anyone granted access to the remote endpoint will be able to execute the SqlProxy functions. We could say our problem of providing delegated administration is solved, however unless your security admins are very adapt at Powershell you’ll probably want to provide GUI.</p>
<h3>PowerGUI SQLProxy PowerPack</h3>
<p>Because PowerGUI provides an easy way to create MMC-style UI’s over Powershell, I applied PowerGUI to the SQLProxy module and created the <a href="http://www.powergui.org/entry.jspa?externalID=3742&amp;categoryID=54" target="_blank">SqlProxy PowerPack</a>. The security administrators can now use GUI-based tool without having to know Powershell. The complete solution does requires some additional setup and configuration as documented below.</p>
<h2>Putting it Together</h2>
<p><a href="http://sev17.com/wp-content/uploads/sqlproxydiag.png"><img style="display: inline" title="sqlproxydiag" alt="sqlproxydiag" src="http://sev17.com/wp-content/uploads/sqlproxydiag_thumb.png" width="638" height="66" /></a></p>
<p><em>Diagram courtesy of </em><a href="http://yuml.me" target="_blank"><em>yuml.me</em></a></p>
<p>A security administer will use PowerGUI with the SqlProxy PowerPack installed to connect to a proxy server via Poewershell remoting. The proxy server has a session configuration with delegated credentials. The proxy server also has a SqlProxy module which is then used to connect to the various SQL Servers through SMO. <em>Note Powershell remoting is not used between the proxy server and SQL Server. The only area where Powershell remoting is used is between the user’s machine and the proxy server.</em></p>
<h3>Setup</h3>
<h4>Setup Proxy Server</h4>
<h5>Requirements:</h5>
<ul>
<li>Windows 7 with Sp1, Windows 2008 R2 with SP1 or Windows 8. </li>
<li>Powershell V3 CTP1 or higher </li>
</ul>
<h5></h5>
<h5></h5>
<h5></h5>
<h5>Installation</h5>
<p>1. Install SMO or SQL Server clients tools (Express edition will work) </p>
<p>2, Copy the SqlProxy.psm1 module from <a href="http://poshcode.org/3040" target="_blank">http://poshcode.org/3040</a> to C:\Windows\system32\WindowsPowerShell\v1.0\Modules\SqlProxy folder</p>
<p>3. Create the SqlProxy Eventlog and Source by running following command </p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">New<span style="color: pink;">-</span>EventLog <span style="color: #008080; font-style: italic;">-LogName</span> SqlProxy <span style="color: pink;">-</span>Source SqlProxy</pre></div></div>

<p>4. Create the SqlProxy Session Configuration with delegated credentials</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span>
Register<span style="color: pink;">-</span>PSSessionConfiguration <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800000;">&quot;SqlProxy&quot;</span> <span style="color: pink;">-</span>RunAsCredential $cred</pre></div></div>

<p>5. ACL the SqlProxy session configuration to the appropriate AD groups (by default only administrators on the proxy server will have access to the remote session):</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Set<span style="color: pink;">-</span>PSSessionConfiguration <span style="color: #008080; font-style: italic;">-Name</span> SqlProxy –ShowSecurityDescriptorUI</pre></div></div>

<p>6. <strong>Optional</strong>: As noted on the Powershell team blog <a href="http://blogs.msdn.com/b/powershell/archive/2010/05/03/configuring-wsman-limits.aspx" target="_blank">the default settings&#160; of Powershell remoting are way too low</a>. The low default settings are particularly problematic in a fan-in scenario like SqlProxy. You should make Powershell remoting more robust by changing the default concurrent and memory settings:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">cd</span> WSMan:\localhost\Shell
<span style="color: #008080; font-weight: bold;">Set-Item</span> .\MaxShellsPerUser <span style="color: #804000;">25</span>
<span style="color: #008080; font-weight: bold;">Set-Item</span> .\MaxConcurrentUsers <span style="color: #804000;">25</span>
<span style="color: #008080; font-weight: bold;">Set-Item</span> .\MaxMemoryPerShellMB <span style="color: #804000;">1024</span>
&nbsp;
<span style="color: #008000;">#Do the same for the SqlProxy Session Configuration</span>
&nbsp;
<span style="color: #008080; font-weight: bold;">cd</span> WSMan:\localhost\Plugin\SqlProxy\Quotas
<span style="color: #008080; font-weight: bold;">Set-Item</span> .\MaxShellsPerUser <span style="color: #804000;">25</span>
<span style="color: #008080; font-weight: bold;">Set-Item</span> .\MaxConcurrentUsers <span style="color: #804000;">25</span>
<span style="color: #008080; font-weight: bold;">Set-Item</span> .\MaxMemoryPerShellMB <span style="color: #804000;">1024</span></pre></div></div>

<h4>Setup Client</h4>
<h5>Requirements:</h5>
<ul>
<li>Powershell V2 or higher (that’s right you can remote from V2 client to V3 server!) </li>
<li>PowerGUI version 3.0 or higher </li>
</ul>
<h5>Installation</h5>
<ol>
<li>Download and install the <a href="http://www.powergui.org/entry.jspa?externalID=3742&amp;categoryID=54" target="_blank">SqlProxy PowerPack</a> </li>
<li>Right click &quot;Connect to Server&quot; Node </li>
<li>Select &quot;Shared Scripts&quot; and change $global:SqlProxy variable to your proxy server </li>
<li>Uncomment #-ConfigurationName &quot;sqlproxy&quot; in the Get-ValidSession function of Shared Scripts </li>
<li>Save script and exit script editor </li>
<li><strong>Optional</strong>: Re-export the PowerPack with your customizations for reuse in your environment:
<ol>
<li>Select File, PowerPack Management </li>
<li>Highlight the SQL Security Administration PowerPack </li>
<li>Select edit and change the PowerPack file link to a UNC share in your environment all of your users will have access to. </li>
<li>Export the PowerPack to the UNC share. </li>
<li>Have your colleagues install the customized PowerPack. PowerGUI has nice feature to automatically update PowerPack where you specify a file link whenever you update your customized version all of your users will get update notifications</li>
</ol>
</li>
</ol>
<h3>Using SqlProxy</h3>
<p>The SqlProxy PowerPack was written to duplicate functionality available in SQL Server Management Studio. If you’re familiar with logins, user mappings, database users and roles the interface should look familiar. I’ve posted some <a href="http://www.powergui.org/servlet/KbServlet/download/3742-102-5931/SqlProxyScreenShots.pdf" target="_blank">screen shots</a> as an additional download on the PowerGUI SqlProxy PowerPack site.</p>
<h3>Testing On Single Machine</h3>
<p>The solution described in this blog post will work on a single machine running Powershell V2 with minimal configuration. The only caveat is that the delegated credentials requires v3. In the course of developing this solution I used Windows 7 with a local SQL Server instance and then would test using a 3-tier solution. A minimal setup for testing on a Windows 7 machine looks like this:</p>
<p>Requirements</p>
<ul>
<li>Powershell V2 </li>
<li>Local SQL Server instance </li>
<li>PowerGUI 3.o or higher </li>
</ul>
<p>1. Copy the SqlProxy.psm1 module from <a href="http://poshcode.org/3040" target="_blank">http://poshcode.org/3040</a> to C:\Windows\system32\WindowsPowerShell\v1.0\Modules\SqlProxy folder</p>
<p>2. Create the SqlProxy Eventlog and Source by running following command </p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">New<span style="color: pink;">-</span>EventLog <span style="color: #008080; font-style: italic;">-LogName</span> SqlProxy <span style="color: pink;">-</span>Source SqlProxy</pre></div></div>

<p>3. Enable Powershell remoting. This&#160; enables remoting but also will allow you to remote into the local machine from the local machine for testing purposes:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Enable<span style="color: pink;">-</span>PSRemoting</pre></div></div>

<p>If you’re on a non-domain machine you will need to add your localhost to the trustedhosteds as described <a href="http://blogs.msdn.com/b/wmi/archive/2009/07/24/powershell-remoting-between-two-workgroup-machines.aspx" target="_blank">here</a>.</p>
<h3>Additional Configurations</h3>
<p>If you use this solution you may want to consider additional steps to secure the environment. You can configure a Powershell session configuration to only allow specific commands to be executed. If this is something you’d like to do I encourage you read about constraining a Powershell session. In addition PowerGUI supports a lockdown script to enable administrators to customize PowerGUI configurations for their environment. You read about PowerGUI lockdown features <a href="http://wiki.powergui.org/index.php/Customizing_PowerGUI_Configuration" target="_blank">here</a>.</p>
<h2>Final Thoughts</h2>
<p>I deployed this solution in my environment with around three dozen users. Was it successful? Well, it did allow me to remove various groups from syadmin role. My only gripe is the touch point on each workstation. Because of this you end up dealing with some inconsistencies in configurations. The touch point issue becomes more of a problem as you deal with various operating systems, PowerGUI versions and even antivirus and firewall settings (I found several instances where a client firewall would block Powershell remoting traffic through PowerGUI, but allow it from Powershell). One of the things I’m looking at is moving PowerGUI into a Citrix environment to ensure a single working configuration, but for now I’m happy with the solution. That said, looking at the general problem of delegated administration I wish there was an easier way to create web-based solutions which leverage Powershell.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/11/delegated-sql-server-administration-with-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Gaining SQL Server SysAdmin Access</title>
		<link>http://sev17.com/2011/10/gaining-sql-server-sysadmin-access/</link>
		<comments>http://sev17.com/2011/10/gaining-sql-server-sysadmin-access/#comments</comments>
		<pubDate>Sat, 22 Oct 2011 19:51:18 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[schtasks]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10748</guid>
		<description><![CDATA[I’ve seen this come a few times at work and I’m sure most you have experienced something similar. Someone or an application installs SQL Server, doesn’t grant access to the DBA group and asks for DBA support. In SQL Server 2008 and higher the built-in local administrators group is no longer automatically part of the...]]></description>
			<content:encoded><![CDATA[<p>I’ve seen this come a few times at work and I’m sure most you have experienced something similar.</p>
<blockquote><p>Someone or an application installs SQL Server, doesn’t grant access to the DBA group and asks for DBA support.</p></blockquote>
<p>In SQL Server 2008 and higher the built-in local administrators group is no longer automatically part of the SQL Server sysadmin role. You should add necessary logins to the sysadmin role as part of your SQL Server installation. Not automatically granting local administrators access to SQL Server is generally a good thing, however when the SQL Server installation is done by say another application then we see issues were support groups do not have access to SQL Server even though they are local administrators on the box. In the last few months I’ve seen this scenario several times and so has Argenis Fernandez (<a href="http://sqlblog.com/blogs/argenis_fernandez/default.aspx" target="_blank">blog</a>|<a href="http://twitter.com/#!/DBArgenis" target="_blank">twitter</a>) as he has a helpful blog post entitled&#160; “<a href="http://sqlblog.com/blogs/argenis_fernandez/archive/2011/07/10/think-your-windows-administrators-don-t-have-access-to-sql-server-2008-by-default-think-again.aspx" target="_blank">Think Your Windows Administrators Don’t Have Access to SQL Server 2008 by Default? Think Again</a>.”&#160; The post describes a technique of using the Sysinternals tool <a href="http://technet.microsoft.com/en-us/sysinternals/bb897553" target="_blank">PsExec</a> to gain SQL sysadmin access to SQL Server on which you already have local administrator access. The&#160; post also links to a documented way of starting SQL Server in single user mode in order to gain SQL Server sysadmin access (see “<a href="http://msdn.microsoft.com/en-us/library/dd207004.aspx" target="_blank">Troubleshooting: Connecting to SQL Server When System Administrators Are Locked Out</a>).” And finally the post mentions, but does not demonstrate a method of using the Windows Task Scheduler. If you’re interested in the how’s and why’s this works and how different versions of SQL Servers are well, different in security settings defaults I encourage you to give the post and comments a read.</p>
<p>Armed with information on how to gain SQL Server administration I looked at the various options. The psexec utility is blacklisted in my environment, blocked from download and listed as an untrusted application. The approach of starting SQL Server in single user mode requires taking SQL Server down and since the application is a quasi-production system restarting SQL Server would have to be coordinated or done after hours. So, I chose to to use the Windows Scheduler trick. This should work on Windows 2003/XP and higher and I’ve tested on Windows 2008 and Windows 7. The typical UAC things apply—you’ll need to run as administrator. The script I came up with is listed below. I figured if I’m getting a server were some other group performed the installation or configuration there’s no guarantee PowerShell will be installed, so I’m going old school Windows batch file on this. It feels wrong to post a Windows batch file&#160; instead of a Powershell script on my blog, but I think using a batch file is the best approach for the problem. To use, save the script as AddDBA.bat and see the example syntax. The script must be run locally.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> off
<span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">if</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">1</span>&quot;==&quot;?&quot; <span style="color: #00b100; font-weight: bold;">goto</span> Syntax
<span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">if</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">1</span>&quot;==&quot;&quot;  <span style="color: #00b100; font-weight: bold;">goto</span> Syntax
<span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">if</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">2</span>&quot;==&quot;&quot;  <span style="color: #00b100; font-weight: bold;">goto</span> Syntax
<span style="color: #808080; font-style: italic;">rem **********************************</span>
<span style="color: #808080; font-style: italic;">rem Script AddDBA.bat</span>
<span style="color: #808080; font-style: italic;">rem Creation Date: 10/21/2011</span>
<span style="color: #808080; font-style: italic;">rem Last Modified: 10/21/2011</span>
<span style="color: #808080; font-style: italic;">rem Author: Chad Miller</span>
<span style="color: #808080; font-style: italic;">rem ***********************************</span>
<span style="color: #808080; font-style: italic;">rem Description: Adds a Windows Account to SQL Sysadmin Role</span>
<span style="color: #808080; font-style: italic;">rem Use when you have local Windows admin access but lost SQL Sysadmin access</span>
<span style="color: #808080; font-style: italic;">rem ***********************************</span>
&nbsp;
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> ************************
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> *** ServerInstance: <span style="color: #33cc33;">%</span><span style="color: #448888;">1</span>
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> ************************
<span style="color: #b1b100; font-weight: bold;">set</span> TMPFILE=<span style="color: #33cc33;">%</span><span style="color: #448888;">TMP</span><span style="color: #33cc33;">%</span>\AddDBA-<span style="color: #33cc33;">%</span><span style="color: #448888;">RANDOM</span><span style="color: #33cc33;">%</span>-<span style="color: #33cc33;">%</span><span style="color: #448888;">TIME:~6,5</span><span style="color: #33cc33;">%</span>.tmp
schtasks  /Create /TN AddDBA /SC Once /ST 12:00 ^
/TR &quot;sqlcmd -S <span style="color: #33cc33;">%</span><span style="color: #448888;">1</span> -Q \&quot;CREATE LOGIN [<span style="color: #33cc33;">%</span><span style="color: #448888;">2</span>] FROM WINDOWS; EXEC sp_addsrvrolemember [<span style="color: #33cc33;">%</span><span style="color: #448888;">2</span>],[sysadmin];\&quot; -o \&quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">TMPFILE</span><span style="color: #33cc33;">%</span>\&quot; -e&quot; ^
/RU &quot;NT AUTHORITY\SYSTEM&quot; /F
schtasks /Run /TN AddDBA
schtasks /Query /TN AddDBA /V /FO List
<span style="color: #808080; font-style: italic;">rem Wait 5 seconds</span>
PING 127.0.0.1 -n 6  <span style="color: #33cc33;">&gt;</span><span style="color: #0000ff; font-weight: bold;">NUL</span>
<span style="color: #808080; font-style: italic;">rem Display output file</span>
type <span style="color: #33cc33;">%</span><span style="color: #448888;">TMPFILE</span><span style="color: #33cc33;">%</span>
schtasks /Delete /TN AddDBA /F
<span style="color: #00b100; font-weight: bold;">goto</span> :<span style="color: #b100b1; font-weight: bold;"><span style="color: #00b100; font-weight: bold;">EXIT</span></span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">Syntax</span>
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> Syntax: AddDBA ServerInstance WindowsGroupOrLogin
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> Example: AddDBA Z001\SQL1 Contoso\DBAGroup
<span style="color: #00b100; font-weight: bold;">goto</span> :<span style="color: #b100b1; font-weight: bold;"><span style="color: #00b100; font-weight: bold;">EXIT</span></span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;"><span style="color: #00b100; font-weight: bold;">EXIT</span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/10/gaining-sql-server-sysadmin-access/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>SQL Saturday #85 Orlando 2011 Presentation</title>
		<link>http://sev17.com/2011/09/sql-saturday-85-orlando-2011-presentation/</link>
		<comments>http://sev17.com/2011/09/sql-saturday-85-orlando-2011-presentation/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 19:11:33 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Presentations]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10745</guid>
		<description><![CDATA[Presentation and scripts from my Storing Powershell Output session at SQL Saturday #85 Orlando 2011:]]></description>
			<content:encoded><![CDATA[<p><a href="https://skydrive.live.com/?cid=ea42395138308430&#038;sc=documents&#038;id=EA42395138308430%21268#" title="Presentation and scripts">Presentation and scripts</a> from my <a href="http://sqlsaturday.com/viewsession.aspx?sat=85&#038;sessionid=5407" title="Storing Powershell Output" target="_blank">Storing Powershell Output</a> session at SQL Saturday #85 Orlando 2011:</p>
<p><iframe title ="Preview" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="width:98px;height:115px;padding:0;background-color:#fcfcfc;" src="https://skydrive.live.com/embedicon.aspx/Public/SQLSaturday20090124?cid=ea42395138308430&#038;sc=documents"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/09/sql-saturday-85-orlando-2011-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Managing AlwaysOn with Powershell</title>
		<link>http://sev17.com/2011/09/managing-alwayson-with-powershell/</link>
		<comments>http://sev17.com/2011/09/managing-alwayson-with-powershell/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 22:46:32 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[AlwaysOn]]></category>
		<category><![CDATA[sqlps]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10742</guid>
		<description><![CDATA[Although you can use SQL Server Management Studio or T-SQL to manage AlwaysOn, SQL Server Denali CTP 3 includes 25 cmdlet which together provide complete coverage for creating, confiiguring and administering the AlwaysOn database feature. In this post we’ll look at using Powershell to perform various management tasks for AlwayOn. Note: This blog post describes...]]></description>
			<content:encoded><![CDATA[<p>Although you can use SQL Server Management Studio or T-SQL to manage AlwaysOn, SQL Server Denali CTP 3 includes 25 cmdlet which together provide complete coverage for creating, confiiguring and administering the AlwaysOn database feature. In this post we’ll look at using Powershell to perform various management tasks for AlwayOn.</p>
<p><em><font color="#ff0000">Note: This blog post describes features in SQL Server Denali CTP 3 which may change on final product release.</font></em></p>
<h2>Getting Started</h2>
<p>You’ll need&#160; a simple Windows 2008 R2 cluster with two standalone installs of SQL Server. I say simple because you don’t have to worry about shared storage, quorum disks and shared MSTDTC installations like you would in a traditional SQL Server installation on a Windows Server Failover Cluster. All you need are two servers running Windows Server 2008 R2 Enterprise Edition. For test purposes I’ve setup a two-node Windows Server Failover Cluster as follows:</p>
<p>1. Configure a Private virtual machine network for intra-cluster communication. Note this step is optional and not really necessary for a bare minimum cluster, but I setup it up anyways to mimic close to what I’ll have in production. This network uses a separate IP subnet than the Internal only network I had already setup in Hyper-V. </p>
<p>2. Setup the private only network&#160; which allows communication between virtual machines only.</p>
<ol>
<li>Right-click virtual machine </li>
<li>Select Virtual Network Manager &gt; Select Private &gt; Add </li>
</ol>
<p><a href="http://sev17.com/wp-content/uploads/VirtualNetworkManager.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="VirtualNetworkManager" border="0" alt="VirtualNetworkManager" src="http://sev17.com/wp-content/uploads/VirtualNetworkManager_thumb.png" width="244" height="146" /></a></p>
<p>3. Add the private network to each of the virtual machines. </p>
<ol>
<ol>
<li>Shutdown each machine </li>
<li>Select machine in Hyper-V Manager </li>
<li>Select Settings </li>
<li>Select Add Hardware and choose Network Adapter and click Add </li>
<li>Select the private network you created from the Network drop down list and click OK </li>
</ol>
</ol>
<p><a href="http://sev17.com/wp-content/uploads/AddNetwork.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="AddNetwork" border="0" alt="AddNetwork" src="http://sev17.com/wp-content/uploads/AddNetwork_thumb.png" width="244" height="146" /></a></p>
<p>4. On the virtual machines assign IP addresses under Network and Sharing Center. Here’s a table of my setup:</p>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="133"><strong>Machine</strong></td>
<td valign="top" width="133"><strong>Internal Network</strong></td>
<td valign="top" width="133"><strong>Private Network</strong></td>
</tr>
<tr>
<td valign="top" width="133">Node1</td>
<td valign="top" width="133">192.168.1.71</td>
<td valign="top" width="133">192.1.1.2</td>
</tr>
<tr>
<td valign="top" width="133">Node2</td>
<td valign="top" width="133">192.168.1.72</td>
<td valign="top" width="133">192.1.13</td>
</tr>
<tr>
<td valign="top" width="133">DC1</td>
<td valign="top" width="133">192.168.1.50</td>
<td valign="top" width="133">N/A</td>
</tr>
<tr>
<td valign="top" width="133">Clusterxm*</td>
<td valign="top" width="133">192.168.1.70</td>
<td valign="top" width="133">N/A</td>
</tr>
<tr>
<td valign="top" width="133">Availability Group* Listener</td>
<td valign="top" width="133">192.168.1.73</td>
<td valign="top" width="133">N/A</td>
</tr>
</tbody>
</table>
<p>DC1= Domain Controller</p>
<p>Cluster1 = cluster management IP (assigned during cluster configuration)</p>
<p>Availability Group Listener (assigned during AlwaysOn&#160; Availability Group Listener configuration)</p>
<p>*Don’t worry about these for now.</p>
<p>5. Since we’re using a two-node cluster without a quorum disk it is suggested to use a Node and File Share Majority so I’ll setup network share which is read/write accessible by the Cluster Service account. For my testing purposes I created share on my DC1 machine called \\DC1\Share1 located on DC1 C:\Share1 folder.</p>
<h2>Setting Up Windows Failover Clustering</h2>
<p>1.&#160; Add the Failover Cluster Manage feature to both modes by running the following&#160; PowerShell commands</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">import<span style="color: pink;">-</span>module ServerManager
Add<span style="color: pink;">-</span>WindowsFeature <span style="color: #008080; font-style: italic;">-Name</span> Failover<span style="color: pink;">-</span>Clustering</pre></div></div>

<p>2. Create the cluster by running the following PowerShell commands on one node:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">import<span style="color: pink;">-</span>module FailoverClusters
new<span style="color: pink;">-</span>cluster clusterxm <span style="color: pink;">-</span>Node node1<span style="color: pink;">,</span>node2 <span style="color: pink;">-</span>StaticAddress 192.168.1.70 <span style="color: pink;">-</span>NoStorage</pre></div></div>

<p>3. Set the quorum mode to Node and File Share Majority by running the following command on one node:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"> Set<span style="color: pink;">-</span>ClusterQuorum <span style="color: pink;">-</span>NodeAndFileShareMajority \\DC1\share1<span style="color: #800000;">&quot;&gt;\\DC1\share1</span></pre></div></div>

<h2>Install SQL Server on Both Nodes</h2>
<p>Install SQL Server and this important – <strong>As a standalone instance. </strong>Sorry no Powershell commands here just run through the installation screens. Be sure to set the SQL Server service account to a domain account (I had issues when using Local System). </p>
<p><a href="http://sev17.com/wp-content/uploads/DenaliInstall.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="DenaliInstall" border="0" alt="DenaliInstall" src="http://sev17.com/wp-content/uploads/DenaliInstall_thumb.png" width="244" height="184" /></a></p>
<h2>Database Prerequisites</h2>
<p>You need to have a database which is not already part of an AlwaysOn Availability Group in FULL recovery mode and has been backed up. As a test I’ll just use the <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=23654" target="_blank">old school pubs sample database</a>. Run the instpubs.sql file and create a backup using Powershell.</p>
<p>Start SQL Server Management Studio and select “Start PowerShell” from Object Explorer. Run the following command to backup the database to the default backup directory:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\Databases\pubs<span style="color: pink;">&gt;</span> Backup<span style="color: pink;">-</span>SqlDatabase <span style="color: pink;">-</span>Database pubs</pre></div></div>

<p>You’ll need to create a share accessible by both nodes for storing the SQL Server database and transaction log initialization backups. For my example I’ll create a folder called sqlrec under Node1’s C drive C:\sqlrec and share named sqlrec \\node1\sqlrec</p>
<h2>AlwaysOn Powershell Documentation</h2>
<p>The CTP3 version of Books Online contains some documentation and scripts for configuring AlwaysOn however as to be expected with pre-release software some topics are not covered and there are documentation errors in other sections. As I’ve encountered documentation errors I&#160; submitted Connect Items&#160; (see Connect Items below for details).&#160; Relevant helps topics included in CTP3 are listed below:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ff878441(v=sql.110).aspx" target="_blank">Specify the Endpoint URL When Adding or Modifying an Availability Replica</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/ff878259(v=SQL.110).aspx" target="_blank">Enable and Disable AlwaysOn</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/gg492181(v=SQL.110).aspx" target="_blank">Create and Configure an Availability Group</a></p>
<p>Rather than use the scripts includes with Books Online which only handle part of the configuration or write my own script I think its more important to demonstrate the commands to create and configure AlwaysOn. By approaching Powershell as simply running command versus vs. writing a script you’ll learn how to use Powershell commands for new administration functions. Once you’re happy with the results of the commands you then can turn the series of commands into a script. Let’s get started…</p>
<h2>Using Powershell to Create and Configure AlwaysOn</h2>
<p><em>Note: The following examples work within the SQLServer provider while connected to specific SQL Server machines. In my example I’m using machines named node1 and node2&#160; running a default instance. Pay particular attention to the context in which the commands are run on either Node1 (primary) or Node2 (secondary).</em></p>
<h3>Enable HADRService on both nodes:</h3>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> Enable<span style="color: pink;">-</span>SqlHADRService <span style="color: pink;">-</span>ServerInstance NODE1 <span style="color: #008080; font-style: italic;">-force</span>
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> Enable<span style="color: pink;">-</span>SqlHADRService <span style="color: pink;">-</span>ServerInstance NODE2 <span style="color: #008080; font-style: italic;">-force</span></pre></div></div>

<p><em>Note: The force switch. Enabling or disabling HADR requires SQL Server service to be restarted. If you omit the force switch you’ll be prompted to confirm SQL Server restart.</em></p>
<h3>Optionally confirm HADRService enabled on both nodes:</h3>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">get-item</span> . <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> IsHadrEnabled
&nbsp;
IsHadrEnabled
<span style="color: pink;">-------------</span>
True
&nbsp;
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">pushd</span>
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> SQLServer:\SQL\NODE2\DEFAULT
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">get-item</span> . <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> IsHadrEnabled
&nbsp;
IsHadrEnabled
<span style="color: pink;">-------------</span>
True
&nbsp;
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">popd</span></pre></div></div>

<p><em>Note: In order to retrieve the IsHadrEnabled property I need to to cd to node2&#160; and in order to easily change directories back I’m using the pushd and popd commands to store the current location (pushd) and switch back (popd).</em></p>
<h3>Configure HADR Endpoints</h3>
<p>Configure HADR Endpoints and set state to started:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> .\Endpoints
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\Endpoints<span style="color: pink;">&gt;</span> New<span style="color: pink;">-</span>SqlHADREndpoint <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800000;">&quot;hadr_endpoint&quot;</span> <span style="color: pink;">-</span>Port <span style="color: #804000;">5022</span>
&nbsp;
Name
<span style="color: pink;">----</span>
hadr_endpoint
&nbsp;
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\Endpoints<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">|</span> Set<span style="color: pink;">-</span>SqlHADREndpoint <span style="color: pink;">-</span>State <span style="color: #800000;">&quot;Started&quot;</span>
&nbsp;
Name
<span style="color: pink;">----</span>
hadr_endpoint
&nbsp;
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\Endpoints<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> SQLServer:\SQL\NODE2\DEFAULT\Endpoints
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT\Endpoints<span style="color: pink;">&gt;</span> New<span style="color: pink;">-</span>SqlHADREndpoint <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800000;">&quot;hadr_endpoint&quot;</span> <span style="color: pink;">-</span>Port <span style="color: #804000;">5022</span>
&nbsp;
Name
<span style="color: pink;">----</span>
hadr_endpoint
&nbsp;
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT\Endpoints<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">|</span> Set<span style="color: pink;">-</span>SqlHADREndpoint <span style="color: pink;">-</span>State <span style="color: #800000;">&quot;Started&quot;</span>
&nbsp;
Name
<span style="color: pink;">----</span>
hadr_endpoint</pre></td></tr></table></div>

<h3>Backup Database and Transaction Log</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT\Endpoints<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> SQLServer:\SQL\NODE1\DEFAULT
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> Backup<span style="color: pink;">-</span>SqlDatabase pubs \\NODE1\sqlrec\pubs.bak
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> Backup<span style="color: pink;">-</span>SqlDatabase pubs \\NODE1\sqlrec\pubs.trn <span style="color: pink;">-</span>BackupAction Log</pre></td></tr></table></div>

<h3>Create Replicas</h3>
<p>Note: This doesn’t actually create the replicate, rather the –AsTemplate parameter allows you to create a definition of the replica which is stored in the $replica1 and $replica2 variables. These variables will be used when creating the availability group next.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #800080;">$replica1</span> <span style="color: pink;">=</span> New<span style="color: pink;">-</span>SqlAvailabilityReplica <span style="color: #008080; font-style: italic;">-Name</span> NODE1 <span style="color: pink;">-</span>EndpointURL <span style="color: #800000;">&quot;TCP://NODE1:5022&quot;</span> <span style="color: pink;">-</span>AsTemplate <span style="color: pink;">-</span>AvailabilityMode SynchronousCommit <span style="color: pink;">-</span>FailoverMode Automatic  <span style="color: pink;">-</span>ConnectionModeInSecondaryRole AllowAllConnections
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #800080;">$replica2</span> <span style="color: pink;">=</span> New<span style="color: pink;">-</span>SqlAvailabilityReplica <span style="color: #008080; font-style: italic;">-Name</span> NODE2 <span style="color: pink;">-</span>EndpointURL <span style="color: #800000;">&quot;TCP://NODE2:5022&quot;</span> <span style="color: pink;">-</span>AsTemplate <span style="color: pink;">-</span>AvailabilityMode SynchronousCommit <span style="color: pink;">-</span>FailoverMode Automatic <span style="color: pink;">-</span>ConnectionModeInSecondaryRole AllowAllConnections</pre></td></tr></table></div>

<h3>Create Availability Group</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> New<span style="color: pink;">-</span>SqlAvailabilityGroup AVGPubs <span style="color: pink;">-</span>AvailabilityReplica <span style="color: #000000;">&#40;</span><span style="color: #800080;">$replica1</span><span style="color: pink;">,</span><span style="color: #800080;">$replica2</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">-</span>Database pubs
&nbsp;
Name                 PrimaryReplicaServerName
<span style="color: pink;">----</span>                 <span style="color: pink;">------------------------</span>
AVGPubs              NODE1</pre></td></tr></table></div>

<h3>Join Availability Group on Secondary Node</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">pushd</span>
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> SQLServer:\SQL\NODE2\DEFAULT
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT<span style="color: pink;">&gt;</span> Join<span style="color: pink;">-</span>SqlAvailabilityGroup <span style="color: #008080; font-style: italic;">-Name</span> AVGPubs
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">popd</span></pre></td></tr></table></div>

<h3>Optionally Verify Availability Groups</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> .\AvailabilityGroups
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> <span style="color: #008080; font-style: italic;">-ExpandProperty</span> AvailabilityReplicas <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> name<span style="color: pink;">,</span> ConnectionModeInPrimaryRole<span style="color: pink;">,</span> ConnectionModeInSecondaryRole
&nbsp;
Name                                                ConnectionModeInPrimaryRole           ConnectionModeInSecondaryRole
<span style="color: pink;">----</span>                                                <span style="color: pink;">---------------------------</span>           <span style="color: pink;">-----------------------------</span>
NODE1                                                       AllowAllConnections                     AllowAllConnections
NODE2                                                       AllowAllConnections                     AllowAllConnections</pre></td></tr></table></div>

<h3>Restore Database and Transaction Log On Secondary Node</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> SQLServer:\SQL\NODE2\DEFAULT
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT<span style="color: pink;">&gt;</span> Restore<span style="color: pink;">-</span>SqlDatabase pubs \\NODE1\sqlrec\pubs.bak  <span style="color: pink;">-</span>NoRecovery
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT<span style="color: pink;">&gt;</span> Restore<span style="color: pink;">-</span>SqlDatabase pubs \\NODE1\sqlrec\pubs.trn  <span style="color: pink;">-</span>RestoreAction <span style="color: #800000;">&quot;Log&quot;</span> <span style="color: pink;">-</span>NoRecovery</pre></td></tr></table></div>

<h3>Add Database to Availability Group on Secondary Node</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> .\AvailabilityGroups
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT\AvailabilityGroups<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">|</span> Add<span style="color: pink;">-</span>SqlAvailabilityDatabase <span style="color: pink;">-</span>Database pubs</pre></td></tr></table></div>

<h3>Create Availability Group Listener</h3>
<p><em>Note: This is what you connect to (or a least that’s my impression) from your client machines. The listener provides a network name and IP Address which will failover between nodes.</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">cd</span> SQLSERVER:\
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\<span style="color: pink;">&gt;</span> New<span style="color: pink;">-</span>SqlAvailabilityGroupListener <span style="color: #008080; font-style: italic;">-Name</span> Network1 <span style="color: pink;">-</span>StaticIp 192.168.1.73<span style="color: pink;">/</span>255.255.255.0 <span style="color: #008080; font-style: italic;">-path</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs
Name                 PortNumber      ClusterIPConfiguration
<span style="color: pink;">----</span>                 <span style="color: pink;">----------</span>      <span style="color: pink;">----------------------</span>
Network1             <span style="color: #804000;">1433</span>            <span style="color: #000000;">&#40;</span><span style="color: #800000;">'IP Address: 192.168.1.73'</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<h3>Optionally Verify Listener Connectivity</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span>cd SQLSERVER:\SQL\NODE1\DEFAULT
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> Invoke<span style="color: pink;">-</span>Sqlcmd <span style="color: pink;">-</span>ServerInstance Network1 <span style="color: pink;">-</span>Database master <span style="color: #008080; font-style: italic;">-Query</span> <span style="color: #800000;">&quot;select @@servername&quot;</span>
WARNING: Using provider context. Server <span style="color: pink;">=</span> NODE1.
&nbsp;
Column1
<span style="color: pink;">-------</span>
NODE1</pre></td></tr></table></div>

<h3>Determine AlwaysOn Health</h3>
<p>SQL Server includes three cmdlets for verifying the health of the various AlwaysOn components:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">get-command</span> <span style="color: pink;">-</span>module sqlps <span style="color: #008080; font-style: italic;">-Name</span> test<span style="color: pink;">-*</span>
&nbsp;
CommandType     Name                                                Definition
<span style="color: pink;">-----------</span>     <span style="color: pink;">----</span>                                                <span style="color: pink;">----------</span>
Cmdlet          Test<span style="color: pink;">-</span>SqlAvailabilityGroup                           Test<span style="color: pink;">-</span>SqlAvailabilityGroup <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #008080; font-style: italic;">-Path</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">&lt;</span>string   <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: pink;">&gt;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span>...
Cmdlet          Test<span style="color: pink;">-</span>SqlAvailabilityReplica                         Test<span style="color: pink;">-</span>SqlAvailabilityReplica <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #008080; font-style: italic;">-Path</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">&lt;</span>string   <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: pink;">&gt;</span><span style="color: #000000;">&#93;</span>...
Cmdlet          Test<span style="color: pink;">-</span>SqlDatabaseReplicaState                        Test<span style="color: pink;">-</span>SqlDatabaseReplicaState <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span><span style="color: #008080; font-style: italic;">-Path</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">&lt;</span>string   <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: pink;">&gt;</span>...</pre></td></tr></table></div>

<p>The easiest way to run the test cmdlets is within the SQL Server provider context as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> .\AvailabilityGroups
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">|</span> Test<span style="color: pink;">-</span>SqlAvailabilityGroup
&nbsp;
HealthState            Name
<span style="color: pink;">-----------</span>            <span style="color: pink;">----</span>
Healthy                AVGPubs
&nbsp;
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> .\AVGPubs\AvailabilityReplicas <span style="color: pink;">|</span> Test<span style="color: pink;">-</span>SqlAvailabilityReplica
&nbsp;
HealthState            AvailabilityGroup    Name
<span style="color: pink;">-----------</span>            <span style="color: pink;">-----------------</span>    <span style="color: pink;">----</span>
Healthy                AVGPubs              NODE1
Healthy                AVGPubs              NODE2
&nbsp;
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> .\AVGPubs\DatabaseReplicaStates <span style="color: pink;">|</span> Test<span style="color: pink;">-</span>SqlDatabaseReplicaState
&nbsp;
HealthState            AvailabilityGroup    AvailabilityReplica  Name
<span style="color: pink;">-----------</span>            <span style="color: pink;">-----------------</span>    <span style="color: pink;">-------------------</span>  <span style="color: pink;">----</span>
Healthy                AVGPubs              NODE1                pubs
Healthy                AVGPubs              NODE2                pubs</pre></td></tr></table></div>

<h3>Manually Failing Over an Availability Group</h3>
<p>To manually fail over an Availability Group we use the Switch-SqlAvailabilityGroup cmdlet. Interestingly enough I could not figure out a way to fail over the availability resource using the GUI in CTP3, so Powershell is the only way to I could do this for now which is fine by me <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://sev17.com/wp-content/uploads/wlEmoticon-smile1.png" /></p>
<p><em>Note: Be aware the context in which you run the Switch-SqlAvailabilityGroup cmdlet. The cmdlet should be run from whichever node is functioning as the secondary as we will see in a moment:</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups<span style="color: pink;">&gt;</span>cd AVGPubs
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs<span style="color: pink;">&gt;</span> Switch<span style="color: pink;">-</span>SqlAvailabilityGroup</pre></td></tr></table></div>

<p>Running the switch-sqlavailabilityGroup command on the prmiary produces the following error:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;">Switch<span style="color: pink;">-</span>SqlAvailabilityGroup : The local availability replica of availability <span style="color: #008080; font-weight: bold;">group</span> <span style="color: #800000;">'AVGPubs'</span> cannot accept signal <span style="color: #800000;">'FAIL
OVER_PENDING'</span> <span style="color: #0000FF;">in</span> its current role <span style="color: #800000;">'PRIMARY_NORMAL'</span> and state <span style="color: #000000;">&#40;</span>configuration is <span style="color: #0000FF;">in</span> Windows Server Failover Clustering st
ore<span style="color: pink;">,</span> local availability replica has joined<span style="color: #000000;">&#41;</span>.  The availability replica signal is invalid given the current replica role
.  Verify that the signal is permitted based on the current role of the local availability replica<span style="color: pink;">,</span> and retry the opera
tion.
At line:<span style="color: #804000;">1</span> char:<span style="color: #804000;">28</span>
<span style="color: pink;">+</span> Switch<span style="color: pink;">-</span>SqlAvailabilityGroup <span style="color: pink;">&lt;&lt;&lt;&lt;</span>
    <span style="color: pink;">+</span> CategoryInfo          : InvalidOperation: <span style="color: #000000;">&#40;</span>:<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#91;</span>Switch<span style="color: pink;">-</span>SqlAvailabilityGroup<span style="color: #000000;">&#93;</span><span style="color: pink;">,</span> SqlException
    <span style="color: pink;">+</span> FullyQualifiedErrorId : ExecutionFailed<span style="color: pink;">,</span>Microsoft.SqlServer.Management.PowerShell.Hadr.FailoverSqlAvailabilityGr
   oupCommand</pre></td></tr></table></div>

<p>At first I thought I had configured something incorrectly, but then was able to to failover the availability group through Failover Cluster Manager. It was then I realized this needs to be run from the context of the secondary node.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">pushd</span>
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> SQLSERVER:\SQL\NODE2\DEFAULT\AvailabilityGroups\AVGPubs
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT\AvailabilityGroups\AVGPubs<span style="color: pink;">&gt;</span> Switch<span style="color: pink;">-</span>SqlAvailabilityGroup
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE2\DEFAULT\AvailabilityGroups\AVGPubs<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">popd</span>
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs<span style="color: pink;">&gt;</span> Switch<span style="color: pink;">-</span>SqlAvailabilityGroup</pre></td></tr></table></div>

<h3></h3>
<h2>Pausing and Resuming an Availability Group</h2>
<p>You can pause and then resume synchronization of the Always database using the suspend-SqlAvailabilityDatabase and Resume-SqlAvailabilityDatabase cmdlets:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">cd</span> .\AvailabilityDatabases
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs\AvailabilityDatabases<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">|</span> Suspend<span style="color: pink;">-</span>SqlAvailabilityDatabase
<span style="color: #008080; font-weight: bold;">PS</span> SQLSERVER:\SQL\NODE1\DEFAULT\AvailabilityGroups\AVGPubs\AvailabilityDatabases<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">dir</span> <span style="color: pink;">|</span> Resume<span style="color: pink;">-</span>SqlAvailabilityDatabase</pre></td></tr></table></div>

<h2>Summary</h2>
<p>This post has demonstrated the Powershell cmdlets available to help you manage your AlwaysOn configuration. The commands could be used to build a reusable script to provide a consistent configuration of AlwaysOn. </p>
<p>My testing was completed on SQL Server CTP3 as I encountered documentation issues I logged Connect Items. I’ve included a list of Connect Items below. There are other issues with the documentation which I did not log because of missing documentation rather than documentation bugs. I think not having all the documentation complete is to be expected while a product is still in CTP.</p>
<p>I’ll save my commentary on the AlwaysOn cmdlets for a future post, but for now I will say I’m impressed with the coverage and ease of use provided by the AlwaysOn cmdlets and SQL Server provider.</p>
<h2>Connect Items</h2>
<p><a href="https://connect.microsoft.com/SQLServer/feedback/details/687148/determine-whether-alwayson-availability-groups-is-enabled" target="_blank">Determine Whether AlwaysOn Availability Groups is Enabled</a></p>
<p><a href="https://connect.microsoft.com/SQLServer/feedback/details/687149/enable-and-disable-alwayson-sql-server-documentation" target="_blank">Enable and Disable AlwaysOn (SQL Server) Documentation</a></p>
<p><a href="https://connect.microsoft.com/SQLServer/feedback/details/687153/create-and-configure-an-availability-group-sql-server-powershell-doc-error" target="_blank">Create and Configure an Availability Group (SQL Server PowerShell) Doc Error</a></p>
<p><a href="https://connect.microsoft.com/SQLServer/feedback/details/687156/new-sqlavailabilityreplica-cmdlt-allows-incompatible-settings" target="_blank">New-SqlAvailabilityReplica cmdlt Allows Incompatible settings</a></p>
<p><a href="https://connect.microsoft.com/SQLServer/feedback/details/687158/set-sqlavailabilityreplica-cmdlet-does-not-take-pipeline-input" target="_blank">Set-SqlAvailabilityReplica Cmdlet Does Not Take Pipeline input</a></p>
<p><a href="https://connect.microsoft.com/SQLServer/feedback/details/687166/new-sqlavailabilitygrouplistener-help-example-incorrect" target="_blank">New-SqlAvailabilityGroupListener Help Example Incorrect</a></p>
<p><a href="https://connect.microsoft.com/SQLServer/feedback/details/687168/creating-a-network-name-for-alwayson-availability-groups-obsolete" target="_blank">Creating a Network Name for AlwaysOn Availability Groups Obsolete</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/09/managing-alwayson-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Recursively Enumerating Local and AD Groups</title>
		<link>http://sev17.com/2011/08/recursively-enumerating-local-and-ad-groups/</link>
		<comments>http://sev17.com/2011/08/recursively-enumerating-local-and-ad-groups/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 16:16:30 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[SQLPSX]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10730</guid>
		<description><![CDATA[If you ever need to flatten out groups which may include nested local and AD groups there’s a really easy way to do this in&#160; the System.DirectoryServices.AccountManagement.GroupPrincipal GetMembers method. Here’s some PowerShell code which works against both local and AD groups. The code can easily be adapted into a function and in fact I’m using...]]></description>
			<content:encoded><![CDATA[<p>If you ever need to flatten out groups which may include nested local and AD groups there’s a really easy way to do this in&#160; the <a href="http://msdn.microsoft.com/en-us/library/bb339975.aspx" target="_blank">System.DirectoryServices.AccountManagement.GroupPrincipal GetMembers method</a>. Here’s some PowerShell code which works against both local and AD groups. The code can easily be adapted into a function and in fact I’m using similar code in the SQLPSX project for <a href="http://sqlpsx.codeplex.com/SourceControl/changeset/view/62407#536173" target="_blank">SQL Server permission auditing</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;">add<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">-</span>AssemblyName System.DirectoryServices.AccountManagement
&nbsp;
<span style="color: #800080;">$domain</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$env:computername&quot;</span>
<span style="color: #800080;">$groupname</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Administrators&quot;</span>
&nbsp;
<span style="color: #008000;">#Determine if domain a machine or domain</span>
try <span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$domainName</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Domain<span style="color: #000000;">&#93;</span>::GetComputerDomain<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> <span style="color: #008080; font-style: italic;">-ExpandProperty</span> Name
    <span style="color: #800080;">$isDomain</span> <span style="color: pink;">=</span> <span style="color: #800080;">$domainName</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">&quot;$domain\.&quot;</span>
<span style="color: #000000;">&#125;</span>
catch <span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$isDomain</span> <span style="color: pink;">=</span> <span style="color: #800080;">$false</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$isDomain</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span> <span style="color: #800080;">$ctype</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.AccountManagement.ContextType<span style="color: #000000;">&#93;</span>::Domain <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
<span style="color: #000000;">&#123;</span> <span style="color: #800080;">$ctype</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.AccountManagement.ContextType<span style="color: #000000;">&#93;</span>::Machine <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">#Create objects to filter based on group name and ContextType--Domain or Machine</span>
<span style="color: #800080;">$principal</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.DirectoryServices.AccountManagement.PrincipalContext <span style="color: #800080;">$ctype</span><span style="color: pink;">,</span><span style="color: #800080;">$domain</span>
<span style="color: #800080;">$groupPrincipal</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.DirectoryServices.AccountManagement.GroupPrincipal <span style="color: #800080;">$principal</span><span style="color: pink;">,</span><span style="color: #800080;">$groupname</span>
<span style="color: #800080;">$searcher</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.DirectoryServices.AccountManagement.PrincipalSearcher 
<span style="color: #800080;">$searcher</span>.QueryFilter <span style="color: pink;">=</span> <span style="color: #800080;">$groupPrincipal</span>
&nbsp;
<span style="color: #008000;">#Note GetMembers($true) recursively enumerates groups members while GetMembers() simply enumerates group members</span>
<span style="color: #800080;">$searcher</span>.FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.GetMembers<span style="color: #000000;">&#40;</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/08/recursively-enumerating-local-and-ad-groups/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 2/97 queries in 0.080 seconds using disk: basic
Object Caching 2360/2537 objects using disk: basic

Served from: sev17.com @ 2012-02-04 11:24:52 -->

