<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">

<channel>
	<title>Sev17</title>
	
	<link>http://sev17.com</link>
	<description>SQL Server, PowerShell and so on</description>
	<lastBuildDate>Fri, 27 Apr 2012 16:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Sev17" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="sev17" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Appending New Rows Only</title>
		<link>http://sev17.com/2012/04/appending-new-rows-only/</link>
		<comments>http://sev17.com/2012/04/appending-new-rows-only/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 16:06:48 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[TVP]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10914</guid>
		<description><![CDATA[I saw a question in the forums related to inserting new rows into a SQL Server table only if they didn’t exist. The current solution was using an ADO.NET DataTable , checking for new rows and then pushing the rows back to SQL Server by calling the Update method on the DataAdapter. Although the solution...]]></description>
			<content:encoded><![CDATA[<p>I saw <a href="http://stackoverflow.com/questions/10323767/proper-usage-of-data-tables" target="_blank">a question in the forums</a> related to inserting new rows into a SQL Server table only if they didn’t exist. The current solution was using an ADO.NET DataTable , checking for new rows and then pushing the rows back to SQL Server by calling the Update method on the DataAdapter. Although the solution works, the process becomes longer as each time the process is run the entire table is retrieved and compared.</p>
<p>There’s a number of approaches you could take to solve this problem. One solution is to use <a href="http://sev17.com/2012/04/table-valued-parameter-example/" target="_blank">Table Valued Parameters which I’ve previously blogged</a> about to push a batch of rows to SQL Server and add only new rows. This does require creating both a table type and stored procedure on the SQL Server and only works for SQL Server 2008 and higher:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">&lt;#
/* FROM SSMS */
USE tempdb
GO
/* Create a Request table for testing purposes*/
CREATE TABLE Request
( PKID INT,
 MessageText varchar(max));
GO
/* Create a RequestList table type */
CREATE TYPE RequestList AS TABLE
( PKID INT,
 MessageText varchar(max));
GO
&nbsp;
/* Create a procedure to use insert only new rows  */
CREATE PROCEDURE uspSetRequest
    @TVP RequestList READONLY
    AS
    SET NOCOUNT ON
    INSERT Request
    SELECT tvp.PKID, tvp.MessageText
    FROM @TVP tvp
    LEFT JOIN Request r ON
    tvp.PKID = r.PKID
    WHERE r.PKID IS NULL;
 GO
 #&gt;</span>
&nbsp;
<span style="color: #008000;">#FROM Powershell</span>
<span style="color: #008000;">#Create an ADO.NET DataTable matching the RequestList Table Type:</span>
<span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Data.datatable
<span style="color: #800080;">$col</span> <span style="color: pink;">=</span>  <span style="color: #008080; font-weight: bold;">new-object</span> Data.DataColumn
<span style="color: #800080;">$col</span>.ColumnName <span style="color: pink;">=</span> <span style="color: #800000;">'PKID'</span>
<span style="color: #800080;">$col</span>.DataType <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Int32<span style="color: #000000;">&#93;</span>
<span style="color: #800080;">$dt</span>.Columns.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Col</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$col</span> <span style="color: pink;">=</span>  <span style="color: #008080; font-weight: bold;">new-object</span> Data.DataColumn
<span style="color: #800080;">$col</span>.ColumnName <span style="color: pink;">=</span> <span style="color: #800000;">'MessageText'</span>
<span style="color: #800080;">$col</span>.DataType <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span>
<span style="color: #800080;">$dt</span>.Columns.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Col</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;">#BEGIN INSERT foreach Loops to add records to DataTable</span>
<span style="color: #008000;">#Example below inserts only one record</span>
<span style="color: #008000;">#Add a Row to the DataTable</span>
<span style="color: #800080;">$dr</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dt</span>.NewRow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$dr</span>.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">'PKID'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>
<span style="color: #800080;">$dr</span>.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">'MessageText'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #800000;">'It worked!'</span>
<span style="color: #800080;">$dt</span>.Rows.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dr</span><span style="color: #000000;">&#41;</span>
<span style="color: #008000;">#END INSERT foreach Loops to add records to DataTable </span>
&nbsp;
<span style="color: #008000;">#Connection and Query Info</span>
<span style="color: #800080;">$serverName</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;$env:computername\sql1&quot;</span>
<span style="color: #800080;">$databaseName</span><span style="color: pink;">=</span><span style="color: #800000;">'tempdb'</span>
<span style="color: #800080;">$query</span><span style="color: pink;">=</span><span style="color: #800000;">'uspSetRequest'</span> 
&nbsp;
<span style="color: #008000;">#Connect</span>
<span style="color: #800080;">$connString</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Server=$serverName;Database=$databaseName;Integrated Security=SSPI;&quot;</span>
<span style="color: #800080;">$conn</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlConnection <span style="color: #800080;">$connString</span>
<span style="color: #800080;">$conn</span>.Open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;">#Create Sqlcommand type and params</span>
<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlCommand
<span style="color: #800080;">$cmd</span>.Connection <span style="color: pink;">=</span> <span style="color: #800080;">$conn</span>
<span style="color: #800080;">$cmd</span>.CommandType <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Data.CommandType<span style="color: #000000;">&#93;</span><span style="color: #800000;">&quot;StoredProcedure&quot;</span>
<span style="color: #800080;">$cmd</span>.CommandText<span style="color: pink;">=</span> <span style="color: #800080;">$query</span>
<span style="color: #800080;">$null</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cmd</span>.Parameters.Add<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;@TVP&quot;</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>System.Data.SqlDbType<span style="color: #000000;">&#93;</span>::Structured<span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cmd</span>.Parameters<span style="color: #000000;">&#91;</span><span style="color: #800000;">&quot;@TVP&quot;</span><span style="color: #000000;">&#93;</span>.Value <span style="color: pink;">=</span> <span style="color: #800080;">$dt</span>
&nbsp;
<span style="color: #008000;">#Execute Query and close connection</span>
<span style="color: #800080;">$cmd</span>.ExecuteNonQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out-null</span>
<span style="color: #800080;">$conn</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/04/appending-new-rows-only/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Backup Database Object</title>
		<link>http://sev17.com/2012/04/backup-database-object/</link>
		<comments>http://sev17.com/2012/04/backup-database-object/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 22:15:37 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[URN]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10905</guid>
		<description><![CDATA[I saw this question in one of forums on backing up i.e. scripting out a database object. The problem is easy to solve, but only if you&#8217;re familiar with SMO . Even so, there some more obscure aspects of SMO like URNs which not many people are aware of. If you read the MSDN docs...]]></description>
			<content:encoded><![CDATA[<p>I saw this question in one of forums on backing up i.e. scripting out a database object. The problem is easy to solve, but only if you&#8217;re familiar with SMO <img src='http://sev17.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Even so, there some more obscure aspects of SMO like URNs which not many people are aware of. If you read the <a href="http://msdn.microsoft.com/en-us/library/ms162557.aspx" target="_blank">MSDN docs on SMO</a> you&#8217;ll find URNs are referenced in a few places. I haven&#8217;t used them much, but for this case  it makes sense. Normally if you want to get to an object in SMO you&#8217;d reference the server, then the database then the object type collection (StoredProcedures, Views, etc.), and then the object;  however if you don&#8217;t know the object type you can call EnumObject method on the database to get a list of objects with its URN. The URN is like a primary key of objects in SMO. So, here&#8217;s my solution with with comments&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;">add<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">-</span>AssemblyName <span style="color: #800000;">&quot;Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&quot;</span>
add<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">-</span>AssemblyName <span style="color: #800000;">&quot;Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&quot;</span>
add<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">-</span>AssemblyName <span style="color: #800000;">&quot;Microsoft.SqlServer.SMOExtended, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&quot;</span>
add<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">-</span>AssemblyName <span style="color: #800000;">&quot;Microsoft.SqlServer.SqlEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&quot;</span>
add<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">-</span>AssemblyName <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&quot;</span>
&nbsp;
<span style="color: #008000;">#######################</span>
<span style="color: #008000;">&lt;#
.SYNOPSIS
Backs up a database object definition.
.DESCRIPTION
The Backup-DatabaseObject function  backs up a database object definition by scripting out the object to a .sql text file.
.EXAMPLE
Backup-DatabaseObject -ServerInstance Z002 -Database AdventureWorks -Schema HumanResources -Name vEmployee -Path &quot;C:\Users\Public&quot;
This command backups up the vEmployee view to a .sql file.
.NOTES
Version History
v1.0   - Chad Miller - Initial release
#&gt;</span>
<span style="color: #0000FF;">function</span> Backup<span style="color: pink;">-</span>DatabaseObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>CmdletBinding<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ValidateNotNullorEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ServerInstance</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ValidateNotNullorEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Database</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ValidateNotNullorEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Schema</span><span style="color: pink;">,</span>
    <span style="color: #008000;">#Database Object Name</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ValidateNotNullorEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Name</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ValidateNotNullorEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Path</span>
    <span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Microsoft.SqlServer.Management.Smo.Server<span style="color: #000000;">&#40;</span><span style="color: #800080;">$ServerInstance</span><span style="color: #000000;">&#41;</span>
    <span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$server</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800080;">$Database</span><span style="color: #000000;">&#93;</span>
&nbsp;
    <span style="color: #008000;">#Create a UrnCollection. URNs are used by SMO as unique identifiers of objects. You can think of URN like primary keys</span>
    <span style="color: #008000;">#The URN format is similar to XPath</span>
    <span style="color: #800080;">$urns</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Microsoft.SqlServer.Management.Smo.UrnCollection
&nbsp;
    <span style="color: #008000;">#Get a list of database object which match the schema and object name specified</span>
    <span style="color: #008000;">#New up an URN object and add the URN to the urns collection</span>
    <span style="color: #800080;">$db</span>.enumobjects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.schema <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$Schema</span> <span style="color: #FF0000;">-and</span>  <span style="color: #000080;">$_</span>.name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$Name</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span>
        <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;">$urn</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Microsoft.SqlServer.Management.Sdk.Sfc.Urn<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.Urn<span style="color: #000000;">&#41;</span>;
                 <span style="color: #800080;">$urns</span>.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$urn</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$urns</span>.Count <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #008000;">#Create a scripter object with a connection to the server object created above</span>
        <span style="color: #800080;">$scripter</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Microsoft.SqlServer.Management.Smo.Scripter<span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span><span style="color: #000000;">&#41;</span>
&nbsp;
        <span style="color: #008000;">#Set some scripting option properties</span>
        <span style="color: #800080;">$scripter</span>.options.ScriptBatchTerminator <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
        <span style="color: #800080;">$scripter</span>.options.FileName <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$Path\BEFORE_$Schema.$Name.sql&quot;</span>
        <span style="color: #800080;">$scripter</span>.options.ToFileOnly <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
        <span style="color: #800080;">$scripter</span>.options.Permissions <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
        <span style="color: #800080;">$scripter</span>.options.DriAll <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
        <span style="color: #800080;">$scripter</span>.options.Triggers <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
        <span style="color: #800080;">$scripter</span>.options.Indexes <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
        <span style="color: #800080;">$scripter</span>.Options.IncludeHeaders <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
&nbsp;
        <span style="color: #008000;">#Script the collection of URNs</span>
        <span style="color: #800080;">$scripter</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #800080;">$urns</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-weight: bold;">write-warning</span> <span style="color: #800000;">&quot;Object $Schema.$Name Not Found!&quot;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span> <span style="color: #008000;">#Backup-DatabaseObject</span></pre></td></tr></table></div>

<p>And here&#8217;s example of sourcing and calling the function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;">. .<span style="color: pink;">/</span>Backup<span style="color: pink;">-</span>DatabaseObject.ps1
Backup<span style="color: pink;">-</span>DatabaseObject <span style="color: pink;">-</span>ServerInstance Z002 <span style="color: pink;">-</span>Database AdventureWorks <span style="color: pink;">-</span>Schema HumanResources <span style="color: #008080; font-style: italic;">-Name</span> vEmployee <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Users\Public&quot;</span></pre></td></tr></table></div>

<p>I&#8217;ve posted the code on <a href="http://poshcode.org/3367" target="_blank">PoshCode</a> also.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/04/backup-database-object/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Table-Valued Parameter Example</title>
		<link>http://sev17.com/2012/04/table-valued-parameter-example/</link>
		<comments>http://sev17.com/2012/04/table-valued-parameter-example/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 19:34:53 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[TVP]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10896</guid>
		<description><![CDATA[I wanted show someone how to use table-valued parameters available in SQL Server 2008 and higher. The main use case of table-valued parameters is for sending a list or array of items as parameter to a SQL Server stored procedure or function. This is more efficient than parsing strings or XML on the SQL Server...]]></description>
			<content:encoded><![CDATA[<p>I wanted show someone how to use table-valued parameters available in SQL Server 2008 and higher. The main use case of table-valued parameters is for sending a list or array of items as parameter to a SQL Server stored procedure or function. This is more efficient than parsing strings or XML on the SQL Server side. I couldn&#8217;t seem to find a complete example of <a href="http://msdn.microsoft.com/en-us/library/bb510489.aspx" target="_blank">table-valued parameters</a> in the SQL Server documentation. The SQL Server docs only shows the T-SQL portion of the code and not the ADO.NET. I think its difficult to see how you would use this feature without having both the T-SQL and .NET code shown together so, here&#8217;s a simple T-SQL and Powershell script demonstrating table-valued parameters:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">&lt;#
/* FROM SSMS */
USE AdventureWorks
GO
/* Create a CustomerList table type */
CREATE TYPE Sales.CustomerList AS TABLE 
( CustomerID INT );
GO
&nbsp;
/* Create a procedure to use new table type */
CREATE PROCEDURE Sales.uspGetCustomer
    @TVP CustomerList READONLY
    AS 
    SET NOCOUNT ON
    SELECT c.*
    FROM Sales.Customer c
    JOIN @TVP t ON
    c.CustomerID = t.CustomerID;
 GO
&nbsp;
/* Test type and procedure in SSMS */
&nbsp;
/* Declare a variable that references the type. */
DECLARE @CustomerTVP AS Sales.CustomerList;
&nbsp;
/* Add data to the table variable. */
INSERT INTO @CustomerTVP (CustomerID)
SELECT * FROM (
	VALUES (1),(2),(3),(4),(5)
) AS v (CustomerID)
&nbsp;
/* Pass the table variable data to a stored procedure. */
EXEC Sales.uspGetCustomer @CustomerTVP;
GO
#&gt;</span>
&nbsp;
<span style="color: #008000;">#FROM Powershell</span>
<span style="color: #008000;">#Create an ADO.NET DataTable matching the CustomerList Table Type:</span>
<span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Data.datatable  
<span style="color: #800080;">$col</span> <span style="color: pink;">=</span>  <span style="color: #008080; font-weight: bold;">new-object</span> Data.DataColumn  
<span style="color: #800080;">$col</span>.ColumnName <span style="color: pink;">=</span> <span style="color: #800000;">'CustomerID'</span>  
<span style="color: #800080;">$col</span>.DataType <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Int32<span style="color: #000000;">&#93;</span>
<span style="color: #800080;">$dt</span>.Columns.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Col</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;">#Add a Row to the DataTable</span>
<span style="color: #800080;">$dr</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dt</span>.NewRow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$dr</span>.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">'CustomerId'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>   
<span style="color: #800080;">$dt</span>.Rows.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dr</span><span style="color: #000000;">&#41;</span>  
&nbsp;
<span style="color: #008000;">#Add a 2nd Row to the DataTable</span>
<span style="color: #800080;">$dr</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dt</span>.NewRow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$dr</span>.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">'CustomerId'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #804000;">2</span>   
<span style="color: #800080;">$dt</span>.Rows.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dr</span><span style="color: #000000;">&#41;</span>  
&nbsp;
<span style="color: #008000;">#Add a 3rd Row to the DataTable</span>
<span style="color: #800080;">$dr</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dt</span>.NewRow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$dr</span>.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">'CustomerId'</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">=</span> <span style="color: #804000;">3</span>   
<span style="color: #800080;">$dt</span>.Rows.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$dr</span><span style="color: #000000;">&#41;</span>  
&nbsp;
<span style="color: #008000;">#Connection and Query Info</span>
<span style="color: #800080;">$serverName</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;$env:computername\sql1&quot;</span> 
<span style="color: #800080;">$databaseName</span><span style="color: pink;">=</span><span style="color: #800000;">'AdventureWorks'</span> 
<span style="color: #800080;">$query</span><span style="color: pink;">=</span><span style="color: #800000;">'Sales.uspGetCustomer'</span> 
&nbsp;
<span style="color: #008000;">#Connect</span>
<span style="color: #800080;">$connString</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Server=$serverName;Database=$databaseName;Integrated Security=SSPI;&quot;</span> 
<span style="color: #800080;">$conn</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlConnection <span style="color: #800080;">$connString</span> 
<span style="color: #800080;">$conn</span>.Open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;">#Create Sqlcommand type and params</span>
<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Data.SqlClient.SqlCommand
<span style="color: #800080;">$cmd</span>.Connection <span style="color: pink;">=</span> <span style="color: #800080;">$conn</span>
<span style="color: #800080;">$cmd</span>.CommandType <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Data.CommandType<span style="color: #000000;">&#93;</span><span style="color: #800000;">&quot;StoredProcedure&quot;</span>
<span style="color: #800080;">$cmd</span>.CommandText<span style="color: pink;">=</span> <span style="color: #800080;">$query</span>
<span style="color: #800080;">$null</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cmd</span>.Parameters.Add<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;@TVP&quot;</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>System.Data.SqlDbType<span style="color: #000000;">&#93;</span>::Structured<span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cmd</span>.Parameters<span style="color: #000000;">&#91;</span><span style="color: #800000;">&quot;@TVP&quot;</span><span style="color: #000000;">&#93;</span>.Value <span style="color: pink;">=</span> <span style="color: #800080;">$dt</span>
&nbsp;
<span style="color: #008000;">#Create and fill dataset</span>
<span style="color: #800080;">$ds</span><span style="color: pink;">=</span><span style="color: #008080; font-weight: bold;">New-Object</span> system.Data.DataSet
<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: #000000;">&#41;</span>
<span style="color: #800080;">$null</span> <span style="color: pink;">=</span> <span style="color: #800080;">$da</span>.fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$ds</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$conn</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;">#Return results</span>
<span style="color: #800080;">$ds</span>.Tables</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/04/table-valued-parameter-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Transforming Event Log Data</title>
		<link>http://sev17.com/2012/03/transforming-event-log-data/</link>
		<comments>http://sev17.com/2012/03/transforming-event-log-data/#comments</comments>
		<pubDate>Fri, 23 Mar 2012 00:49:28 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[SqlProxy]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10882</guid>
		<description><![CDATA[Several months ago I described a solution for Delegated SQL Server Administration with Powershell. In the solution, the SqlProxy module audits all security administration activity to a custom Windows Event log. In this blog post, I&#8217;ll described a process to transform and incrementally load the audit data into a SQL Server table for reporting purposes....]]></description>
			<content:encoded><![CDATA[<p>Several months ago I described a solution for <a href="http://sev17.com/2011/11/delegated-sql-server-administration-with-powershell/" target="_blank">Delegated SQL Server Administration with Powershell</a>. In the solution, the SqlProxy module audits all security administration activity to a custom Windows Event log. In this blog post, I&#8217;ll described a process to transform and incrementally load the audit data into a SQL Server table for reporting purposes.</p>
<h2>Writing to the Event Log</h2>
<p>First a quick review of how  SqlProxy module writes messages to the event log. This is important because as we&#8217;ll see in a moment, how the message is constructed helps in extracting Event log data. In the SqlProxy module I use a standard template in each function for logging messages to the Event log:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$PSUserName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$PSSenderInfo</span>.UserInfo.Identity.Name
<span style="color: #800080;">$logmessage</span> <span style="color: pink;">=</span>  <span style="color: #800000;">&quot;PSUserName=$PSUserName&quot;</span> <span style="color: pink;">+</span> $<span style="color: #000000;">&#40;</span><span style="color: #800080;">$psBoundParameters</span>.GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: pink;">%</span><span style="color: #000000;">&#123;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>$($_.Key)=$($_.Value)&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
write<span style="color: pink;">-</span>sqlproxylog <span style="color: pink;">-</span>eventID <span style="color: #800080;">$eventID</span>.<span style="color: #800000;">&quot;$($myinvocation.mycommand.name)&quot;</span> <span style="color: #008080; font-style: italic;">-message</span> $logmessage</pre></div></div>

<p>The message is constructed using several built-in variables written as key/value pairs.</p>
<ol>
<li>The $PSSenderInfo variable is available inside of remote session and returns information about the user who started the PSSession. Since I&#8217;m using runas credentials I&#8217;ll grab the name of the person who is connected.</li>
<li>$psBoundParameters contains a hashtable of the parameters and their values for the current function.</li>
<li>This code may look a little odd, $eventID.&#8221;$($myinvocation.mycommand.name)&#8221;. I created hashtable called $eventid in the SqlProxy module to translate a function name from a an EventId. Since $myinvocation.mycommand.name returns the function name I&#8217;ll use this as the hashtable key as shown below:</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$EventID</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>
<span style="color: #800000;">&quot;Add-SqlDatabaseRoleMember&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">0</span>
<span style="color: #800000;">&quot;Add-SqlLogin&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">1</span>
<span style="color: #800000;">&quot;Add-SqlServerRoleMember&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">2</span>
<span style="color: #800000;">&quot;Add-SqlUser&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">3</span>
<span style="color: #800000;">&quot;Remove-SqlDatabaseRoleMember&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">4</span>
<span style="color: #800000;">&quot;Remove-SqlLogin&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">5</span>
<span style="color: #800000;">&quot;Remove-SqlServerRoleMember&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">6</span>
<span style="color: #800000;">&quot;Remove-SqlUser&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">7</span>
<span style="color: #800000;">&quot;Rename-SqlLogin&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">8</span>
<span style="color: #800000;">&quot;Set-SqlLogin&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">9</span>
<span style="color: #800000;">&quot;Set-SqlLoginDefaultDatabase&quot;</span><span style="color: pink;">=</span><span style="color: #804000;">10</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The write-sqlproxlog function is just a wrapper around write-eventlog as follows:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Write<span style="color: pink;">-</span>SqlProxyLog
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">0</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$EventID</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">1</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$Message</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">2</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$EntryType</span><span style="color: pink;">=</span><span style="color: #800000;">'SuccessAudit'</span>
    <span style="color: #000000;">&#41;</span>
&nbsp;
    write<span style="color: pink;">-</span>eventlog <span style="color: #008080; font-style: italic;">-logname</span> SqlProxy <span style="color: pink;">-</span>source SqlProxy <span style="color: pink;">-</span>eventID <span style="color: #800080;">$eventID</span> <span style="color: #008080; font-style: italic;">-message</span> <span style="color: #800080;">$message</span> <span style="color: pink;">-</span>EntryType <span style="color: #800080;">$EntryType</span>
&nbsp;
<span style="color: #000000;">&#125;</span> <span style="color: #008000;">#Write-SqlProxyLog</span></pre></div></div>

<p>A typical event log entry will look like this:</p>
<p><a href="http://sev17.com/wp-content/uploads/SqlProxEvtLog.jpg"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="SqlProxEvtLog" src="http://sev17.com/wp-content/uploads/SqlProxEvtLog_thumb.jpg" alt="SqlProxEvtLog" width="244" height="181" border="0" /></a></p>
<h2>Extracting Data from the Eventlog</h2>
<p>In order to load the Eventlog data into a SQL Server table I created a module called SqlTools which is collection of functions I use frequently for querying and loading data. I&#8217;ve posted the module <a href="https://skydrive.live.com/redir.aspx?cid=ea42395138308430&amp;resid=EA42395138308430!1014&amp;parid=EA42395138308430!194" target="_blank">here</a>.</p>
<p>The initial load script as shown below makes use of SqlTools module:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">import<span style="color: pink;">-</span>module SqlTools
&nbsp;
<span style="color: #800080;">$ComputerName</span> <span style="color: pink;">=</span> <span style="color: #800000;">'Z001'</span>
<span style="color: #800080;">$ServerInstance</span> <span style="color: pink;">=</span> <span style="color: #800000;">'Z002\sql1'</span>
<span style="color: #800080;">$Database</span> <span style="color: pink;">=</span> <span style="color: #800000;">'SqlProxy'</span>
&nbsp;
<span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-EventLog</span> <span style="color: #008080; font-style: italic;">-LogName</span> SqlProxy <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$ComputerName</span> <span style="color: pink;">-</span>EntryType <span style="color: #800000;">'SuccessAudit'</span> <span style="color: pink;">|</span> <span style="color: pink;">%</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$ht</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.Message <span style="color: #FF0000;">-replace</span> <span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span>
    ConvertFrom<span style="color: pink;">-</span>StringData; <span style="color: #800080;">$xml</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> psobject <span style="color: #008080; font-style: italic;">-Property</span> <span style="color: #800080;">$ht</span> <span style="color: pink;">|</span> ConvertTo<span style="color: pink;">-</span>Xml <span style="color: #008080; font-style: italic;">-NoTypeInformation</span> <span style="color: #FF0000;">-As</span> String;
    <span style="color: #008080; font-weight: bold;">new-object</span> psobject <span style="color: #008080; font-style: italic;">-Property</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span><span style="color: #800000;">'Index'</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index; <span style="color: #800000;">'TimeGenerated'</span><span style="color: pink;">=</span><span style="color: #000080;">$_</span>.TimeGenerated;
    <span style="color: #800000;">'EventId'</span><span style="color: pink;">=</span><span style="color: #000080;">$_</span>.EventId; <span style="color: #800000;">'MessageXml'</span><span style="color: pink;">=</span><span style="color: #800080;">$xml</span><span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> Out<span style="color: pink;">-</span>DataTable
&nbsp;
Add<span style="color: pink;">-</span>SqlTable <span style="color: pink;">-</span>ServerInstance <span style="color: #800080;">$ServerInstance</span> <span style="color: pink;">-</span>Database <span style="color: #800080;">$Database</span> <span style="color: pink;">-</span>TableName <span style="color: #800000;">'SqlProxyLog'</span> <span style="color: pink;">-</span>DataTable <span style="color: #800080;">$dt</span> <span style="color: pink;">-</span>AsScript <span style="color: pink;">|</span> clip</pre></div></div>

<p>At this point I&#8217;ll paste the T-SQL script into SSMS, modify column data types, null/not null, add primary key and finally create the table:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyLog<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">int</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>TimeGenerated<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">datetime</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>MessageXml<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span>xml<span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Index</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">int</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
 <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>PK_SqlProxyLog<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>TimeGenerated<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span>,
	<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Index</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span></pre></div></div>

<p>Then I&#8217;ll return to Powershell to execute the write-datatable function:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Write<span style="color: pink;">-</span>DataTable <span style="color: pink;">-</span>ServerInstance <span style="color: #800080;">$ServerInstance</span> <span style="color: pink;">-</span>Database <span style="color: #800080;">$Database</span> <span style="color: pink;">-</span>TableName <span style="color: #800000;">'SqlProxyLog'</span> <span style="color: pink;">-</span>Data $dt</pre></div></div>

<p>In order to incrementally load only new events, I&#8217;ll modify the get-sqlproxylog.ps1 to first grab the max timegenerated or 1900-01-01 if its null and then use value for the -After param of the Get-Eventlog cmdlet:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">import<span style="color: pink;">-</span>module SqlTools
&nbsp;
<span style="color: #800080;">$ComputerName</span> <span style="color: pink;">=</span> <span style="color: #800000;">'Z001'</span>
<span style="color: #800080;">$ServerInstance</span> <span style="color: pink;">=</span> <span style="color: #800000;">'Z002\sql1'</span>
<span style="color: #800080;">$Database</span> <span style="color: pink;">=</span> <span style="color: #800000;">'SqlProxy'</span>
&nbsp;
<span style="color: #800080;">$query</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT ISNULL(MAX(TimeGenerated),'1900-01-01') AS TimeGenerated FROM dbo.SqlProxyLog&quot;</span>
<span style="color: #800080;">$maxDtm</span> <span style="color: pink;">=</span> invoke<span style="color: pink;">-</span>sqlcmd2 <span style="color: pink;">-</span>ServerInstance <span style="color: #800080;">$ServerInstance</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;">$query</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> <span style="color: #008080; font-style: italic;">-ExpandProperty</span> TimeGenerated
&nbsp;
<span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-EventLog</span> <span style="color: #008080; font-style: italic;">-LogName</span> SqlProxy <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$ComputerName</span> <span style="color: pink;">-</span>EntryType <span style="color: #800000;">'SuccessAudit'</span> <span style="color: pink;">-</span>After <span style="color: #800080;">$maxDtm</span> <span style="color: pink;">|</span> <span style="color: pink;">%</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$ht</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.Message <span style="color: #FF0000;">-replace</span> <span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span>
    ConvertFrom<span style="color: pink;">-</span>StringData; <span style="color: #800080;">$xml</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> psobject <span style="color: #008080; font-style: italic;">-Property</span> <span style="color: #800080;">$ht</span> <span style="color: pink;">|</span> ConvertTo<span style="color: pink;">-</span>Xml <span style="color: #008080; font-style: italic;">-NoTypeInformation</span> <span style="color: #FF0000;">-As</span> String;
    <span style="color: #008080; font-weight: bold;">new-object</span> psobject <span style="color: #008080; font-style: italic;">-Property</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span><span style="color: #800000;">'Index'</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Index; <span style="color: #800000;">'TimeGenerated'</span><span style="color: pink;">=</span><span style="color: #000080;">$_</span>.TimeGenerated;
    <span style="color: #800000;">'EventId'</span><span style="color: pink;">=</span><span style="color: #000080;">$_</span>.EventId; <span style="color: #800000;">'MessageXml'</span><span style="color: pink;">=</span><span style="color: #800080;">$xml</span><span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> Out<span style="color: pink;">-</span>DataTable
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$dt</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    Write<span style="color: pink;">-</span>DataTable <span style="color: pink;">-</span>ServerInstance <span style="color: #800080;">$ServerInstance</span> <span style="color: pink;">-</span>Database <span style="color: #800080;">$Database</span> <span style="color: pink;">-</span>TableName <span style="color: #800000;">'SqlProxyLog'</span> <span style="color: pink;">-</span>Data <span style="color: #800080;">$dt</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Some interesting points about this script:</p>
<ul>
<li>Since the message data is stored as key/value pairs, the built-in ConvertFrom-StringData cmdlet is used to create the hashtable $ht</li>
<li>The hashtable is then used to create a psobject</li>
<li>The psobject is converted into XML using ConverTo-Xml.</li>
</ul>
<p>One minor issue I ran into is with unenclosed backslashes. If backslashes are enclosed in quotes its fine, if not it causes an error with ConvertFrom-StringData, so I replace them with forward slashes.</p>
<h2>Data Is Loaded, Now What?</h2>
<p>After I&#8217;ve loaded the data I&#8217;ll use XQuery to shred the message XML column into a relational data set. I created a function and view in SQL Server for this purpose:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">FUNCTION</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>ufn_GetEventMessage<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span>@MessageXml XML<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">RETURNS</span> @Message <span style="color: #0000FF;">TABLE</span>
<span style="color: #808080;">&#40;</span>
	 ChangeOrder <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,dbname  <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,PSUserName   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,name   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,rolename   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,<span style="color: #808080;">&#91;</span>Description<span style="color: #808080;">&#93;</span>   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,sqlserver   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,loginame   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,<span style="color: #808080;">&#91;</span>login<span style="color: #808080;">&#93;</span>   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	,DefaultDatabase <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">INSERT</span> @Message <span style="color: #808080;">&#40;</span>ChangeOrder,dbname,PSUserName,name,rolename,Description,sqlserver,loginame,<span style="color: #808080;">&#91;</span>login<span style="color: #808080;">&#93;</span>,DefaultDatabase<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">SELECT</span>
	 Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;ChangeOrder&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> ChangeOrder
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;dbname&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> dbname
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;PSUserName&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> PSUserName
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;name&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> name
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;rolename&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> rolename
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;Description&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Description<span style="color: #808080;">&#93;</span>
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;sqlserver&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> sqlserver
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;loginame&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> loginame
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;login&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>login<span style="color: #808080;">&#93;</span>
	,Objects.<span style="color: #0000FF;">Object</span>.<span style="color: #202020;">query</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Property[@Name=&quot;DefaultDatabase&quot;]'</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'varchar(128)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> DefaultDatabase
	<span style="color: #0000FF;">FROM</span> @MessageXml.<span style="color: #202020;">nodes</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'/Objects/Object'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Objects<span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Object</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">RETURN</span>
<span style="color: #0000FF;">END</span>;
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>vw_SqlProxyLog<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">SELECT</span> l.<span style="color: #808080;">*</span>, m.<span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">SqlProxyLog</span> l
<span style="color: #808080;">CROSS</span> APPLY dbo.<span style="color: #202020;">ufn_GetEventMessage</span><span style="color: #808080;">&#40;</span>l.<span style="color: #202020;">MessageXml</span><span style="color: #808080;">&#41;</span> m
GO</pre></div></div>

<p>The function requires I define all the possible attributes as columns. Here&#8217;s an example of parsing a typical message:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @MessageXml XML
<span style="color: #0000FF;">SET</span> @MessageXml <span style="color: #808080;">=</span>
<span style="color: #FF0000;">'&lt;Objects&gt;&lt;Object&gt;&lt;Property Name=&quot;DefaultDatabase&quot;&gt;master&lt;/Property&gt;
&lt;Property Name=&quot;PasswordPolicyEnforced&quot;&gt;False&lt;/Property&gt;
&lt;Property Name=&quot;Description&quot; /&gt;
&lt;Property Name=&quot;sqlserver&quot;&gt;[Z002/SQL1]&lt;/Property&gt;
&lt;Property Name=&quot;password&quot;&gt;System.Security.SecureString&lt;/Property&gt;
&lt;Property Name=&quot;name&quot;&gt;test3&lt;/Property&gt;
&lt;Property Name=&quot;PasswordExpirationEnabled&quot;&gt;False&lt;/Property&gt;
&lt;Property Name=&quot;ChangeOrder&quot;&gt;1234&lt;/Property&gt;
&lt;Property Name=&quot;logintype&quot;&gt;SqlLogin&lt;/Property&gt;
&lt;Property Name=&quot;PSUserName&quot;&gt;Z002/u00&lt;/Property&gt;
&lt;/Object&gt;&lt;/Objects&gt;'</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">ufn_GetEventMessage</span><span style="color: #808080;">&#40;</span>@MessageXMl<span style="color: #808080;">&#41;</span></pre></div></div>

<table width="656">
<tbody>
<tr>
<td><strong>ChangeOrder </strong></td>
<td><strong>PSUserName </strong></td>
<td><strong>name </strong></td>
<td><strong>sqlserver </strong></td>
<td><strong>DefaultDatabase</strong></td>
</tr>
<tr>
<td>1234</td>
<td>Z002/u00</td>
<td>test3</td>
<td>[Z002/SQL1]</td>
<td>master</td>
</tr>
</tbody>
</table>
<p>Finally I&#8217;ll add reference table for the SqlProxyEventType:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">smallint</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
 <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>PK_SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Add-SqlDatabaseRoleMember'</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Add-SqlLogin'</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Add-SqlServerRoleMember'</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Add-SqlUser'</span>, <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Remove-SqlDatabaseRoleMember'</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Remove-SqlLogin'</span>, <span style="color: #000;">5</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Remove-SqlServerRoleMember'</span>, <span style="color: #000;">6</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Remove-SqlUser'</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Rename-SqlLogin'</span>, <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Set-SqlLogin'</span>, <span style="color: #000;">9</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SqlProxyEventType<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EventName<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EventId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'Set-SqlLoginDefaultDatabase'</span>, <span style="color: #000;">10</span><span style="color: #808080;">&#41;</span></pre></div></div>

<p>The last thing I&#8217;ll do is setup a schedule SQL Agent job to run the get-sqlproxylog.ps1 script:</p>
<p>&#8211;Setup SQL Job with CmdExec Job Step:</p>
<p>&#8211;C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command c:\scripts\get-sqlproxylog.ps1</p>
<h2>Summary</h2>
<p>This post demonstrated extracting information from a specially crafted message in an event log and loading the data into a SQL Server table. Its kind of interesting to think about how easy it is to transform data from one thing to the next using Powershell. In this case the data was converted six times:</p>
<ol>
<li>To an Event log message data stored as key/value pairs using Write-Eventlog/Get-Eventlog</li>
<li>To a hashtable using ConvertFrom-StringData</li>
<li>To a psobject using New-Object -property hashtable</li>
<li>To a DataTable using Out-DataTable</li>
<li>Into a SQL Server table using Write-DataTable</li>
<li>Out of a SQL Server table XML column to relational data set using XQuery</li>
</ol>
<p>That said, next time I should probably just write the damn thing directly to a database to begin with.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/03/transforming-event-log-data/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Reconciling SPNs</title>
		<link>http://sev17.com/2012/02/reconciling-spns/</link>
		<comments>http://sev17.com/2012/02/reconciling-spns/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 14:39:32 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[SPN]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10837</guid>
		<description><![CDATA[As part of troubleshooting Kerberos authentication for SQL Server I had to verify SPNs so I thought I&#8217;d blog about the process I went through. Sometimes I think a post blog is nothing more than public documentation of  a complex problem &#8230; Get SQL Server SPNs from Active Directory Before Powershell I would use the...]]></description>
			<content:encoded><![CDATA[<p>As part of troubleshooting Kerberos authentication for SQL Server I had to verify SPNs so I thought I&#8217;d blog about the process I went through. Sometimes I think a post blog is nothing more than public documentation of  a complex problem &#8230;</p>
<h2>Get SQL Server SPNs from Active Directory</h2>
<p>Before Powershell I would use the command-line utility setspn.exe to retrieve a list of SPNs for a given account. The syntax is:</p>
<p>setspn -L AccountName</p>
<p>Although this works reasonably well there are a couple of issues first the  output is text instead of objects which means I&#8217;d have to do a lot parsing and if you find you&#8217;re parsing text in Powershell too much there&#8217;s a high likelihood you&#8217;re doing it the hard way!. The second issue with setspn -L is that it expects an account and doesn&#8217;t retrieve ALL SPNs for a given service. For these reasons I created a script called Get-SqlSpn which will query Active Directory for all SQL Server SPNs. Querying AD for SPNs still requires a bit of parsing (I&#8217;ve taken care of this for you in the Get-SqlSpn function), but not near as much as starting from setspn. Let&#8217;s look at how I use Get-SqlSpn&#8230;</p>
<h3>Using Get-SqlSpn</h3>
<p>1. Download <a title="Get-SqlSpn" href="http://poshcode.org/3234" target="_blank">Get-SqlSpn</a><br />
2. Source the function</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">. .<span style="color: pink;">/</span>Get<span style="color: pink;">-</span>Sqlspn.ps1</pre></div></div>

<p>#Get the SQL Server SPNs</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"> <span style="color: #800080;">$spns</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>SqlSpn</pre></div></div>

<p>If I&#8217;m interactively exploring data I like to use a tool for that specific purpose which for me is either loading the data into SQL Server or using Excel. For this particular I think Excel is a better fit. There is some tidying up and normalizing data that needs to be done which is really easy with Excel. Although you could mess with additional scripts to export data directly into Excel or or convert CSV files&#8211;a quick and dirty way to get data into Excel is simply copy and paste from out-gridview:<br />
3. Open a new Excel document and rename a worksheet servers<br />
4. Get Column Headers</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$object</span> <span style="color: pink;">=</span> <span style="color: #800080;">$spns</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> <span style="color: #008080; font-style: italic;">-first</span> <span style="color: #804000;">1</span>
<span style="color: #800080;">$ht</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$property</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$object</span>.PsObject.get_properties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #800080;">$ht</span>.add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$property</span>.Name.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #800080;">$property</span>.Name.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>5. Copy/Paste heading row to Excel (Ctrl-A, Ctrl-C)</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">new-object</span> psobject <span style="color: #008080; font-style: italic;">-Property</span> <span style="color: #800080;">$ht</span> <span style="color: pink;">|</span> out<span style="color: pink;">-</span>gridview</pre></div></div>

<p>6. Copy/Paste spns to Excel (Ctrl-A, Ctrl-C)</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$spns</span> <span style="color: pink;">|</span> out<span style="color: pink;">-</span>gridview</pre></div></div>

<p>Now that you have a list of all SQL Server SPNs from AD, let&#8217;s grab some information from all of our SQL Servers to match against. For this purpose I created a script called Get-SqlWmi..</p>
<h3>Using Get-SqlWmi</h3>
<p>1. Download <a title="Get-SqlWmi" href="http://poshcode.org/3235" target="_blank">Get-SqlWmi</a> and <a title="Invoke-SqlCmd2" href="http://poshcode.org/2279" target="_blank">Invoke-Sqlcmd2</a><br />
2. Source the functions</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">. .\invoke<span style="color: pink;">-</span>sqlcmd2.ps1
. .\get<span style="color: pink;">-</span>sqlwmi.ps1</pre></div></div>

<p>I use <a href="http://sev17.com/2008/11/inventory-sql-server-databases-with-powershell/" target="_blank">a  query against System Center Configuration Manager database</a> to pull in a list of SQL Servers. A second approach, if you maintain a SQL Server Center Management Server (CMS)  is to query sysmanagement tables. I&#8217;ll then pipe the list of servers to the get-sqlwmi function:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$servers</span> <span style="color: pink;">=</span> invoke<span style="color: pink;">-</span>sqlcmd2 <span style="color: pink;">-</span>ServerInstance <span style="color: #800000;">'smsserver\sql10'</span> <span style="color: pink;">-</span>Database <span style="color: #800000;">'dbautility'</span> <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800000;">&quot;select server_name AS 'ComputerName' from sms_sql_server_name_vw&quot;</span> <span style="color: pink;">|</span> get<span style="color: pink;">-</span>sqlwmi</pre></div></div>

<p>3. Rename Excel worksheet Sheet2 to servers<br />
4. Get Column Headers</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$object</span> <span style="color: pink;">=</span> <span style="color: #800080;">$servers</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> <span style="color: #008080; font-style: italic;">-first</span> <span style="color: #804000;">1</span>
<span style="color: #800080;">$ht</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$property</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$object</span>.PsObject.get_properties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$ht</span>.add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$property</span>.Name.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #800080;">$property</span>.Name.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>5. Copy/Paste heading row into Excel</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">new-object</span> psobject <span style="color: #008080; font-style: italic;">-Property</span> <span style="color: #800080;">$ht</span> <span style="color: pink;">|</span> out<span style="color: pink;">-</span>gridview</pre></div></div>

<p>6. Copy/Paste servers to Excel</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$servers</span> <span style="color: pink;">|</span> out<span style="color: pink;">-</span>gridview</pre></div></div>

<p>Now that I collected the data, I start analyzing&#8230;</p>
<h2>Analyzing SPNs</h2>
<p><em>Note: The steps which follow  involve using some basic data analysis functions in Excel. If you&#8217;re not sure how to filter, define named ranges or use Excel functions you may brush up on Excel. I&#8217;ve also included an Excel document with the appropriate fields and formulas for <a href="https://skydrive.live.com/redir.aspx?cid=ea42395138308430&amp;resid=EA42395138308430!1012&amp;parid=EA42395138308430!194&amp;authkey=!AJ59P92kso7I9hY" target="_blank">download here</a>.</em></p>
<p>First I&#8217;ll look for invalid SPNs. I&#8217;ve seen issues where people will manually create SPNs incorrectly. The correct format is:</p>
<p><strong>MSSQLSvc/&lt;SQL Server computer name&gt;:1433 AccountName</strong></p>
<p><strong>MSSQLSvc/&lt;SQL Server FQDN&gt;:1433  AccountName</strong></p>
<p>Strangely enough I&#8217;ve seen semi-colons or commas instead of colons used which simply doesn&#8217;t work. The easiest way to find these SPNs is to use the Text Filter using contains <strong>;</strong> or <strong>,</strong></p>
<p><a href="http://sev17.com/wp-content/uploads/contains.jpg"><img class="alignnone size-thumbnail wp-image-10848" title="contains" src="http://sev17.com/wp-content/uploads/contains-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>The next thing I&#8217;ll do is normalize the data. Get-SqlSpn lists the accounts without the domain prefix, while Get-SqlWmi does so I&#8217;ll do a global replace to remove the domain slash.</p>
<p>In order to define a unique key, I&#8217;ll create a column which combines the SPN and service account name:</p>
<p>= B6 &amp; &#8221; &#8221;  &amp;C6</p>
<p>&nbsp;</p>
<p>I&#8217;ll name the column searchSPN, this will allow me to do matching against the server list as we&#8217;ll see in a moment.</p>
<p><a href="http://sev17.com/wp-content/uploads/searchSpn.jpg"><img class="alignnone size-thumbnail wp-image-10849" title="searchSpn" src="http://sev17.com/wp-content/uploads/searchSpn-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>On the servers worksheet I&#8217;ll create two new columns of spn and combined as follows:</p>
<p>= &#8220;MSSQLSvc/&#8221; &amp; D2 &amp; &#8220;.contoso.com:&#8221; &amp;A2</p>
<p>= E2 &amp; &#8221; &#8221; &amp;C2</p>
<p>I&#8217;ll then name the combine column (unique key) searchServer</p>
<p><a href="http://sev17.com/wp-content/uploads/searchServer.jpg"><img class="alignnone size-thumbnail wp-image-10850" title="searchServer" src="http://sev17.com/wp-content/uploads/searchServer-150x139.jpg" alt="" width="150" height="139" /></a></p>
<p>Next I&#8217;ll add a column called matched to each worksheet and use the MATCH function to look for well, matches between SPN and Server lists:</p>
<p>=MATCH(E2,searchServer,0)</p>
<p>=MATCH(F2,searchSpn,0)</p>
<p>If there&#8217;s a match the match column will list the row number of corresponding match and if not you&#8217;ll see #NA. You can then filter on #NA and try to figure out why there are mismatches. Did I miss a server in inventory? Are SPNs defined for servers no longer on the network, etc.</p>
<p>And finally I&#8217;ll add one more column of a setspn command to delete or add SPNs using a Excel formula:</p>
<p>=&#8221;setspn -D &#8221; &amp; E2</p>
<p>=&#8221;setspn -A &#8221; &amp; F2</p>
<p>It may seem a little odd to build up commands in this way rather than using Powershell script, but I&#8217;m really paranoid about SPNs and want to verify each action. If you&#8217;re not careful you can cause some authentication problems by deleting needed SPNs or adding duplicates.</p>
<p><em>Note: a quick way to find duplicate SPNs is to use the command-line <strong>setspn -T * -X</strong>. You can also use  Excel&#8217;s conditional formatting to quickly identify duplicate values as described in <a href="http://www.addictivetips.com/windows-tips/excel-2010-duplicate-unique-values/" target="_blank">here</a>. <em> I&#8217;ve seen duplicate SPNs cause SQL Server to fallback to NTLM instead of using Kerberos. Searching for duplicate SPNs should be part of  your reconciliation. To fix simply remove the duplicate SPN.</em></em></p>
<h2>Summary</h2>
<p>This post demonstrated how to reconcile SQL Server SPNs between Active Directory and the SQL Server services. I think its  important to point out that you don&#8217;t need to take a 100% Powershell approach to a problem. In this example I could have used various Powershell cmdlets (compare-object, where-object, group-object, etc.), however for moderate to complex data analysis I prefer to use tools suited to task of data analysis either Excel or SQL Server. I&#8217;ll still use Powershell to gather the data and for simple data analysis tasks.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/02/reconciling-spns/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Sharing Profiles</title>
		<link>http://sev17.com/2012/02/sharing-profiles/</link>
		<comments>http://sev17.com/2012/02/sharing-profiles/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 18:01:38 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[Symlink]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10829</guid>
		<description><![CDATA[A question came up in a class I was teaching:  “How do you share your Powershell profiles across accounts?” Well, there’s documented way of sharing profiles on the same machine where you create an All Users profile. As stated in help about_Profiles Powershell will look in the following locations for profiles: Name Description ----------- -----------...]]></description>
			<content:encoded><![CDATA[<p>A question came up in a class I was teaching:  “How do you share your Powershell profiles across accounts?”</p>
<p>Well, there’s documented way of sharing profiles on the same machine where you create an All Users profile. As stated in <a href="http://technet.microsoft.com/en-us/library/dd315342.aspx" target="_blank">help about_Profiles</a> Powershell will look in the following locations for profiles:</p>
<blockquote>
<pre>        Name                               Description
        -----------                        -----------
        $Profile                           Current User,Current Host
        $Profile.CurrentUserCurrentHost    Current User,Current Host
        $Profile.CurrentUserAllHosts       Current User,All Hosts
        $Profile.AllUsersCurrentHost       All Users, Current Host
        $Profile.AllUsersAllHosts          All Users, All Hosts</pre>
</blockquote>
<p>The All Users profile creation is a little odd in that you would need to create your profiles in the $pshome directory instead of the $home directory. The $pshome directory is the location where powershell.exe is installed i.e. C:\Windows\System32\WindowsPowerShell\v1.0 while $home is under &lt;user&gt;\Documents\WindowsPowershell.</p>
<p>I don’t like messing with storing my shared profiles in a system directory, so instead I use a different technique…</p>
<h2>Enter Symlinks</h2>
<p>I’ve <a href="http://sev17.com/2010/10/using-symlinks-in-powershell-scripts/" target="_blank">previously blogged about Symlinks</a>, but I didn’t mention I use them for sharing profiles. <em>Note: This  requires Vista, Windows 7  or 2008 or higher OS</em>. So here’s the steps to share profiles using symlinks:</p>
<ol>
<li>Create A WindowsPowershell folder or copy your existing WindowsPowershell to a shared location. I use C:\Users\Public\Documents.</li>
<li>Make sure your accounts don’t have a WindowsPowershell folder under &lt;user&gt;\Documents already.</li>
<li>Start a classic Window command prompt as Administrator (that’s right don’t use Powershell to create symlinks)</li>
<li>Change directories to the Documents folder for account you want to share profiles</li>
<li>Run the following command</li>
</ol>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="dos" style="font-family:monospace;">c:\Users\u00\Documents<span style="color: #33cc33;">&gt;</span>mklink /D WindowsPowerShell C:\users\Public\Documents\WindowsPowerShell
symbolic link created <span style="color: #00b100; font-weight: bold;">for</span> WindowsPowerShell <span style="color: #33cc33;">&lt;&lt;</span>===<span style="color: #33cc33;">&gt;&gt;</span> C:\users\Public\Documents\WindowsPowerShell</pre></td></tr></table></div>

<p>Repeat steps 4 and 5 for each account.</p>
<p>You now can now share profiles and modules across accounts on the same computer.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/02/sharing-profiles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Sarasota IT Pro Camp</title>
		<link>http://sev17.com/2012/02/sarasota-it-pro-camp/</link>
		<comments>http://sev17.com/2012/02/sarasota-it-pro-camp/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 16:24:10 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10811</guid>
		<description><![CDATA[Join system administrators IT professionals and database professionals in addition to managers at all levels who work with Microsoft technologies for the Sarasota IT Pro Camp on Saturday, February 18th, 2012. IT Pro Camps are focused on serving the needs of  IT Pro&#8217;s (Windows system administrators). IT Pro&#8217;s haven&#8217;t had many events like our developer counterpart&#8217;s...]]></description>
			<content:encoded><![CDATA[<p>Join system administrators IT professionals and database professionals in addition to managers at all levels who work with Microsoft technologies for the <a href="http://itprocamp.com/sarasota" target="_blank">Sarasota IT Pro Camp</a> on <strong>Saturday, February 18th, 2012</strong>.</p>
<p><a href="http://itprocamp.com/" target="_blank">IT Pro Camps</a> are focused on serving the needs of  IT Pro&#8217;s (Windows system administrators). IT Pro&#8217;s haven&#8217;t had many events like our developer counterpart&#8217;s code camps and this is why I&#8217;m excited to be a part of IT Pro Camp. There&#8217;s a definite need for events which serve the IT Pro community. We&#8217;ve done Tampa, South Florida, Orlando and in two weeks we&#8217;ll add Sarasota to our growing list of cities. We&#8217;re also planning on Jacksonville, Pensacola and for the second year we&#8217;ll be returning to Tampa and South Florida. We hope to have dates for the remainder of 2012 finalized by the end of March. I&#8217;ll be sure to post an update once we work out scheduling and venues.</p>
<p>The <a href="http://itprocamp.com/sarasota" target="_blank">Sarasota IT Pro Camp</a> will feature 24 one-hour sessions on topics covering Powershell, BI, SQL Server, Cloud, Active Directory, System Center and Data Security. I&#8217;ll be presenting on <a href="http://itprocamp.com/sarasota/sessions/?session=mp5gmb" target="_blank">Storing Powershell Output</a>. Although there are many good sessions to choose from, I&#8217;d like to highlight a few sessions which peaked my interest:</p>
<p>We&#8221;ll have three sessions, on a topic we haven&#8217;t had at previous camps, Data Security.</p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=1z7lg3">Adam Malone &#8211; Cyber Crime and the FBI</a></p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=rglpy3">Joseph Schorr &#8211; Rule 1: Cardio</a></p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=gky8ff">Jeff Wolach - Introduction to Next Generation Firewalls</a></p>
<p>I think this this is one of the interesting things about having IT Pro Camps in different cities, there tends to be a strong technical community around particular disciplines and for Sarasota I&#8217;d say its Data Security.</p>
<p>Jose Chinchilla  (<a href="http://www.sqljoe.com" target="_blank">blog</a>|<a href="http://twitter.com/SQLJoe" target="_blank">twitter</a>) is doing a two-part session on Business Intelligence.</p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=7c3ddk">Jose Chinchilla - Introduction to Microsoft Business Intelligence</a></p>
<p style="padding-left: 30px;"><a href="http://itprocamp.com/sarasota/sessions/?session=mrrbry">Jose Chinchilla - Taking Business Intelligence to the next level with SharePoint 2010!</a></p>
<p>This is a good opportunity for attendees to get both an introduction and more advanced overview on business intelligence. Two-part series are kind of unique to community events and you tend not to see a two-part series at paid conferences.</p>
<p>Be sure to <a href="http://itprocamp.com/sarasota/register-now/" target="_blank">register</a> to attend the free <a href="http://itprocamp.com/sarasota" target="_blank">Sarasota IT Pro Camp</a> event. A continental breakfast and lunch will be provided. Please tell your colleagues about <a href="http://itprocamp.com/" target="_blank">IT Pro Camps</a>. I look forward to seeing you there!</p>
<p>&#8211;Chad Miller</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/02/sarasota-it-pro-camp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Orlando IT Pro Camp 2012 Presentation</title>
		<link>http://sev17.com/2012/01/orlando-it-pro-camp-2012-presentation/</link>
		<comments>http://sev17.com/2012/01/orlando-it-pro-camp-2012-presentation/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 02:29:00 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Presentations]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10806</guid>
		<description><![CDATA[Presentation and scripts from my Storing Powershell Output session at Orlando IT Pro Camp 2012.]]></description>
			<content:encoded><![CDATA[<p><a title="Presentation and scripts" href="https://skydrive.live.com/redir.aspx?cid=ea42395138308430&amp;resid=EA42395138308430!1011&amp;parid=EA42395138308430!113" target="_blank">Presentation and scripts</a> from my <a title="Storing Powershell Output" href="http://itprocamp.com/orlando/sessions/?session=yyxxk4" target="_blank">Storing Powershell Output</a> session at Orlando IT Pro Camp 2012.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/01/orlando-it-pro-camp-2012-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Orlando IT Pro Camp</title>
		<link>http://sev17.com/2012/01/orlando-it-pro-camp/</link>
		<comments>http://sev17.com/2012/01/orlando-it-pro-camp/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 03:22:23 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[IT Pro Camp]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10799</guid>
		<description><![CDATA[The Orlando IT Pro Camp marks our third event after Tampa and South Florida. As with the prior IT Pro Camps I’m helping put together the Orlando IT Pro Camp. Register to attend this free event on Saturday, January 21st. Orlando will be even bigger than Tampa or South Florida with 30 sessions on topics...]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://itprocamp.com/orlando/" target="_blank">Orlando IT Pro Camp</a> marks our third event after Tampa and South Florida. As with the prior IT Pro Camps I’m helping put together the Orlando IT Pro Camp. <a href="http://orlandoitprocamp.eventbrite.com/" target="_blank">Register to attend</a> this free event on <strong>Saturday, January 21st</strong>. Orlando will be even bigger than Tampa or South Florida with 30 sessions on topics covering Hyper-v, Private Cloud, SharePoint, System Center Products, SQL Server, PowerShell, Active Directory, Windows 2008 R2, and Windows 7 . I’ll be presenting a PowerShell session on <a href="http://itprocamp.com/orlando/sessions/?session=yyxxk4" target="_blank">Storing Powershell Output</a>.  A few sessions I’ve seen previously and highly recommend:</p>
<ul>
<li><a href="http://itprocamp.com/orlando/sessions/?session=prbvcd" target="_blank">Intro to SQL Server for IT Professionals</a> by Michael Wells (<a href="http://sqltechmike.com/" target="_blank">Blog</a>|<a href="http://twitter.com/SqlTechMike" target="_blank">Twitter</a>)—An excellent overview of SQL Server for the system admin SQL noob.</li>
<li><a href="http://itprocamp.com/orlando/sessions/?session=oh126k2" target="_blank">Version Control for IT Professionals</a> by Jason Hofferle (<a href="http://www.hofferle.com/" target="_blank">Blog</a>|<a href="http://twitter.com/jhofferle" target="_blank">Twitter</a>) – A good overview of using Mercurial source control for sys admin scripts.</li>
<li><a href="http://itprocamp.com/orlando/sessions/?session=oh126k" target="_blank">Introduction to PowerShell Remoting</a> also by Jason Hofferle (<a href="http://www.hofferle.com/" target="_blank">Blog</a>|<a href="http://twitter.com/jhofferle" target="_blank">Twitter</a>)  —An eye opening look at the benefits of using Powershell remoting in a large enterprise based on real-world benchmarks.</li>
</ul>
<p>If you can’t make Orlando, the Sarasota IT Pro Camp will be held on Saturday, February 18th. 2012. See the <a href="http://itprocamp.com/" target="_blank">IT Pro Camp site</a> for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2012/01/orlando-it-pro-camp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Importing CSV Files to SQL Server with PowerShell</title>
		<link>http://sev17.com/2011/11/importing-csv-files-to-sql-server-with-powershell/</link>
		<comments>http://sev17.com/2011/11/importing-csv-files-to-sql-server-with-powershell/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 13:02:51 +0000</pubDate>
		<dc:creator>Chad Miller</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQLServerPedia]]></category>
		<category><![CDATA[CSV]]></category>

		<guid isPermaLink="false">http://sev17.com/?p=10792</guid>
		<description><![CDATA[Ed Wilson (Blog&#124;Twitter) aka Scripting Guy is kicking off another guest blogger week  (Nov 28th 2011) with my guest blog post, Four Easy Ways to Import CSV Files to SQL Server with PowerShell. The post demonstrates the following approaches to importing CSVs into a SQL Server table: T-SQL BULK INSERT command LogParser command-line LogParser COM-based scripting...]]></description>
			<content:encoded><![CDATA[<p>Ed Wilson (<a href="http://technet.microsoft.com/en-us/scriptcenter/default.aspx">Blog</a>|<a href="http://twitter.com/scriptingguys/">Twitter</a>) aka Scripting Guy is kicking off another guest blogger week  (Nov 28th 2011) with my guest blog post, <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/28/four-easy-ways-to-import-csv-files-to-sql-server-with-powershell.aspx" target="_blank">Four Easy Ways to Import CSV Files to SQL Server with PowerShell</a>. The post demonstrates the following approaches to importing CSVs into a SQL Server table:</p>
<ul>
<li>T-SQL BULK INSERT command</li>
<li>LogParser command-line</li>
<li>LogParser COM-based scripting</li>
<li>A Windows Powershell-based approach using several functions</li>
</ul>
<div>Most of the time I&#8217;ll I use BULK-INSERT or the Windows Powershell-based approach, although as explained in the post the ability of LogParser to automatically create a SQL table based on a CSV is pretty handy.</div>
]]></content:encoded>
			<wfw:commentRss>http://sev17.com/2011/11/importing-csv-files-to-sql-server-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching using disk: basic
Object Caching 1596/1724 objects using disk: basic

Served from: sev17.com @ 2012-05-09 19:38:01 -->

