{"id":486,"date":"2012-06-18T10:34:00","date_gmt":"2012-06-18T10:34:00","guid":{"rendered":"\/blogs\/bobb\/post\/Using-PowerShell-with-DAC-30.aspx"},"modified":"2012-06-18T10:34:00","modified_gmt":"2012-06-18T10:34:00","slug":"using-powershell-with-dac-3-0","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/","title":{"rendered":"Using PowerShell with DAC 3.0"},"content":{"rendered":"<p>\nAt my DAC talk at TechEd last week, the thing that seemed to cause the most interest was the PowerShell scripts to use DACPACs\/BACPACs. I thought I&#39;d post that here, along a question about cmdlets in general. Using PowerShell against the library allows admins to use all of the functionality that&#39;s in SSMS, with the options available in SqlPackage (think DacOptions object) and more (like BACPACs)&#8230; The premise is that administrators (DBAs or others) might want to integrate this functionality&nbsp;as part of their deployment into a PowerShell script.\n<\/p>\n<p>\nPowerShell doesn&#39;t officially support .NET 4.0 assemblies until PowerShell 3.0, so you&#39;ll need a config file. You&#39;d name this PowerShell.exe.config and place it in the same directory as the PowerShell.exe:\n<\/p>\n<p>\n&lt;?xml version=&quot;1.0&quot;?&gt;<br \/>\n&lt;configuration&gt;<br \/>\n&nbsp; &lt;startup useLegacyV2RuntimeActivationPolicy=&quot;true&quot;&gt;<br \/>\n&nbsp;&nbsp;&nbsp; &lt;supportedRuntime version=&quot;v4.0.30319&quot;\/&gt;<br \/>\n&nbsp;&nbsp;&nbsp; &lt;supportedRuntime version=&quot;v2.0.50727&quot;\/&gt;<br \/>\n&nbsp; &lt;\/startup&gt;<br \/>\n&lt;\/configuration&gt;\n<\/p>\n<p>\nThen the scripts. Granted, I&#39;m using the defaults, not the DacOptions or list of tables to export.\n<\/p>\n<p>\n# load in DAC DLL (requires config file to support .NET 4.0)<br \/>\n# change file location for a 32-bit OS<br \/>\nadd-type -path &quot;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\DAC\\bin\\Microsoft.SqlServer.Dac.dll&quot;\n<\/p>\n<p>\n# make DacServices object, needs a connection string<br \/>\n$d = new-object Microsoft.SqlServer.Dac.DacServices &quot;server=.&quot;\n<\/p>\n<p>\n# register events, if you want &#39;em<br \/>\nregister-objectevent -in $d -eventname Message -source &quot;msg&quot; -action { out-host -in $Event.SourceArgs[1].Message.Message }\n<\/p>\n<p>\n# Extract DACPAC from database<br \/>\n# Extract pubs database to a file using DAC application name pubs, version 1.2.3.4<br \/>\n$d.extract(&quot;c:\\temp\\pubs.dacpac&quot;, &quot;pubs&quot;, &quot;pubs&quot;, &quot;1.2.3.4&quot;)\n<\/p>\n<p>\n# Export schema and data from database pubsdac<br \/>\n$d.exportbacpac(&quot;c:\\temp\\pubsdac.bacpac&quot;, &quot;pubsdac&quot;)\n<\/p>\n<p>\n# Load dacpac from file &amp; deploy to database named pubsnew<br \/>\n$dp = [Microsoft.SqlServer.Dac.DacPackage]::Load(&quot;c:\\temp\\pubs.dacpac&quot;)<br \/>\n$d.deploy($dp, &quot;pubsnew&quot;)\n<\/p>\n<p>\n# Load bacpac from file &amp; import to database named pubsdac2<br \/>\n$bp = [Microsoft.SqlServer.Dac.BacPackage]::Load(&quot;c:\\temp\\pubsdac.bacpac&quot;)<br \/>\n$d.importbacpac($bp, &quot;pubsdac2&quot;)\n<\/p>\n<p>\n# clean up event<br \/>\nunregister-event -source &quot;msg&quot;\n<\/p>\n<p>\n<br \/>\nNow, the question for the reader. There has been a lot of work going on to make sets of APIs into libraries of cmdlets. I thought about this, but it seemed to be such a simple API that I&#39;d be writing a cmdlet encapsulating 2-3 lines of code each time. So when is it useful to &quot;cmdlet-ize&quot; APIs? Always? Or is it too much to cmdlet-ize for every two lines of code? How about&nbsp;5 lines of code? Is there a cutoff? Let me know on Twitter or in comments&#8230;\n<\/p>\n<p>\nThanks to those of you who attended the session.\n<\/p>\n<p>\n@bobbeauch<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At my DAC talk at TechEd last week, the thing that seemed to cause the most interest was the PowerShell scripts to use DACPACs\/BACPACs. I thought I&#39;d post that here, along a question about cmdlets in general. Using PowerShell against the library allows admins to use all of the functionality that&#39;s in SSMS, with the [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,21],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-data-tier-applications-dac","category-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using PowerShell with DAC 3.0 - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using PowerShell with DAC 3.0 - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"At my DAC talk at TechEd last week, the thing that seemed to cause the most interest was the PowerShell scripts to use DACPACs\/BACPACs. I thought I&#039;d post that here, along a question about cmdlets in general. Using PowerShell against the library allows admins to use all of the functionality that&#039;s in SSMS, with the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2012-06-18T10:34:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/\",\"name\":\"Using PowerShell with DAC 3.0 - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2012-06-18T10:34:00+00:00\",\"dateModified\":\"2012-06-18T10:34:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data-Tier Applications (DAC)\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-tier-applications-dac\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Using PowerShell with DAC 3.0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using PowerShell with DAC 3.0 - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/","og_locale":"en_US","og_type":"article","og_title":"Using PowerShell with DAC 3.0 - Bob Beauchemin","og_description":"At my DAC talk at TechEd last week, the thing that seemed to cause the most interest was the PowerShell scripts to use DACPACs\/BACPACs. I thought I&#39;d post that here, along a question about cmdlets in general. Using PowerShell against the library allows admins to use all of the functionality that&#39;s in SSMS, with the [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/","og_site_name":"Bob Beauchemin","article_published_time":"2012-06-18T10:34:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/","name":"Using PowerShell with DAC 3.0 - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2012-06-18T10:34:00+00:00","dateModified":"2012-06-18T10:34:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/using-powershell-with-dac-3-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Data-Tier Applications (DAC)","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-tier-applications-dac\/"},{"@type":"ListItem","position":3,"name":"Using PowerShell with DAC 3.0"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}