<?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> &#187; powershell</title>
	<atom:link href="http://benchmarkitconsulting.com/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://benchmarkitconsulting.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 Feb 2012 16:34:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>EDMPASS &#8211; August Meeting w/Aaron Nelson</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2010/08/06/edmpass-august-meeting-waaron-nelson/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2010/08/06/edmpass-august-meeting-waaron-nelson/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 16:12:17 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Aaron Nelson]]></category>
		<category><![CDATA[EDMPASS]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=1450</guid>
		<description><![CDATA[On August 25th 2010 the Edmonton Chapter of PASS is having it’s next meeting. Details below: http://www.eventbrite.com/event/792195478 Please be sure to not only click the “Add to my calendar” but also the “Register” button so that we can plan accordingly for food and drinks.  Date:  August 25th 2010 Time: 5:00 pm &#8211; 7:00 pm Location: Stanley A. Milner library Map: 7 [...]]]></description>
			<content:encoded><![CDATA[<p>On August 25th 2010 the Edmonton Chapter of PASS is having it’s next meeting. Details below:</p>
<p><strong><a href="http://www.eventbrite.com/event/792195478" target="_blank">http://www.eventbrite.com/event/792195478</a></strong></p>
<p><strong>Please be sure to not only click the “Add to my calendar” but also the “Register” button so that we can plan accordingly for food and drinks.</strong> </p>
<p><strong>Date:</strong>  August 25th 2010<br />
<strong>Time:</strong> 5:00 pm &#8211; 7:00 pm<br />
<strong>Location:</strong> Stanley A. Milner library<br />
<strong>Map:</strong> <a href="http://www.mapquest.com/maps/map.adp?formtype=address&amp;country=CA&amp;popflag=0&amp;latitude=&amp;longitude=&amp;name=&amp;phone=&amp;level=&amp;addtohistory=&amp;cat=&amp;address=7+Sir+Winston+Churchill+Square+NW&amp;city=Edmonton&amp;state=AB&amp;zipcode=" target="_blank">7 Sir Winston Churchill Square</a><br />
<strong>Meeting Room:</strong> <strong>Centennial Room &#8211; Basement</strong><br />
<strong>Speaker:</strong> Aaron Nelson<br />
<strong>Topic:</strong> Powershell for Database Professionals</p>
<p><strong>Session Abstract:</strong></p>
<p>In this session we will begin exploring the amazing world of the PowerShell language. We will learn to perform several everyday DBA tasks like backing up user databases, scripting table objects and evaluating disk space usage with PowerShell; then we will change these into multi-server scripts by adding only a single line of code.  We will also take a look at some data visualization techniques that require only a small amount of code.  Only a basic understanding of PowerShell or DOS is needed. This session should serve as a good introduction to PowerShell for database users.  The goal is to get data professionals feet wet about PowerShell and ready to practice it when they leave.  Yes you will get a copy of the scripts I use.</p>
<p><strong>Presenter Information:</strong>  Aaron Nelson (<a href="http://twitter.com/SQLvariant" target="_blank">@SQLvariant</a>)  <a href="http://sqlvariant.com/wordpress/" target="_blank">sqlvariant.com/wordpress/</a><br />
Aaron Nelson is a Senior SQL Server Architect with over 10 years experience in architecture, business intelligence, development, and performance tuning of SQL Server.<br />
He has experience managing enterprise-wide data needs in both transactional and data warehouse environments.  Aaron holds certifications for MCITP: Business Intelligence Developer, Database Administrator, Database Developer; as well as MCTS: Windows Server Virtualization, Configuration (meaning Hyper-V).</p>
<p><strong>Agenda:<br />
</strong>5:00 pm &#8211; Pizza and Socializing<br />
5:30 pm &#8211; Sponsor Presentation<br />
5:45 pm &#8211; Feature Presentation<br />
7:00 pm &#8211; Wrap Up and Draws</p>
<p>If you haven’t signed up already at <a href="http://edmpass.com/?page_id=7" target="_blank">EDMPASS.com</a> please do so now to receive meeting notifications, news, and updates from EDMPASS.</p>
<p>Hope to see you there.</p>
<p>Enjoy!!</p>
<p><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=EDMPASS+%26%238211%3B+August+Meeting+w%2FAaron+Nelson+http://tinyurl.com/326rmxe" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/08/06/edmpass-august-meeting-waaron-nelson/&amp;title=EDMPASS+%26%238211%3B+August+Meeting+w%2FAaron+Nelson" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/08/06/edmpass-august-meeting-waaron-nelson/&amp;title=EDMPASS+%26%238211%3B+August+Meeting+w%2FAaron+Nelson" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/08/06/edmpass-august-meeting-waaron-nelson/&amp;title=EDMPASS+%26%238211%3B+August+Meeting+w%2FAaron+Nelson" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2010/08/06/edmpass-august-meeting-waaron-nelson/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monitoring With Powershell (pings, services, and websites)</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2010/03/25/monitoring-with-powershell-pings-services-and-websites/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2010/03/25/monitoring-with-powershell-pings-services-and-websites/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 16:49:12 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[ping]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=1364</guid>
		<description><![CDATA[So it&#8217;s no secret that I&#8217;ve been having an affair on TSQL. I&#8217;ve been seeing PowerShell behind her back and I gotta tell ya even after the initial excitement of a new language I&#8217;m still loving it. Let&#8217;s talk monitoring. I started with a requirement of: Pinging a list of production level servers Checking specific [...]]]></description>
			<content:encoded><![CDATA[<p>So it&#8217;s no secret that I&#8217;ve been having an affair on TSQL. I&#8217;ve been seeing PowerShell behind her back and I gotta tell ya even after the initial excitement of a new language I&#8217;m still loving it. Let&#8217;s talk monitoring. I started with a requirement of:</p>
<ul>
<li>Pinging a list of production level servers</li>
<li>Checking specific services on those servers</li>
<li>Checking website availability for a list of websites</li>
<li>All information and history needs to be tracked in a database for SLA analysis.</li>
</ul>
<p>So where am I going to get my list of servers from?</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># REMOVE FILE FOR NEW EXECUTION RESULTS</span>
<span style="color: #008080; font-weight: bold;">del</span> F:\MonitoringDB\AllDomainServers.txt 
&nbsp;
<span style="color: #008000;"># SEARCH CRITERIA COMPUTERS THAT HAVE WINDOWS*SERVER AS AN OS</span>
<span style="color: #800080;">$SearchCategory</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;computer&quot;</span>
<span style="color: #800080;">$SearchOS</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Windows*Server*&quot;</span>
&nbsp;
<span style="color: #800080;">$Domain</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.DirectoryServices.DirectoryEntry
&nbsp;
<span style="color: #800080;">$objSearcher</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.DirectoryServices.DirectorySearcher
<span style="color: #800080;">$objSearcher</span>.SearchRoot <span style="color: pink;">=</span> <span style="color: #800080;">$Domain</span>
<span style="color: #800080;">$objSearcher</span>.<span style="color: #0000FF;">Filter</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;OperatingSystem=$SearchOS&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$ServerName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;name&quot;</span>
&nbsp;
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$ServerName</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$objSearcher</span>.PropertiesToLoad.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #800080;">$Results</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objSearcher</span>.FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$ResultRow</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$Results</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$Computer</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ResultRow</span>.Properties;
	<span style="color: #800080;">$Computer</span>.name <span style="color: pink;">|</span>Out<span style="color: pink;">-</span>File F:\MonitoringDB\AllDomainServers.txt <span style="color: #008080; font-style: italic;">-Append</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now let&#8217;s create a database called MonitoringDB on a SQL Server 2008 Instance and run the following script to create the tables needed:</p>

<div class="wp_syntax"><div class="code"><pre class="t-sql" style="font-family:monospace;">CREATE TABLE [dbo].[ServerList](
	[ServerID] [int] IDENTITY(1,1) NOT NULL,
	[ServerName] [nvarchar](255) NOT NULL,
	[Environment] [nvarchar](10) NULL,
	[CreateDateTime] [datetime] NOT NULL,
	[LastModDateTime] [datetime] NOT NULL,
	[ActiveInd] [bit] NOT NULL,
 CONSTRAINT [PK_ServerList] PRIMARY KEY CLUSTERED
(
	[ServerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
&nbsp;
GO
ALTER TABLE [dbo].[ServerList] ADD  CONSTRAINT [DF_ServerList_CreateDateTime]  DEFAULT (getdate()) FOR [CreateDateTime]
GO
ALTER TABLE [dbo].[ServerList] ADD  CONSTRAINT [DF_ServerList_LastModDateTime]  DEFAULT (getdate()) FOR [LastModDateTime]
GO
ALTER TABLE [dbo].[ServerList] ADD  CONSTRAINT [DF_ServerList_ActiveInd]  DEFAULT ((1)) FOR [ActiveInd]
GO
CREATE TABLE [dbo].[ServiceCheck](
	[ServiceCheckID] [int] IDENTITY(1,1) NOT NULL,
	[ServerID] [int] NOT NULL,
	[ServiceName] [nvarchar](255) NOT NULL,
	[ServiceType] [nvarchar](10) NULL,
	[Notes] [nvarchar](500) NULL,
	[ActiveInd] [bit] NOT NULL,
	[CreateDateTime] [datetime] NOT NULL,
	[LastModDateTime] [datetime] NOT NULL,
 CONSTRAINT [PK_ServiceCheck_1] PRIMARY KEY CLUSTERED
(
	[ServiceCheckID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ServiceCheck] ADD  CONSTRAINT [DF_ServiceCheck_ActiveInd]  DEFAULT ((1)) FOR [ActiveInd]
GO
ALTER TABLE [dbo].[ServiceCheck] ADD  CONSTRAINT [DF_ServiceCheck_CreateDateTime]  DEFAULT (getdate()) FOR [CreateDateTime]
GO
ALTER TABLE [dbo].[ServiceCheck] ADD  CONSTRAINT [DF_ServiceCheck_LastModDateTime]  DEFAULT (getdate()) FOR [LastModDateTime]
GO
CREATE TABLE [dbo].[ServiceCheckHistoryAudit](
	[ServiceCheckHistoryAuditID] [bigint] IDENTITY(1,1) NOT NULL,
	[ServiceCheckID] [int] NOT NULL,
	[AuditDateTime] [datetime] NOT NULL,
	[ServiceState] [bit] NOT NULL,
 CONSTRAINT [PK_ServiceCheckHistoryAudit] PRIMARY KEY CLUSTERED
(
	[ServiceCheckHistoryAuditID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ServiceCheckHistoryAudit] ADD  CONSTRAINT [DF_ServiceCheckHistoryAudit_AuditDateTime]  DEFAULT (getdate()) FOR [AuditDateTime]
GO</pre></div></div>

<p>OK now that we have our list of servers and a database to store them in, import your list into the ServerList table. If you&#8217;re having troubles with this step an easy way to do this is just to:</p>
<ul>
<li>Right click on the MonitoringDB database, Tasks, Import Data.</li>
<li>Choose a flatfile source and browse to your AllDomainServers.txt file</li>
<li>Import it into the ServerList table making sure to map the only column in the txt file to the ServerName column</li>
</ul>
<p>OK so let&#8217;s pick a couple of development SQL Servers instances and update the active indicator (ActiveInd) so that those servers are the only ones that are going to be used in this post. Your script should look something like:</p>

<div class="wp_syntax"><div class="code"><pre class="t-sql" style="font-family:monospace;">UPDATE	dbo.ServerList
SET	ActiveInd = 0
WHERE	ServerName NOT IN ('Server1', 'Server2', 'Server3')</pre></div></div>

<p>OK so let&#8217;s first populate some services that we want to check. Hmmmmmm how are we going to know the service names? Don&#8217;t worry I wont leave ya hanging&#8230; Here is yet another PowerShell script that you can use to get the list of services and their current status:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$WMI</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Class</span> win32_service <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;Server1&quot;</span>
&nbsp;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$WMI</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$srv</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$serviceArray</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$service</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`t</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$service</span>.state
&nbsp;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So Cherrypick a few services that you&#8217;d like to check from each of the servers that you left the ActiveInd to 1. I&#8217;m a DBA so if I had to pick two I&#8217;ll use MSSQLSERVER and SQLSERVERAGENT.</p>
<p>So let&#8217;s get the ServerIDs of our testing servers by using the following script:</p>

<div class="wp_syntax"><div class="code"><pre class="t-sql" style="font-family:monospace;">SELECT	ServerID, ServerName
FROM	ServerList
WHERE	ActiveInd = 1</pre></div></div>

<p>OK for my example my ServerIDs are 70, 79, and 363.</p>
<p>Now we&#8217;re going to open the ServiecCheck Table and populate 2 rows per test server&#8230; something like this:</p>
<p>ServerID: 70<br />
ServiceName: MSSQLSERVER<br />
ServiceType: SERVICE</p>
<p>ServerID: 70<br />
ServiceName: SQLSERVERAGENT<br />
ServiceType: SERVICE</p>
<p>ServerID: 79<br />
ServiceName: MSSQL$DEVE<br />
ServiceType: SERVICE</p>
<p>ServerID: 79<br />
ServiceName: SQLAGENT$DEVE<br />
ServiceType: SERVICE</p>
<p>ServerID: 363<br />
ServiceName: MSSQLSERVER<br />
ServiceType: SERVICE</p>
<p>ServerID: 363<br />
ServiceName: SQLSERVERAGENT<br />
ServiceType: SERVICE</p>
<p>You&#8217;ll notice that my ServerID 79 is a named instance so the SQL Server service and the SQL Server Agent service are named a bit differently. You&#8217;ll also notice that I&#8217;ve populated the ServiceType filed with &#8220;Service&#8221; this is to distinguish between services and websites. You&#8217;ll see what I do with this field later.</p>
<p>Let&#8217;s throw in a website availability checks as well.</p>
<p>ServerID: 0<br />
ServiceName: http://BenchmarkITConsulting.com<br />
ServiceType: WEBSITE<br />
Notes: *Colin Stasiuk*</p>
<p>OK so a little different setup then the service check. There is no &#8220;server&#8221; to check on a website availability check so I set the ServerID to 0. The service name is the http:// address of the site you&#8217;re looking to check. The service type is &#8220;WEBSITE&#8221; and the Notes column for a website is used to hold the search criteria for what the check looks for on the page to validate it&#8217;s availability.</p>
<p>Now we&#8217;re going to create a couple views to filter out the services and servers that we want to check by using the following script:</p>

<div class="wp_syntax"><div class="code"><pre class="t-sql" style="font-family:monospace;">CREATE VIEW [dbo].[vwServerListActive] AS
SELECT	ServerID, ServerName
FROM	dbo.ServerList
WHERE	ActiveInd = 1
GO
CREATE VIEW [dbo].[vwActiveServiceChecks] AS
SELECT	A.ServiceCheckID, A.ServerID, B.ServerName, A.ServiceName, A.ActiveInd, A.CreateDateTime, A.LastModDateTime
FROM	dbo.ServiceCheck A INNER JOIN
		dbo.vwServerListActive B ON A.ServerID = B.ServerID
WHERE	A.ActiveInd = 1 AND
		A.ServiceType = 'SERVICE'
GO
CREATE VIEW [dbo].[vwActiveServiceChecks_Websites] AS
SELECT	A.ServiceCheckID, A.ServiceName, A.Notes, A.ActiveInd, A.CreateDateTime, A.LastModDateTime
FROM	dbo.ServiceCheck A
WHERE	A.ActiveInd = 1 AND
		A.ServiceType = 'WEBSITE'</pre></div></div>

<p>OK finally we&#8217;re going to get to the &#8220;bacon&#8221; of this blog post&#8230;. the more I think I should of done a multi-part post but if you&#8217;ve stuck with me this long we&#8217;ve just got one more script to go&#8230;</p>
<p>So here we go&#8230; This is the script that you want to schedule to run as often as you feel necessary (I have mine running every 5 minutes)&#8230; first I&#8217;ll show the script and then break it down a bit.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$Server</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;MySQL2008Server&quot;</span>
<span style="color: #800080;">$Database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;MonitoringDB&quot;</span>
&nbsp;
<span style="color: #800080;">$emailFrom</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;blah@blah.com&quot;</span>
<span style="color: #800080;">$emailTo</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;DBA@blah.com&quot;</span>
<span style="color: #800080;">$subject</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Service Check Failure&quot;</span>
<span style="color: #800080;">$smtpServer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SMTPServer.blah.com&quot;</span>
&nbsp;
<span style="color: #800080;">$FailureCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
&nbsp;
<span style="color: #008000;">###################		WINDOWS SERVICE CHECKS	(start)		###################</span>
&nbsp;
<span style="color: #800080;">$con</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;server=$Server;database=$Database;Integrated Security=sspi&quot;</span>
<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT DISTINCT ServerName FROM dbo.vwActiveServiceChecks&quot;</span>
&nbsp;
  <span style="color: #800080;">$da</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlDataAdapter <span style="color: #000000;">&#40;</span><span style="color: #800080;">$cmd</span><span style="color: pink;">,</span> <span style="color: #800080;">$con</span><span style="color: #000000;">&#41;</span>
&nbsp;
  <span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.DataTable
&nbsp;
  <span style="color: #800080;">$da</span>.fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dt</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out-null</span>
&nbsp;
  <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$srv</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$dt</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$ServerName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.ServerName
&nbsp;
	<span style="color: #800080;">$ping</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Net.NetworkInformation.Ping
	<span style="color: #800080;">$Reply</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ping</span>.send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$ServerName</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Reply</span>.status –eq “Success”<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$cmd2</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT ServiceCheckID, ServiceName FROM dbo.vwActiveServiceChecks WHERE ServerName = '$ServerName'&quot;</span>
		<span style="color: #800080;">$da2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlDataAdapter <span style="color: #000000;">&#40;</span><span style="color: #800080;">$cmd2</span><span style="color: pink;">,</span> <span style="color: #800080;">$con</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$dt2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.DataTable
		<span style="color: #800080;">$da2</span>.fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dt2</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out-null</span>
&nbsp;
		<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$svc</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$dt2</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #800080;">$ServiceCheckID</span> <span style="color: pink;">=</span> <span style="color: #800080;">$svc</span>.ServiceCheckID
			<span style="color: #800080;">$ServiceName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$svc</span>.ServiceName
&nbsp;
			<span style="color: #800080;">$colOS</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">get-wmiobject</span> win32_service <span style="color: pink;">-</span><span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;name='$ServiceName'&quot;</span> <span style="color: #008080; font-style: italic;">-computerName</span> <span style="color: #800080;">$ServerName</span> 
&nbsp;
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$colos</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$objComp</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$colOS</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$State</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objComp</span>.State
&nbsp;
					<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objComp</span>.State <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;Running&quot;</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;INSERT INTO dbo.ServiceCheckHistoryAudit(ServiceCheckID, ServiceState) SELECT '$ServiceCheckID', '1'&quot;</span>
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0000FF;">else</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;INSERT INTO dbo.ServiceCheckHistoryAudit(ServiceCheckID, ServiceState) SELECT '$ServiceCheckID', '0'&quot;</span>
					        <span style="color: #800080;">$body</span> <span style="color: pink;">=</span> <span style="color: #800080;">$body</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`r</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Service Check Failure: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServerName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; (&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServiceName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;)&quot;</span>
						<span style="color: #800080;">$FailureCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">1</span>
					<span style="color: #000000;">&#125;</span>
					invoke<span style="color: pink;">-</span>sqlcmd <span style="color: pink;">-</span>serverinstance <span style="color: #800080;">$Server</span> <span style="color: pink;">-</span>database <span style="color: #800080;">$Database</span> <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800080;">$sql</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$FailureCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">1</span>
				<span style="color: #800080;">$Body</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Body</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`r</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Bad Service Name: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServerName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; (&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServiceName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;)&quot;</span>	
&nbsp;
				<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;INSERT INTO dbo.ServiceCheckHistoryAudit(ServiceCheckID, ServiceState) SELECT '$ServiceCheckID', '0'&quot;</span>
				invoke<span style="color: pink;">-</span>sqlcmd <span style="color: pink;">-</span>serverinstance <span style="color: #800080;">$Server</span> <span style="color: pink;">-</span>database <span style="color: #800080;">$Database</span> <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800080;">$sql</span>		
&nbsp;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$FailureCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">1</span>
		<span style="color: #800080;">$Body</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Body</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`r</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Failed Ping: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServerName</span> 
&nbsp;
		<span style="color: #800080;">$cmd3</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT ServiceCheckID, ServiceName FROM dbo.vwActiveServiceChecks WHERE ServerName = '$ServerName'&quot;</span>
		<span style="color: #800080;">$da3</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlDataAdapter <span style="color: #000000;">&#40;</span><span style="color: #800080;">$cmd3</span><span style="color: pink;">,</span> <span style="color: #800080;">$con</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$dt3</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.DataTable
		<span style="color: #800080;">$da3</span>.fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dt3</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out-null</span>
&nbsp;
		<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$svc2</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$dt3</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$ServiceCheckID</span> <span style="color: pink;">=</span> <span style="color: #800080;">$svc2</span>.ServiceCheckID
			<span style="color: #800080;">$ServiceName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$svc2</span>.ServiceName
			<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;INSERT INTO dbo.ServiceCheckHistoryAudit(ServiceCheckID, ServiceState) SELECT '$ServiceCheckID', '0'&quot;</span>
			invoke<span style="color: pink;">-</span>sqlcmd <span style="color: pink;">-</span>serverinstance <span style="color: #800080;">$Server</span> <span style="color: pink;">-</span>database <span style="color: #800080;">$Database</span> <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800080;">$sql</span>	
&nbsp;
			<span style="color: #800080;">$FailureCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">1</span>
			<span style="color: #800080;">$body</span> <span style="color: pink;">=</span> <span style="color: #800080;">$body</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`r</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Service Check Failure: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServerName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; (&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServiceName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;)&quot;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #800080;">$Reply</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>
&nbsp;
  <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">###################		WINDOWS SERVICE CHECKS	(End)		###################</span>
&nbsp;
<span style="color: #008000;">###################		WEBSITE CHECKS	(start)			###################</span>
&nbsp;
<span style="color: #800080;">$conWeb</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;server=$Server;database=$Database;Integrated Security=sspi&quot;</span>
<span style="color: #800080;">$cmdWeb</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT ServiceCheckID, ServiceName, Notes FROM dbo.vwActiveServiceChecks_Websites&quot;</span>
&nbsp;
<span style="color: #800080;">$daWeb</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlDataAdapter <span style="color: #000000;">&#40;</span><span style="color: #800080;">$cmdWeb</span><span style="color: pink;">,</span> <span style="color: #800080;">$conWeb</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$dtWeb</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.DataTable
<span style="color: #800080;">$daWeb</span>.fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dtWeb</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out-null</span>
&nbsp;
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Website</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$dtWeb</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$ServiceCheckID</span> <span style="color: pink;">=</span> <span style="color: #800080;">$WebSite</span>.ServiceCheckID
	<span style="color: #800080;">$ServiceName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Website</span>.ServiceName
	<span style="color: #800080;">$Notes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Website</span>.Notes
&nbsp;
	<span style="color: #0000FF;">trap</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">continue</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #800080;">$webclient</span><span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Net.WebClient
	<span style="color: #800080;">$webclient</span>.Credentials <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Net.CredentialCache</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">DefaultCredentials</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$webclient</span>.Proxy <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
        	<span style="color: #800080;">$webclient</span>.Proxy.Credentials <span style="color: pink;">=</span>
                <span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Net.CredentialCache</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">DefaultNetworkCredentials</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #800080;">$Output</span> <span style="color: pink;">=</span> <span style="color: #800080;">$webclient</span>.DownloadString<span style="color: #000000;">&#40;</span><span style="color: #800080;">$ServiceName</span><span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #0000FF;">IF</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Output</span> <span style="color: #FF0000;">-like</span> <span style="color: #800080;">$Notes</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;INSERT INTO dbo.ServiceCheckHistoryAudit(ServiceCheckID, ServiceState) SELECT '$ServiceCheckID', '1'&quot;</span>
			invoke<span style="color: pink;">-</span>sqlcmd <span style="color: pink;">-</span>serverinstance <span style="color: #800080;">$Server</span> <span style="color: pink;">-</span>database <span style="color: #800080;">$Database</span> <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800080;">$sql</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">ELSE</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$FailureCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">1</span>
			<span style="color: #800080;">$Body</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Body</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`r</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Bad Website or Search String: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ServiceName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; (&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Notes</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;)&quot;</span>
&nbsp;
			<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;INSERT INTO dbo.ServiceCheckHistoryAudit(ServiceCheckID, ServiceState) SELECT '$ServiceCheckID', '0'&quot;</span>
			invoke<span style="color: pink;">-</span>sqlcmd <span style="color: pink;">-</span>serverinstance <span style="color: #800080;">$Server</span> <span style="color: pink;">-</span>database <span style="color: #800080;">$Database</span> <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800080;">$sql</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #800080;">$Output</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">###################		WEBSITE CHECKS	(End)			###################</span>
&nbsp;
<span style="color: #008000;">###################		MAIL	(start)			###################</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$FailureCount</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;1&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$smtp</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Net.Mail.SmtpClient<span style="color: #000000;">&#40;</span><span style="color: #800080;">$smtpServer</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$smtp</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$emailFrom</span><span style="color: pink;">,</span><span style="color: #800080;">$emailTo</span><span style="color: pink;">,</span><span style="color: #800080;">$subject</span><span style="color: pink;">,</span><span style="color: #800080;">$body</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">###################		MAIL	(End)			###################</span></pre></div></div>

<p>OK let&#8217;s step through this a bit to see what we&#8217;re doing&#8230;</p>
<p>*** There are some variables at the top that will need to be populated with information from your environment ***</p>
<ul>
<li>First we connect to the database and get the list of all servers that have services that need to be checked</li>
<li>For each of those severs we first ping to see if we can even connect to the server</li>
<li>If we can&#8217;t ping then for all the services for that server are marked as failed and the body of our failure email is appended to</li>
<li>If we can ping then we pull the services to check for that server</li>
<li>For each service in that list we check the status&#8230; if it&#8217;s running we audit accordingly if it&#8217;s not then we audit accordingly and the body of our failure email is appended to</li>
<li>Next&#8230; For each website we look at the source and compare it to our search criteria in the notes column</li>
<li>If it can find the search criteria we audit accordingly&#8230; if it can&#8217;t find the search string or can&#8217;t connect at all then we audit accordingly and the body of our failure email is appended to</li>
<li>Finally after everything is done the failure email (if there are any failures) get sent out.</li>
</ul>
<p>A quick SELECT from the ServiceCheckHistoryAudit table will give you a look as to what passed and what failed by the ServiceState column.</p>
<p>I setup my job to run every 5 minutes and I&#8217;m currently looking at over 200 services and websites and my average duration of my job is 1 minute and 20 seconds (not bad in my opinion)&#8230;. anyways this is very much a v1 of this script/process and I&#8217;m hoping that people can not only find a good use for it but also look to take the script and add to it, optimize it, etc.  I&#8217;m still a rookie with PowerShell so any and all help and suggestions would be greatly appreciated</p>
<p>EDIT:  I also have a stored proc that runs monthly to &#8220;clean up&#8221; the data on days when a service has a clean record for the full 24 hour period&#8230; but I&#8217;ve already taken up enough of your time so we&#8217;ll save that for another post another day LOL</p>
<p>Enjoy!!</p>
<p><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Monitoring+With+Powershell+%28pings%2C+services%2C+and+websites%29+http://tinyurl.com/ydesucp" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/03/25/monitoring-with-powershell-pings-services-and-websites/&amp;title=Monitoring+With+Powershell+%28pings%2C+services%2C+and+websites%29" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/03/25/monitoring-with-powershell-pings-services-and-websites/&amp;title=Monitoring+With+Powershell+%28pings%2C+services%2C+and+websites%29" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/03/25/monitoring-with-powershell-pings-services-and-websites/&amp;title=Monitoring+With+Powershell+%28pings%2C+services%2C+and+websites%29" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2010/03/25/monitoring-with-powershell-pings-services-and-websites/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Rebooting a Server Using PowerShell</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2010/02/01/rebooting-a-server-using-powershell/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2010/02/01/rebooting-a-server-using-powershell/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 17:43:05 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[Reboot a Server]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=1337</guid>
		<description><![CDATA[The more I work with PowerShell the more I love it So what do you do if you&#8217;re in a VM environment and you want/need to reboot your server but you can&#8217;t connect via RDP and you can&#8217;t connect to the console using the VM Infrastructure Client, and all other attempts to connect just don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>The more I work with PowerShell the more I love it <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img class="alignnone size-full wp-image-1338" title="love4" src="http://benchmarkitconsulting.com/wp-content/uploads/2010/02/love4.jpg" alt="love4" width="180" height="180" /></p>
<p>So what do you do if you&#8217;re in a VM environment and you want/need to reboot your server but you can&#8217;t connect via RDP and you can&#8217;t connect to the console using the VM Infrastructure Client, and all other attempts to connect just don&#8217;t work.  I decided to go down the route of using PowerShell.</p>
<p>$ServerToRestart = Get-WMIObject win32_operatingsystem -computer <strong>&lt;&lt;Your Server Name Here&gt;&gt;</strong><br />
$ServerToRestart.reboot()</p>
<p>Get-WMIObject &#8211; <a href="http://technet.microsoft.com/en-us/magazine/2007.05.powershell.aspx?pr=blog" target="_blank">Windows PowerShell: The WMI Connection</a></p>
<p>Simple, easy and very dangerous/powerful.  I&#8217;ll channel my inner &#8220;Uncle Ben&#8221; (see Spiderman quotes) and just leave this message as a closing thought:</p>
<p>&#8220;With Great Power Comes Great Responsibility&#8221;</p>
<p>Enjoy!!</p>
<p><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Rebooting+a+Server+Using+PowerShell+http://tinyurl.com/yzrc5d5" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/02/01/rebooting-a-server-using-powershell/&amp;title=Rebooting+a+Server+Using+PowerShell" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/02/01/rebooting-a-server-using-powershell/&amp;title=Rebooting+a+Server+Using+PowerShell" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2010/02/01/rebooting-a-server-using-powershell/&amp;title=Rebooting+a+Server+Using+PowerShell" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2010/02/01/rebooting-a-server-using-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fail Whale for SSMS and CMS for PBM using PS</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/09/22/failwhaleforssmsandcms/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/09/22/failwhaleforssmsandcms/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 21:56:50 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[PBM]]></category>
		<category><![CDATA[Policy Based Management]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[PS]]></category>
		<category><![CDATA[SQL Server Management Studio]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=1126</guid>
		<description><![CDATA[Translation: Fail Whale for SQL Server Management Studio and Central Management Server for Policy Based Managment using PowerShell&#8230;. lol simple eh? So this is a Microsoft Connect Item in the making&#8230; I had created a PowerShell script to run a bunch of policies against a list of servers that I defined in a ServerList.txt file.  [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-1127" title="fwwwwww" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/09/fwwwwww.png" alt="fwwwwww" width="240" height="180" /></p>
<p>Translation: Fail Whale for SQL Server Management Studio and Central Management Server for Policy Based Managment using PowerShell&#8230;. lol simple eh?</p>
<p>So this is a Microsoft Connect Item in the making&#8230;</p>
<p>I had created a PowerShell script to run a bunch of policies against a list of servers that I defined in a ServerList.txt file.  I decided that I didn&#8217;t like the idea of needing to maintain a text file and instead of storing the Instance Names in a table somewhere I decided to hook into the Central Managment Server.</p>
<p>So I logged into the server that is currently running the SQL Server Agent job that was doing all this using a ServerList.txt file.  I changed the PowerShell Script to point to my CMS server instead and ran the .ps1 directly on the server using &#8220;sqlps F:\PBM\EvaluatePolicies.ps1&#8243;.  The script ran as expected, used my CMS server, and everything was tickidyboo.  Or so I thought&#8230;.</p>
<p>I decided to do my final check and run the SQL Server Agent job itself&#8230; and it failed.</p>
<p><img class="alignnone size-full wp-image-1129" title="toslkdsl" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/09/toslkdsl.jpg" alt="toslkdsl" width="168" height="132" /></p>
<p>Hmmmm that&#8217;s weird&#8230; so I confirmed that the SQL Server Agent account had access to the CMS server and even granted it explicit sysadmin rights (just for my troubleshooting).</p>
<p><strong>FAIL</strong></p>
<p>I started tweeting about the problem and got some immediate advice from @<a href="http://twitter.com/afernandez" target="_blank">afernandez</a>, <a href="http://twitter.com/russjohnson">@russjohnson</a>, <a href="http://twitter.com/wendy_dance">@wendy_dance</a>, and <a href="http://twitter.com/sqlchicken">@SQLChicken</a>.  While none of the advice solved my problem it was great to have some other &#8220;eyes&#8221; reading my problem and confirming the stuff that I had already thought of and tested.</p>
<p>The SQL Server Agent Service Account is a secure account that I don&#8217;t even have access or the password for so I asked for some help from one of our Server Admins for my troubleshooting&#8230; he logged into the server for me and I tried to run the ps1 file&#8230;.</p>
<p><strong>FAIL</strong></p>
<p>So I opened up SSMS and checked if I could connect to the CMS directly&#8230;.</p>
<p><strong><span style="text-decoration: underline;">SOLUTION:  </span></strong>In order to use a PowerShell script to connect to the Central Management Server using the SQL Server Agent the service account that SQL Server Agent is running under must REGISTER the CMS Server through SSMS on the Server that is running the job.</p>
<p>Did you get all that&#8230;. because I had never Remote Desktopped onto the Server using the SQL Server Agent service account and Registered the CMS Server in SSMS using that account when I tried to access the CMS Server through my PowerShell script it failed.</p>
<p>I registered the CMS Server using the SQL Server Agent Service Account and everything started sizzling like bacon on a hot pan.</p>
<p>Enjoy!!</p>
<p><span><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></span></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Fail+Whale+for+SSMS+and+CMS+for+PBM+using+PS+http://tinyurl.com/npkg83" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/09/22/failwhaleforssmsandcms/&amp;title=Fail+Whale+for+SSMS+and+CMS+for+PBM+using+PS" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/09/22/failwhaleforssmsandcms/&amp;title=Fail+Whale+for+SSMS+and+CMS+for+PBM+using+PS" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/09/22/failwhaleforssmsandcms/&amp;title=Fail+Whale+for+SSMS+and+CMS+for+PBM+using+PS" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/09/22/failwhaleforssmsandcms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Bacon Nugget of Excellence Award @SQLGal</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/08/26/sql-bacon-nugget-of-excellence-award-sqlgal/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/08/26/sql-bacon-nugget-of-excellence-award-sqlgal/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 13:31:17 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[2008]]></category>
		<category><![CDATA[Evaluate Policies]]></category>
		<category><![CDATA[PBM]]></category>
		<category><![CDATA[Policy Based Management]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[PS]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=970</guid>
		<description><![CDATA[OK so the last time I handed out a SQL Bacon Nugget of Excellence Award was July 8th for @SQLAgentMan &#8216;s article on Determining Free Space, Consumed Space, and Total Space Allocated for Microsoft SQL Server Databases and this time around @SQLGal has put something together that can only be described as a SQL Bacon Nugget [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-971" title="BaNug" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/08/BaNug.jpg" alt="BaNug" width="180" height="135" /></p>
<p>OK so the last time I handed out a SQL Bacon Nugget of Excellence Award was July 8th for <a href="http://twitter.com/sqlagentman" target="_blank">@SQLAgentMan</a> &#8216;s article on <a href="http://www.mssqltips.com/tip.asp?tip=1629" target="_blank">Determining Free Space, Consumed Space, and Total Space Allocated for Microsoft SQL Server Databases</a></p>
<p>and this time around <a href="http://twitter.com/sqlgal" target="_blank">@SQLGal</a> has put something together that can only be described as a SQL Bacon Nugget of Excellence.  So a little background story on this one&#8230; I&#8217;ve been doing more with Policy Based Management lately and coincidently more with PowerShell as well.  So I started playing around with having an automated policy checker run in the night via PowerShell and email me the &#8220;non conformists&#8221; <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I was almost done my initial release when I all of a sudden found that if I ran the powershell script from the server directly that I couldn&#8217;t connect to some of my servers (SQL 2000).  I re-ran&#8230; nothing&#8230; tried a few different ideas&#8230; no dice.  So as I like to do from time to time if my &#8220;googling&#8221; doesn&#8217;t help much is I posed the question to the Twitterverse.</p>
<p>As always (cause Twitter is just great for community support) I got a couple replies and offers of assistance.  One reply was from <a href="http://twitter.com/timbo_b_edwards" target="_blank">@Timbo_B_Edwards</a>.  His tweet pointed me to a site that suprisingly (with my googling on Policy Based Management and Powershell) I didn&#8217;t find before.</p>
<p><a href="http://epmframework.codeplex.com/" target="_blank">Enterprise Policy Management Framework</a></p>
<p>This offering from <a href="http://twitter.com/sqlgal" target="_blank">@SQLGal</a> puts my ragamuffin first attempt at Policy Based Management via Powershell to shame.</p>
<p>The Enterprise Policy Managment Framework uses PowerShell to evaluate policies against your SQL Server Environment, shreds the XML for you, and then renders and delivers your policy history reports via Reporting Services.</p>
<p><img class="alignnone size-full wp-image-974" title="aaswsdw" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/08/aaswsdw.png" alt="aaswsdw" width="155" height="136" /></p>
<p> </p>
<p>So for the love of all things good and pure in this world&#8230; if you have SQL Server 2008 (and if you don&#8217;t this is one heck of a good reason to start) this is something you <strong><span style="text-decoration: underline;">HAVE TO</span></strong> check out and spend some quality time with. </p>
<p><strong><em>Lara Rubbelke</em></strong></p>
<ul>
<li><a href="http://sqlblog.com/blogs/lara_rubbelke/default.aspx" target="_blank">Laura on SQLBlog.com</a></li>
<li><a href="http://twitter.com/sqlgal" target="_blank">@SQLGal on Twitter</a></li>
<li><a href="http://epmframework.codeplex.com/" target="_blank">Enterprise Policy Management Framework</a></li>
</ul>
<p>P.S. I found the solution to my problem on the top of the Enterprise Policy Management Framework page&#8230;</p>
<p><em>&#8220;Update on SP1: You must install SQL Server 2008 SP1 Cumulative Update 3 (</em><a href="http://support.microsoft.com/kb/971491" target="_blank"><em>http://support.microsoft.com/kb/971491</em></a><em>) if you have installed SP1 on the Central Management Server. SP1 Cumulative Update 3 will fix the SP1 bug which prevented down version policy evaluation.&#8221;</em></p>
<p>Enjoy!!</p>
<p><span><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></span></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=SQL+Bacon+Nugget+of+Excellence+Award+%40SQLGal+http://tinyurl.com/lf6pgo" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/08/26/sql-bacon-nugget-of-excellence-award-sqlgal/&amp;title=SQL+Bacon+Nugget+of+Excellence+Award+%40SQLGal" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/08/26/sql-bacon-nugget-of-excellence-award-sqlgal/&amp;title=SQL+Bacon+Nugget+of+Excellence+Award+%40SQLGal" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/08/26/sql-bacon-nugget-of-excellence-award-sqlgal/&amp;title=SQL+Bacon+Nugget+of+Excellence+Award+%40SQLGal" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/08/26/sql-bacon-nugget-of-excellence-award-sqlgal/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>xp_delete_file vs. PowerShell</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/08/14/xp_delete_file-vs-powershell/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/08/14/xp_delete_file-vs-powershell/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 18:56:15 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[delete files]]></category>
		<category><![CDATA[maintenance clean up]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=930</guid>
		<description><![CDATA[So I&#8217;m a firm believer in the KISS theory. No not THAT KISS&#8230; the Keep It Simple Stupid theory. So when it comes to backups&#8230; if I&#8217;m not needing to &#8220;trick them out&#8221; or do anything custom I don&#8217;t have any problems with setting up a nice and easy maintenance plan and calling it a [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m a firm believer in the KISS theory.</p>
<p><img class="alignnone size-full wp-image-931" title="auyauh" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/08/auyauh.jpg" alt="auyauh" width="196" height="184" /></p>
<p>No not THAT KISS&#8230; the Keep It Simple Stupid theory.</p>
<p>So when it comes to backups&#8230; if I&#8217;m not needing to &#8220;trick them out&#8221; or do anything custom I don&#8217;t have any problems with setting up a nice and easy maintenance plan and calling it a day.  I know there are some strong opinions out there that you should avoid Maintenance Plans like the plague and I agree that in some situations that they are not your best option but I don&#8217;t like to write them off altogether.</p>
<p>Sorry&#8230; that went longer then expected&#8230;ok back on topic&#8230;</p>
<p><strong>xp_delete_file</strong>&#8230; So I needed to build a custom backup job because with the size of the database I would get greater performance by backing up to multiple backup files (an option that is not available in a maintenance plan).  Now normally I would just attach the maintenance cleanup task to another maintenance plan but I thought &#8220;Hey&#8230; let&#8217;s get crazy&#8221; I&#8217;m going to click on that View T-SQL Button and see what happens.</p>
<p><img class="alignnone size-full wp-image-932" title="fdfdss" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/08/fdfdss.JPG" alt="fdfdss" width="287" height="334" /></p>
<p>Well isn&#8217;t that easy?</p>
<p><code style="font-size: 12px;"><span style="color:blue">EXECUTE&nbsp;</span><span style="color:black">master.dbo.</span><span style="color:darkred">xp_delete_file&nbsp;</span><span style="color:black">0</span><span style="color:gray">,</span><span style="color:red">N&#39;\\ServerName\Folder&#39;</span><span style="color:gray">,</span><span style="color:red">N&#39;BAK&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;2009-08-13T08:14:17&#39;</span><span style="color:gray">,</span><span style="color:black">1</span></code></p>
<p>Could it REALLY be that simple?</p>
<p>Hey&#8230; what&#8217;s with that &#8220;0&#8243; at the front and the &#8220;1&#8243; at the end?  What are those parameters for?</p>
<p>I BINGed the question (OK I really GOOGLEd it&#8230; sorry Microsoft I just can&#8217;t break my google ties) and found that the first arguement determines if it&#8217;s a backup file or a report file (0 and 1 respectively) and the last arguement determines whether or not to include first level subfolders (0 &#8211; no 1 &#8211; yes)</p>
<p>Great&#8230; it looks like I&#8217;ve found my solution&#8230;.. but wait for it&#8230;&#8230; wait for it&#8230;&#8230;..</p>
<p>ENTER PowerShell&#8230;.</p>
<p> <img class="alignnone size-full wp-image-934" title="pssss_super" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/08/pssss_super.jpg" alt="pssss_super" width="160" height="173" /></p>
<p>(Great picture BTW by <a href="http://blog.theaccidentalgeek.com/post/2007/06/28/PowerShell-to-the-rescue.aspx" target="_blank">Joe Brinkman &#8211; The Accidental Geek</a> )</p>
<p>OK admittedly I&#8217;m one of the 42% on <a href="http://www.brentozar.com/archive/2009/07/july-2009-powershell-poll/" target="_blank">Brent Ozar&#8217;s Powershell Poll </a>that don&#8217;t currently use PowerShell but want to.  So I thought here is my chance&#8230;</p>
<p> I started out simple:</p>
<p>Built myself a nice little .ps1 file with this:</p>
<p><span style="color: #0000ff;">$directory = &#8220;C:\&#8221;<br />
Get-ChildItem $directory</span></p>
<p>Ran that bad boy and low and behold&#8230; I&#8217;m a PowerShell developer <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So I thought&#8230; let&#8217;s add my network share location for my backup files:</p>
<p><span style="color: #0000ff;">$directory = &#8220;\\servername\folder&#8221;<br />
Get-ChildItem $directory</span></p>
<p>Ran that and I&#8217;m back to being a PowerShell failure:</p>
<p><span style="color: #ff0000;">Get-ChildItem : Cannot find path &#8216;\\servername\folder&#8217; because it does not exist.</span></p>
<p>Turns out you need to map a drive for a network share (or at least that&#8217;s how I solved my problem&#8230; please comment if there is a more elegant solution):</p>
<p><span style="color: #0000ff;">$directory = &#8220;\\ServerName\Folder&#8221;<br />
New-PSDrive -name X -psprovider FileSystem -root $directory<br />
Get-ChildItem X:\</span></p>
<p> Now we&#8217;re cooking with fire <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Next I changed my Get-ChildItem statement to filter only the *.BAK files and I set it to recursively go through subdirectories:</p>
<p><span style="color: #0000ff;">$directory = &#8220;\\ServerName\Folder&#8221;<br />
New-PSDrive -name X -psprovider FileSystem -root $directory<br />
Get-ChildItem X:\ -include *.BAK -recurse</span></p>
<p><span style="color: #000000;">This is getting good now <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Now I wanted to only get the *.BAK files that are older then 4 hours&#8230; this took some googling to get my syntax right:</span></p>
<p><span style="color: #0000ff;">$directory = &#8220;\\ServerName\Folder&#8221;<br />
New-PSDrive -name X -psprovider FileSystem -root $directory<br />
Get-ChildItem X:\ -include *.BAK -recurse | where {((get-date)-$_.creationTime).hours -ge 4}</span></p>
<p> And finally I wanted to delete these files so we &#8220;pipe in&#8221; a remove-item and we&#8217;re done:</p>
<p><span style="color: #0000ff;">$directory = &#8220;\\ServerName\Folder&#8221;<br />
New-PSDrive -name X -psprovider FileSystem -root $directory<br />
Get-ChildItem X:\ -include *.BAK -recurse | where {((get-date)-$_.creationTime).hours -ge 4} | remove-item -force -recurse</span></p>
<p>And if you wanted to add your transaction log backups as well you could copy/paste your Get-ChildItem and change accordingly</p>
<p><span style="color: #0000ff;">Get-ChildItem X:\ -include *.TRN -recurse | where {((get-date)-$_.creationTime).days -ge 1.5} | remove-item -force -recurse</span></p>
<p>And there you go&#8230; I&#8217;m not sure if this means I&#8217;m now a PowerShell user but I&#8217;m definitely starting to drink the KoolAid for sure.</p>
<p>Enjoy!!</p>
<p><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=xp_delete_file+vs.+PowerShell+http://tinyurl.com/or8h3k" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/08/14/xp_delete_file-vs-powershell/&amp;title=xp_delete_file+vs.+PowerShell" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/08/14/xp_delete_file-vs-powershell/&amp;title=xp_delete_file+vs.+PowerShell" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/08/14/xp_delete_file-vs-powershell/&amp;title=xp_delete_file+vs.+PowerShell" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/08/14/xp_delete_file-vs-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

