<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	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/"
	>

<channel>
	<title>Edwin M SarmientoPowerShell &#8211; Edwin M Sarmiento</title>
	<atom:link href="https://www.edwinmsarmiento.com/category/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.edwinmsarmiento.com</link>
	<description>Intentional Excellence</description>
	<lastBuildDate>Mon, 13 Apr 2026 21:00:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">84283043</site>		<item>
		<title>Installing, Configuring and Managing Windows Server Failover Cluster using PowerShell Series</title>
		<link>https://www.edwinmsarmiento.com/installing-configuring-and-managing-windows-server-failover-cluster-using-powershell-series/</link>
		<comments>https://www.edwinmsarmiento.com/installing-configuring-and-managing-windows-server-failover-cluster-using-powershell-series/#respond</comments>
		<pubDate>Tue, 23 Sep 2014 14:18:47 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[clustering]]></category>
		<category><![CDATA[High Availability]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Windows Cluster]]></category>
		<category><![CDATA[Windows PowerShell]]></category>
		<category><![CDATA[Windows Server Failover Clustering]]></category>
		<category><![CDATA[WSFC]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/?p=909</guid>

				<description><![CDATA[Last year, I started writing an article that was supposed to be a series for Installing, Configuring and Managing Windows Server Failover Cluster using Windows PowerShell. The first of the series came out on July 2013 (which ended up being the last article I wrote for the year 2013 on MSSQLTips.com.) Since then, I&#8217;ve been involved [&#8230;]]]></description>
					<content:encoded><![CDATA[<p>Last year, I started writing an article that was supposed to be a series for Installing, Configuring and Managing Windows Server Failover Cluster using Windows PowerShell. The <a href="http://www.mssqltips.com/sqlservertip/3001/installing-configuring-and-managing-windows-server-failover-clusters-using-powershell-part-1/" target="_blank">first</a> of the series came out on July 2013 (which ended up being the last article I wrote for the year 2013 on MSSQLTips.com.) Since then, I&#8217;ve been involved with more projects on SharePoint and business intelligence (BI) integration that I barely had a chance to work with the SQL Server database engine on a regular basis. But since part 1 of the series is already out there, I figured it&#8217;s worth the time and effort to finish it up. So, here it is &#8211; the complete series on Installing, Configuring and Managing Windows Server Failover Cluster using Windows PowerShell.</p>
<ul>
<li><a href="http://www.mssqltips.com/sqlservertip/3001/installing-configuring-and-managing-windows-server-failover-clusters-using-powershell-part-1/" target="_blank">Part 1</a> &#8211; from installing the feature to creating the Windows Server Failover Cluster</li>
<li><a href="http://www.mssqltips.com/sqlservertip/3325/installing,-configuring-and-managing-windows-server-failover-cluster-using-powershell-part-2/" target="_blank">Part 2</a> &#8211; retrieving and changing cluster object properties and adding clustered disks</li>
<li><a href="http://www.mssqltips.com/sqlservertip/3326/install-configure-and-manage-windows-server-failover-cluster-with-powershell--part-3/" target="_blank">Part 3</a> &#8211; managing permissions, changing parameter values, moving clustered resources and dependencies</li>
<li><a href="http://www.mssqltips.com/sqlservertip/3343/installing-configuring-and-managing-windows-server-failover-cluster-using-powershell-part-4/" target="_blank">Part 4</a> &#8211; common cluster troubleshooting tasks</li>
</ul>
<p>Enjoy!</p>
<h1></h1>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/installing-configuring-and-managing-windows-server-failover-cluster-using-powershell-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">909</post-id>	</item>
		<item>
		<title>From *-SqlHadrService to *-SqlAlwaysOn</title>
		<link>https://www.edwinmsarmiento.com/from-sqlhadrservice-to-sqlalwayson/</link>
		<comments>https://www.edwinmsarmiento.com/from-sqlhadrservice-to-sqlalwayson/#respond</comments>
		<pubDate>Fri, 18 Nov 2011 00:58:09 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[High Availability]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Server "Denali"]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/?p=510</guid>

				<description><![CDATA[If you&#8217;ve been playing with SQL Server &#8220;Denali&#8221; AlwaysOn, you probably have seen the new PowerShell cmdlets for managing the new AlwaysOn feature. However, as SQL Server 2012 RC0 has been made available for download, you need to watch out for these &#8220;breaking changes&#8221; for AlwaysOn (I call them breaking changes because if you are [&#8230;]]]></description>
					<content:encoded><![CDATA[<p>If you&#8217;ve been playing with SQL Server &#8220;Denali&#8221; AlwaysOn, you probably have seen the new PowerShell cmdlets for managing the new AlwaysOn feature. However, as SQL Server 2012 RC0 has been made available for download, you need to watch out for these &#8220;breaking changes&#8221; for AlwaysOn (I call them breaking changes because if you are using PowerShell scripts to automate AlwaysOn configuration, your scripts would no longer work.)</p>
<p>With SQL Server &#8220;Denali&#8221; CTP3, you may have used cmdlets like <strong>*-SqlHadrService</strong> to enable the AlwaysOn features across all instances that you join in your availability group.</p>
<p><a href="http://bassplayerdoc.files.wordpress.com/2011/11/ctp3-hadr1.jpg"><img fetchpriority="high" decoding="async" class="aligncenter size-full wp-image-515" title="CTP3-HADR" src="http://bassplayerdoc.files.wordpress.com/2011/11/ctp3-hadr1.jpg" alt="" width="529" height="135" srcset="https://www.edwinmsarmiento.com/wp-content/uploads/2011/11/ctp3-hadr1.jpg 888w, https://www.edwinmsarmiento.com/wp-content/uploads/2011/11/ctp3-hadr1-300x77.jpg 300w, https://www.edwinmsarmiento.com/wp-content/uploads/2011/11/ctp3-hadr1-760x195.jpg 760w, https://www.edwinmsarmiento.com/wp-content/uploads/2011/11/ctp3-hadr1-518x133.jpg 518w, https://www.edwinmsarmiento.com/wp-content/uploads/2011/11/ctp3-hadr1-82x21.jpg 82w, https://www.edwinmsarmiento.com/wp-content/uploads/2011/11/ctp3-hadr1-600x154.jpg 600w" sizes="(max-width: 529px) 100vw, 529px" /></a><a href="http://bassplayerdoc.files.wordpress.com/2011/11/ctp3-hadr.jpg"><br />
</a>Starting with SQL Server 2012 RC0, these have now been converted to <strong>*-SqlAlwaysOn</strong>.</p>
<p style="text-align:left;"><a href="http://bassplayerdoc.files.wordpress.com/2011/11/rc0-alwaysonpowershell.jpg"><img decoding="async" class="aligncenter size-full wp-image-521" title="RC0-AlwaysOnPowerShell" src="http://bassplayerdoc.files.wordpress.com/2011/11/rc0-alwaysonpowershell.jpg" alt="" width="529" height="261" /></a><a href="http://bassplayerdoc.files.wordpress.com/2011/11/rc0-hadr.jpg"><br />
</a>So be sure to update those PowerShell scripts before you run them for testing to make sure you don&#8217;t waste your time trying to figure out what went wrong. And if you haven&#8217;t downloaded <a href="http://www.microsoft.com/download/en/details.aspx?id=28145" target="_blank">SQL Server 2012 RC0</a> yet, make sure you do so and get your hands dirty trying out the latest bits.</p>
<p style="text-align:left;">And since we&#8217;re already on the topic of SQL Server 2012, you also might want to catch my webcast on AlwaysOn Availability Groups on 30-Nov-2011. You can register for the webcast thru <a href="https://www2.gotomeeting.com/register/354696082" target="_blank">this URL</a>.</p>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/from-sqlhadrservice-to-sqlalwayson/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">510</post-id>	</item>
		<item>
		<title>Configuring Alternate Access Mapping in SharePoint 2010 using Windows PowerShell</title>
		<link>https://www.edwinmsarmiento.com/configuring-alternate-access-mapping-in-sharepoint-2010-using-windows-powershell/</link>
		<comments>https://www.edwinmsarmiento.com/configuring-alternate-access-mapping-in-sharepoint-2010-using-windows-powershell/#respond</comments>
		<pubDate>Tue, 19 Apr 2011 05:36:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[Alternate Access Mappings]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SharePoint]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2011/04/19/configuring-alternate-access-mapping-in-sharepoint-2010-using-windows-powershell</guid>

				<description><![CDATA[The PowerShell equivalent of configuring alternate access mappings tip on MSSharePointTips.com has been published. Check out the article.]]></description>
					<content:encoded><![CDATA[<p><span style="font-family:Arial, Helvetica, sans-serif;">The PowerShell equivalent of configuring alternate access mappings tip on MSSharePointTips.com has been published. Check out the <a href="http://mssharepointtips.com/tip.asp?id=1113">article</a>. </span></p>
<div class="blogger-post-footer"><img decoding="async" src="https://blogger.googleusercontent.com/tracker/92377218009570869-1713257501205470048?l=bassplayerdoc.blogspot.com" alt="" width="1" height="1" /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/configuring-alternate-access-mapping-in-sharepoint-2010-using-windows-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">230</post-id>	</item>
		<item>
		<title>Check for Free Space on your Drives &#8211; LazyAdmin PowerShell version</title>
		<link>https://www.edwinmsarmiento.com/check-for-free-space-on-your-drives-lazyadmin-powershell-version/</link>
		<comments>https://www.edwinmsarmiento.com/check-for-free-space-on-your-drives-lazyadmin-powershell-version/#respond</comments>
		<pubDate>Sat, 29 Jan 2011 18:36:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Send-MailMessage]]></category>
		<category><![CDATA[Win32_Volume]]></category>
		<category><![CDATA[WMI]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2011/01/29/check-for-free-space-on-your-drives-lazyadmin-powershell-version</guid>

				<description><![CDATA[When tasked to automate a task, I rely on my scripting background to get the job done. Back then, it was all VBScript. Now that more and more customers are moving to Windows Server 2008 servers, I try to convince my customers to use PowerShell for all their scripting needs. A few weeks back, a [&#8230;]]]></description>
					<content:encoded><![CDATA[<p><span style="font-family:Arial, Helvetica, sans-serif;">When tasked to automate a task, I rely on my scripting background to get the job done. Back then, it was all VBScript. Now that more and more customers are moving to Windows Server 2008 servers, I try to convince my customers to use PowerShell for all their scripting needs.</span></p>
<p><span style="font-family:Arial;">A few weeks back, a friend of mine asked me to write a script that will check the disks on all of their servers and send a report via email.  They have over a hundred or so servers in their data center and it will be a bit cumbersome to log in to each server just to check for free space on their disks. While their network operations center have access to reports generated by their monitoring tools, she didn&#8217;t. Which is why she opted to do it on her own. I tried to demonstrate why PowerShell was my scripting language of choice for this task by highlighting how this task can be done in a single line of code. Not that it can&#8217;t be done otherwise but this is an effective way to tell people how easy it is to use Windows PowerShell. Below is the script to do the task (<em>BTW, this can be written in a single line. The blog engine just made it as it is due to space constraints</em>)</span></p>
<p><span style="font-family:&quot;"><strong>Get-WmiObject Win32_Volume -computername &#8220;localhost&#8221; |</strong></span></p>
<p><span style="font-family:&quot;"><strong>Select-Object __SERVER, Name, @{Name=&#8221;Size(GB)&#8221;;Expression={&#8220;{0:N1}&#8221; -f($_.Capacity/1gb)}},@{Name=&#8221;FreeSpace(GB)&#8221;;Expression={&#8220;{0:N1}&#8221; -f($_.freespace/1gb)}},@{Name=&#8221;FreeSpacePerCent&#8221;;Expression={&#8220;{0:P0}&#8221; -f($_.freespace/$_.capacity)}} | </strong></span><br />
<span style="font-family:&quot;"><strong>Where-Object -FilterScript {$_.FreeSpacePerCent -lt 15} |</strong></span><br />
<span style="font-family:&quot;"><strong>Sort-Object -property &#8220;FreeSpacePerCent&#8221; </strong></span><br />
<span style="font-family:&quot;"><strong>Format-Table</strong></span></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">Now this may seem intimidating at first but let&#8217;s disect the script to understand what it is doing. The <a href="http://technet.microsoft.com/en-us/library/dd315295.aspx"><strong><span style="font-size:x-small;">Get-WmiObject</span></strong></a> cmdlet calls the <a href="http://msdn.microsoft.com/en-us/library/aa394515(v=vs.85).aspx"><strong><span style="font-size:x-small;">Win32_Volume</span></strong></a> class to scan thru a list of disks (I&#8217;m using the -computer parameter to highlight an important concept later on.) This includes mountpoints, local drives and USB drives. If you&#8217;re only concerned about local disks and mountpoints, you can exclude USB drives by filtering via the DriveType attribute of the Win32_Volume class. </span></p>
<p><strong><span style="font-family:&quot;">Get-WmiObject Win32_Volume -computername &#8220;localhost&#8221; </span></strong></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">Since we are interested in the size of the disk, the free space in GB and in per cent values, we will use the <strong><span style="font-size:x-small;">Capacity</span></strong> and <strong><span style="font-size:x-small;">FreeSpace</span></strong> attributes of the Win32_Volume class. We do need to perform some calculations to make sure that we get the values we are accustomed with &#8211; GB for capacity and per cent in free space value. That&#8217;s what the calculations are for, noting that capacity and free space are expressed in bytes. The <a href="http://technet.microsoft.com/en-us/library/dd315291.aspx"><strong><span style="font-size:x-small;">Select-Object</span></strong></a> cmdlet simply creates a new object by defining a new attribute from the original Win32_Volume class &#8211; server hostname, drive or mountpoint name, capacity and free space &#8211; named FreeSpacePerCent. This will definitely give you all the disks on a server with the defined attributes. </span></p>
<p><span style="font-family:&quot;"><strong>Select-Object __SERVER, Name, @{Name=&#8221;Size(GB)&#8221;;Expression={&#8220;{0:N1}&#8221; -f($_.Capacity/1gb)}},@{Name=&#8221;FreeSpace(GB)&#8221;;Expression={&#8220;{0:N1}&#8221; -f($_.freespace/1gb)}},@{Name=&#8221;FreeSpacePerCent&#8221;;Expression={&#8220;{0:P0}&#8221; -f($_.freespace/$_.capacity)}}</strong></span></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">But we don&#8217;t want all of the drives. We only want those that have FreeSpacePerCent value less than your allowable threshold. In this example, less than 15 %. So, we use the <a href="http://technet.microsoft.com/en-us/library/dd315399.aspx"><strong><span style="font-size:x-small;">Where-Object</span></strong></a> to filter the results. </span></p>
<p><strong><span style="font-family:&quot;">Where-Object -FilterScript {$_.FreeSpacePerCent -lt 15}</span></strong></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">As an administrator, we need to make sure that we address issues that are more critical than others. This is where the <a href="http://technet.microsoft.com/en-us/library/dd347688.aspx"><strong><span style="font-size:x-small;">Sort-Object</span></strong></a> cmdlet comes in. We sort the results in order of increasing FreeSpacePerCent so we can immediately address those disks with very little free space left. </span></p>
<p><span style="font-family:&quot;"><strong>Sort-Object -property &#8220;FreeSpacePerCent&#8221;</strong></span></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">Finally, the <a href="http://technet.microsoft.com/en-us/library/dd315255.aspx"><strong><span style="font-size:x-small;">Format-Table</span></strong></a> cmdlet is just for aesthetics. The reason is because I will be sending the results of this script as an email attachment. </span></p>
<p>Now, that wasn&#8217;t so hard, was it? Understanding what each cmdlet is doing and how you can pipe the results to another cmdlet is the key to maximizing the use of PowerShell. But if you have a hundred or more servers, you wouldn&#8217;t want to copy the script on all of your servers and run it from there, would you? There are a few ways to accomplish this. One of which is to ue <a href="http://msdn.microsoft.com/en-us/library/ee706585(v=vs.85).aspx"><strong><span style="font-size:x-small;">PowerShell Remoting</span></strong></a>. This will be another topic for a blog post as there is more to it than just simply writing and executing a PowerShell script remotely. What I opted to do here is simpler since I&#8217;m assuming that my friend nor I don&#8217;t have access to Active Directory to create Group Policies to enable PowerShell Remoting on all of the servers. Since the Get-WmiObject cmdlet has a -computer parameter, I can use that to execute queries against remote computers. What I can do is simply read thru a list &#8211; probably a list of computers in Active Directory or even as simple as a text file list. I opted for the text file list for the same reason that I didn&#8217;t go for PowerShell Remoting. So, what I did was to convert the script above to a function that I will call while passing values to the -computer parameter of the Get-WmiObject</p>
<p><span style="font-family:&quot;"><strong>function getDiskFreeSpace</strong></span><br />
<span style="font-family:&quot;"><strong>{</strong></span><br />
<span style="font-family:&quot;"><strong>Get-WmiObject Win32_Volume -computername $args | </strong></span><br />
<span style="font-family:&quot;"><strong>Select-Object __SERVER, Name, @{Name=&#8221;Size(GB)&#8221;;Expression={&#8220;{0:N1}&#8221; -f($_.Capacity/1gb)}},@{Name=&#8221;FreeSpace(GB)&#8221;;Expression={&#8220;{0:N1}&#8221; -f($_.freespace/1gb)}},@{Name=&#8221;FreeSpacePerCent&#8221;;Expression={&#8220;{0:P0}&#8221; -f($_.freespace/$_.capacity)}} |</strong></span><br />
<span style="font-family:&quot;"><strong>Where-Object -FilterScript {$_.FreeSpacePerCent -lt 15}  |</strong></span><br />
<span style="font-family:&quot;"><strong>Sort-Object -property &#8220;FreeSpacePerCent&#8221;  |</strong></span><br />
<span style="font-family:&quot;"><strong>Format-Table</strong></span><br />
<span style="font-family:&quot;"><strong>}</strong></span></p>
<p>I will call this function as I read thru a list of servers in a text file passing the computer names as parameters. And since I want the results of the query in a single output file, I used the <a href="http://technet.microsoft.com/en-us/library/dd315303.aspx"><strong><span style="font-size:x-small;">Out-File</span></strong></a> cmdlet to save the results in a text file</p>
<p><strong><span style="font-family:&quot;">ForEach($s in Get-Content C:serverlist.txt) </span></strong><br />
<strong><span style="font-family:&quot;">{</span></strong><br />
<strong><span style="font-family:&quot;">getDiskFreeSpace $s  | Out-File C:diskFreeSpaceResults.txt -append</span></strong><br />
<strong><span style="font-family:&quot;">}</span></strong></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;"><strong>Sending emails</strong></span></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">I used a simple script in the past to send emails via Windows PowerShell. This was prior to PowerShell v2.0</span></p>
<p><strong><span style="font-family:&quot;">$SmtpClient = new-object system.net.mail.smtpClient</span></strong><br />
<strong><span style="font-family:&quot;">$SmtpServer = &#8220;smtp.yourmailserver.local&#8221;</span></strong><br />
<strong><span style="font-family:&quot;">$SmtpClient.host = $SmtpServer </span></strong></p>
<p><strong><span style="font-family:&quot;">$From = &#8220;Friendly Reminder &#8220;</span></strong><br />
<strong><span style="font-family:&quot;">$To = &#8220;recepient@yourmailserver.net&#8221;</span></strong><br />
<strong><span style="font-family:&quot;">$Title = &#8220;Subject Matter&#8221;</span></strong><br />
<strong><span style="font-family:&quot;">$Body = &#8220;Body Text&#8221; </span></strong><br />
<strong><br />
</strong><br />
<strong><span style="font-family:&quot;">$SmtpClient.Send($from,$to,$title,$Body) </span></strong></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">With PowerShell v2.0, the <a href="http://technet.microsoft.com/en-us/library/dd347693.aspx"><strong><span style="font-size:x-small;">Send-MailMessage</span></strong></a> cmdlet was made available to send an email message. This made it easier to integrate sending email functionalities in PowerShell scripts. Adding one more line to the script above, I&#8217;ve included email sending functionality with attachment</span></p>
<p><span style="font-family:&quot;"><strong>Send-MailMessage -to &#8220;recepient@mail.com&#8221; -from &#8220;sender@mail.com&#8221; -subject &#8220;Servers Disk Free Space Report&#8221; -Attachment &#8220;C:diskFreeSpaceResults.txt&#8221; -SmtpServer &#8220;yourSMTPserver.mail.com&#8221; </strong></span></p>
<p><span style="font-family:Arial, Helvetica, sans-serif;">Now, you can start using this script with a text file that lists all of your servers. Make sure that the server on which you will be running this script has Windows PowerShell v2.0 installed and that you can communicate with those servers. I&#8217;ve seen servers on different VLANs that are isolated from each other but are in the same Active Directory domain. This causes the script to fail due to connectivity issues. Check with your network administrator to be sure.</span></p>
<div class="blogger-post-footer"><img loading="lazy" decoding="async" src="" alt="" width="1" height="1" /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/check-for-free-space-on-your-drives-lazyadmin-powershell-version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">224</post-id>	</item>
		<item>
		<title>Your poor man&#8217;s SQL Server log shipping-PowerShell version</title>
		<link>https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping-powershell-version/</link>
		<comments>https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping-powershell-version/#comments</comments>
		<pubDate>Sat, 01 May 2010 14:18:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[log shipping]]></category>
		<category><![CDATA[modify SQL Server logins]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2010/05/01/your-poor-mans-sql-server-log-shipping-powershell-version</guid>

				<description><![CDATA[In 2008, I was privileged to be a part of a project to write a chapter for a book that was for a good cause. SQL Server MVP Deep Dives became an avenue for a lot of the SQL Server MVPs to share their expertise for the benefit of not just the technical community but [&#8230;]]]></description>
					<content:encoded><![CDATA[<p><span style="font-family: arial;">In 2008, I was privileged to be a part of a project to write a chapter for a book that was for a good cause. </span><a href="http://www.manning.com/nielsen/"><span style="font-family: arial;">SQL Server MVP Deep Dives</span></a><span style="font-family: arial;"> became an avenue for a lot of the SQL Server MVPs to share their expertise for the benefit of not just the technical community but of the beneficiary.</span></p>
<p><span style="font-family: Arial;">I wrote a chapter for the book based on this <a href="https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping/" target="_blank">blog post</a> some three years ago and one of the recommendations I did was to convert the VBScript scripts to Windows PowerShell. So, here it is.</span></p>
<p><span style="font-family: Arial;">I&#8217;ve converted the VBScript that does the checking of the folders containing my latest LOG backups and restoring them in NORECOVERY mode to Windows PowerShell. What is fascinating is the fact that if the process I&#8217;ve outlined in the blog post is followed thru, adding another database to be configured for log shipping was as easy as creating regular LOG backups via TSQL scripts or database maintenance plans &#8211; no need to add an entry for every database that will be configured. How cool was that?</span></p>
<p><span style="font-family: Arial;">So, here&#8217;s the Windows PowerShell version of the custom restore LOG backup command</span></p>
<p><code style="font-size: 12px;"><span style="color: black;">$logBackupFolder</span><span style="color: blue;">=</span><span style="color: darkred;">"your LOG backup location here or a parameter variable"</p>
<p></span><span style="color: black;">##Variable </span><span style="color: blue;">for </span><span style="color: black;">time duration </span><span style="color: gray;">- </span><span style="color: black;">the amount of time you need to generate and restore LOG backups<br />
$1HourOld </span><span style="color: blue;">= </span><span style="color: black;">[DateTime]</span><span style="color: gray;">::</span><span style="color: black;">Now.AddHours</span><span style="color: gray;">(-</span><span style="color: black;">1</span><span style="color: gray;">)</p>
<p></span><span style="color: black;">##Retrieve folders and files </span><span style="color: blue;">in </span><span style="color: black;">the specified directory<br />
</span><span style="color: blue;">foreach </span><span style="color: gray;">(</span><span style="color: black;">$databaseName </span><span style="color: blue;">in </span><span style="color: gray;">(</span><span style="color: black;">Get</span><span style="color: gray;">-</span><span style="color: black;">ChildItem $logBackupFolder </span><span style="color: gray;">| </span><span style="color: black;">Where { $_.PsIsContainer }</span><span style="color: gray;">) )<br />
</span><span style="color: black;">{<br />
</span><span style="color: blue;">foreach </span><span style="color: gray;">(</span><span style="color: black;">$logName </span><span style="color: blue;">in </span><span style="color: gray;">(</span><span style="color: black;">Get</span><span style="color: gray;">-</span><span style="color: black;">ChildItem $logBackupFolder$databasename </span><span style="color: gray;">| </span><span style="color: black;">where {</span><span style="color: gray;">(</span><span style="color: black;">$_.CreationTime </span><span style="color: gray;">-</span><span style="color: black;">ge $1HourOld</span><span style="color: gray;">) -</span><span style="color: black;">and </span><span style="color: gray;">(</span><span style="color: black;">$_.Extension </span><span style="color: gray;">-</span><span style="color: black;">eq </span><span style="color: darkred;">".trn"</span><span style="color: gray;">)</span><span style="color: black;">} SELECT name</span><span style="color: gray;">))<br />
</span><span style="color: black;">{</p>
<p>$logBackupPath </span><span style="color: blue;">= </span><span style="color: black;">[System.String]</span><span style="color: gray;">::</span><span style="color: black;">Concat</span><span style="color: gray;">(</span><span style="color: darkred;">"'$logBackupFolder"</span><span style="color: gray;">,</span><span style="color: black;">$databasename</span><span style="color: gray;">,</span><span style="color: darkred;">""</span><span style="color: gray;">,</span><span style="color: black;">$logName.Name</span><span style="color: gray;">,</span><span style="color: darkred;">"'"</span><span style="color: gray;">)<br />
</span><span style="color: black;">$restoreSQL </span><span style="color: blue;">= </span><span style="color: darkred;">"RESTORE LOG $databaseName FROM DISK=$logBackupPath WITH NORECOVERY" </span><span style="color: gray;">| </span><span style="color: black;">Out</span><span style="color: gray;">-</span><span style="color: black;">File D:$databaseName.sql</p>
<p>Invoke</span><span style="color: gray;">-</span><span style="color: black;">Expression </span><span style="color: darkred;">"osql.exe -SYourSQLServerInstanceHere -E -iD:$databaseName.sql"</p>
<p></span><span style="color: black;">}<br />
}<br />
</span></code></p>
<p><span style="font-family: Arial;">You might be wondering why I am using osql.exe instead of sqlcmd.exe. Well, it&#8217;s still there even if you have SQL Server 2008. Which means this script will work even for SQL Server 2000 instances as long as you install Windows PowerShell on the machine running SQL Server.</span></p>
<p><span style="font-family: Arial;">And all of that for a mere 17-liner script. I could write it even less than that but I&#8217;m a big fan of script readability. Now, that&#8217;s a good enough reason to dig deeper on what Windows PowerShell has to offer</span></p>
<div class="blogger-post-footer"><img loading="lazy" alt="" width="1" height="1" /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping-powershell-version/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">213</post-id>	</item>
	</channel>
</rss>