<?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>Personal Weblog of John Wood &#187; .Net</title>
	<atom:link href="http://www.webofwood.com/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webofwood.com</link>
	<description>Wordpress weblog of John Wood, a Database Administrator</description>
	<lastBuildDate>Fri, 16 Sep 2011 17:18:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Restore All SQL Transaction Logs using Powershell</title>
		<link>http://www.webofwood.com/2011/09/15/restore-all-sql-transaction-logs-using-powershell/</link>
		<comments>http://www.webofwood.com/2011/09/15/restore-all-sql-transaction-logs-using-powershell/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 20:49:16 +0000</pubDate>
		<dc:creator>Woody</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/?p=167</guid>
		<description><![CDATA[I came across another opportunity for using Powershell when setting up mirroring for a large database at our DR site. The database was over 73Gig. The problem arose when it took a very long time to copy the backup and transaction logs and run the database restore. By the time the copy had finished another [...]]]></description>
			<content:encoded><![CDATA[<p>I came across another opportunity for using Powershell when setting up mirroring for a large database at our DR site. The database was over 73Gig. The problem arose when it took a very long time to copy the backup and transaction logs and run the database restore. By the time the copy had finished another several transaction logs had been produced. While applying the *.bak  restore, yet another several transaction logs had been produced.<span id="more-167"></span></p>
<p>I don’t know about you, but using the SSMS GUI tool to restore a bunch of transaction logs is very tedious. I’ve got another number of large databases which will require mirroring also. With this in mind, I decided it would be beneficial to have a script to automate the process. What was of interest to me was being able to iterate through the process several times while copying new transaction logs. The script will process all the transaction logs in a directory at one time. If you happen to generate more transaction logs to the directory and run the script again, you will get error messages for the logs already restored but the new logs will still be restored.</p>
<p>The script also takes into account using UNC paths. So, you can run the script from any machine against an SQL Server on a remote machine.</p>
<pre class="brush: ps"> Restore-TransLogs.ps1 -server "MYSERVER\MYSQL" -database "MY_Database"
	.NOTES
		AUTHOR:    John P. Wood
		CREATED:   September, 2011
		VERSION:   1.0.0
#&gt;
Param(
	[Parameter(Mandatory=$true)]
	[string]$Server,
	[Parameter(Mandatory=$true)]
	[string]$database,
	[Parameter(Mandatory=$true)]
	[string]$FilePath
	)
Set-StrictMode -Version Latest
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")

## SQL Server Backup\Restore uses local file paths.
Function Get-LocalPath($File) {
	$dir = $File.DirectoryName
	$rp = $dir.Remove(0,($dir.IndexOf('$')+2))
	$root = (((Get-Item $dir).Root).Name).Replace('$',':')
	$local = $root + '\' + $rp + '\' + $File
	$local
}
Function New-SMOconnection {
    Param (
		[string]$server
	)
	$conn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($server)
	$conn.applicationName = "PowerShell SMO"
	$conn.StatementTimeout = 0
	$conn.Connect()
	if ($conn.IsOpen -eq $false) {
		Throw "Could not connect to server $($server)"
	}
	$smo = New-Object Microsoft.SqlServer.Management.Smo.Server($conn)
	$smo
}
Function Invoke-SqlRestore {
	Param(
		[string]$filename
	)
	# Get a new connection to the server
    $backupDevice = New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($filename, "File")

	# Load up the Restore object settings
	$Restore = new-object Microsoft.SqlServer.Management.Smo.Restore
	$Restore.Action = [Microsoft.SqlServer.Management.Smo.RestoreActionType]::Log
	$Restore.Database = $database
    $Restore.NoRecovery = $true
	$Restore.Devices.Add($backupDevice)

	$Restore.SqlRestore($smo)
}
Clear-Host
$smo = New-SMOconnection -server $Server
If(!(Test-Path -LiteralPath $FilePath)){Throw "FilePath not found: $FilePath"}
Get-ChildItem $FilePath -Filter "*.trn" | Sort-Object -Property LastWriteTime |
	ForEach-Object {
		If(($_.Fullname).StartsWith('\\')) {$file = Get-LocalPath $_ }
		Else {$file = $_.FullName}
		Try {Invoke-SqlRestore -filename $file}
		Catch {
			$ex = $Error[0].Exception
			Write-Output $ex.message
			while ($ex.InnerException)
			{
				$ex = $ex.InnerException
				Write-Output $ex.message
			}
		}
}
If ($smo.ConnectionContext.IsOpen -eq $true) {$smo.ConnectionContext.Disconnect()}</pre>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2011/09/15/restore-all-sql-transaction-logs-using-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cycle Those SQL Server Logs</title>
		<link>http://www.webofwood.com/2010/02/15/cycle-those-sql-server-logs/</link>
		<comments>http://www.webofwood.com/2010/02/15/cycle-those-sql-server-logs/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 21:21:04 +0000</pubDate>
		<dc:creator>John Wood</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[errorlog]]></category>
		<category><![CDATA[schedule]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/?p=62</guid>
		<description><![CDATA[Don’t you just hate it when you open one of your server logs in Management Studio and then you have to wait forever to review it because it never stops reading? I’ve done it enough times and decided to put an end to it or at least greatly reduce the incidence. Using Powershell and a [...]]]></description>
			<content:encoded><![CDATA[<p>Don’t you just hate it when you open one of your server logs in Management Studio and then you have to wait forever to review it because it never stops reading? I’ve done it enough times and decided to put an end to it or at least greatly reduce the incidence.</p>
<p>Using Powershell and a simple script to execute Sp_Cycle_Errorlog, I now have a weekly, Windows scheduled task which executes the sproc to cycle the logs for all my SQL Servers.</p>
<pre class="brush: ps">foreach ($svr in get-content D:\Scripts\Servers.TXT ){
    $svr
    Invoke-Expression 'SQLCMD -E -S $svr -Q "Exec Sp_Cycle_Errorlog"' | Out-Null
	}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2010/02/15/cycle-those-sql-server-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell Where Clause &#8211; Compare to a list of values</title>
		<link>http://www.webofwood.com/2009/08/04/powershell-where-compare-list/</link>
		<comments>http://www.webofwood.com/2009/08/04/powershell-where-compare-list/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 17:28:35 +0000</pubDate>
		<dc:creator>Woody</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/2009/08/04/powershell-where-clause-compare-to-a-list-of-values/</guid>
		<description><![CDATA[As a databases admin with a few dozen servers and a few hundred SQL Server database, I need to effectively build automated jobs to perform various administrative tasks such as backing up databases. I&#8217;m still new to PowerShell but I am beginning to see how immensely helpful it is to me and my work. I [...]]]></description>
			<content:encoded><![CDATA[<p>As a databases admin with a few dozen servers and a few hundred SQL Server database, I need to effectively build automated jobs to perform various administrative tasks such as backing up databases. I&#8217;m still new to PowerShell but I am beginning to see how immensely helpful it is to me and my work.</p>
<p>I found a good PowerShell script for backing up databases at <a title="Use Powershel to back up all user databases" href="http://blogs.msdn.com/buckwoody/archive/2009/06/25/use-powershell-to-backup-all-user-databases.aspx" target="_blank">this blog location</a>. However, I wanted to change it slightly to accommodate backing up ALL, System, User, or a list of databases. Processing ALL, System, or User is straightforward enough but I had to it was the first time I came up against have to compare a value against a list of values. In SQL you simply use an IN operator with a list of values but I was not sure of how to accomplish the same process in PowerShell.</p>
<p>Here was my problem: the user could supply a list of databases to backup and the backup script has a line with captures all the database objects for that SQL Server instance. I need to backup only those which are in the users list.</p>
<p>I found that the PowerShell <strong>WHERE</strong> clause can use a <strong>-contains</strong> operator which effectively filters the selection for an array. To test the operation I set up a small script to build two arrays and then I piped one array through a WHERE filter of which the results are piped to a ForEach. The ForEach sees only the results of the WHERE which mimics the SQL IN operator.</p>
<p>Run this code in PowerShell to see what I mean.</p>
<pre class="brush: ps">$a = @("db1","db2","db3","db4")
$d = @("db1","db2","db3","db4","db5","db6","db7","db8")
$d | where { $a -contains $_ } | foreach { Write-Host $_ }</pre>
<p><strong>$a</strong> represents the list of database supplied by the user.<br />
<strong>$d</strong> represents the array of databases returned when accessing the Microsoft.SqlServer.Management.Smo.Server object.<br />
I can now back up the user supplied databases within the foreach script block.</p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:d05e848e-d526-4494-8d76-eea0b3f11e9c" class="wlWriterSmartContent" style="margin: 0px; display: inline; padding: 0px;">del.icio.us Tags: <a rel="tag" href="http://del.icio.us/popular/PowerShell%20Script%20Where%20compare%20list">PowerShell Script Where compare list</a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2009/08/04/powershell-where-compare-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tips &amp; Tricks Using Compare-Object &#8211; Copy New Files</title>
		<link>http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/</link>
		<comments>http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 15:57:32 +0000</pubDate>
		<dc:creator>Woody</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[compare]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/</guid>
		<description><![CDATA[This is a wonderful article which not only details the use of Compare-Object but also provides some very excellent examples on its use. Tips &#38; Tricks Using Compare-Object &#8211; Dreaming in PowerShell &#8211; PowerShell.com I am very new to PowerShell but I am beginning to like using it considerably more each day. I recently had [...]]]></description>
			<content:encoded><![CDATA[<p>This is a wonderful article which not only details the use of Compare-Object but also provides some very excellent examples on its use.</p>
<p><a href="http://powershell.com/cs/blogs/tobias/archive/2009/01/09/tipps-amp-tricks-using-compare-object.aspx">Tips &amp; Tricks Using Compare-Object &#8211; Dreaming in PowerShell &#8211; PowerShell.com</a></p>
<p>I am very new to PowerShell but I am beginning to like using it considerably more each day. I recently had a need for a quick and easy way to script a process which would copy only new files from one location to another. after scouring the Internet for a good example, I stumbled upon the Compare-Object cmdlet and this <a title="Compare-Object" href="http://powershell.com/cs/blogs/tobias/archive/2009/01/09/tipps-amp-tricks-using-compare-object.aspx" target="_blank">excellent article</a>.</p>
<p>The actual script I came up with is rather simple and serves my purpose exactly. As I  mentioned, I am new to PowerShell and there may very well be a better method. However, this one works for me.</p>
<h3>The script code:</h3>
<pre class="brush: ps">param(
    [string]$s = '\\serverx\c$\ProgramData\Polaris\3.5\SQLVS1\AuthorityUpdates',
    [string]$t = '\\servery\data03\shared\libraries\zmarc'
)

$target = Get-ChildItem $t
$source = get-childitem $s
Compare-Object $source $target -Property Name -PassThru |
    Where-Object { $_.SideIndicator -eq '&lt;=' } |
    foreach-object -process{
        copy-item $_.FullName -destination $t
        }</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Darn, I was really hoping I wouldn&#8217;t have to re-install .NET 2003</title>
		<link>http://www.webofwood.com/2007/02/23/vs-studio-targets-one-version-of-net/</link>
		<comments>http://www.webofwood.com/2007/02/23/vs-studio-targets-one-version-of-net/#comments</comments>
		<pubDate>Fri, 23 Feb 2007 17:57:37 +0000</pubDate>
		<dc:creator>John Wood</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/2007/02/23/darn-i-was-really-hoping-i-wouldnt-have-to-re-install-net-2003/</guid>
		<description><![CDATA[I write very small console apps which run on different servers, some of which cannot yet be upgraded from .NET 1.1 to 2.0. Why Visual Studio targets only one version of the .NET Framework]]></description>
			<content:encoded><![CDATA[<p>I write very small console apps which run on different servers, some of which cannot yet be upgraded from .NET 1.1 to 2.0.</p>
<h2 style="font-weight: normal"><a href="http://blogs.msdn.com/johnri/archive/2005/11/29/498219.aspx"><small>Why Visual Studio targets only one version of the .NET Framework</small></a></h2>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2007/02/23/vs-studio-targets-one-version-of-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

