{"id":608,"date":"2014-02-06T15:50:17","date_gmt":"2014-02-06T15:50:17","guid":{"rendered":"http:\/\/elbsolutions.com\/projects\/?p=608"},"modified":"2022-02-03T11:25:03","modified_gmt":"2022-02-03T17:25:03","slug":"making-powershell-script-run-aspx-worked-12-hour","status":"publish","type":"post","link":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/","title":{"rendered":"Making a powershell script run from aspx &#8211; it worked in 1\/2 hour!"},"content":{"rendered":"<p>So, there is this process, that if the user could click a button themselves would save a lot of stress on everyone &#8211; the person who has to run the powershell script AND the people who have to ask (who wants to pester someone all the time?)<\/p>\n<p>So in prep &#8211; I did a test &#8211; I didn&#8217;t know anything about powershell scripting so I hit google. I found a simple copy\/paste of a file script and modified it to copy a file from one directory to another (and it echos something to the command line). Since it is the www &#8211; I made a share location so that it could be run from the network instead of my &#8220;C:\\temp&#8221; directory because C: means MY c:\\ and MY will change depending on who hits the button.<\/p>\n<p>Next, I went and copied and pasted an example from someone else, found out which &#8220;using&#8221; statements are required, hooked up a following DLL manually required to get this working and presto &#8211; it worked in about 1\/2 hour. Here are the steps:<!--more--><\/p>\n<p>Steps<\/p>\n<ul>\n<li>make a share and a destination directory (might need a share for that too).<\/li>\n<li>Make a new web project or aspx page<\/li>\n<li>Manually connect the following powershell DLL into the project\u00a0C:\\Windows\\assembly\\GAC_MSIL\\System.Management.Automation\\1.0.0.0__31bf3856ad364e35\\System.Management.Automation.dll<\/li>\n<li>make your powershell script and test it (it is included in the code below &#8211; kinda &#8211; in the variable: &#8220;st&#8221;)<\/li>\n<li>fudge your script into the code below &#8211; I already did and in the future, if this goes through I would read it from a text file not encode it like I did &#8211; this was just a test<\/li>\n<li>compile, test it and publish it (ok &#8211; I can write that easier than I did it but &#8230; it works \ud83d\ude42 )<\/li>\n<\/ul>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nusing System;\r\n using System.Collections.Generic;\r\n using System.Linq;\r\n using System.Web;\r\n using System.Web.UI;\r\n using System.Web.UI.WebControls;\r\n using System.Management;\r\n using System.Management.Automation;\r\n using System.Management.Automation.Runspaces;\r\n using System.Text;\r\n using System.Collections;\r\n using System.Collections.ObjectModel;\r\n\r\nnamespace RunPowerShellToCopyDb\r\n {\r\n public partial class _Default : Page\r\n {\r\n protected void Page_Load(object sender, EventArgs e)\r\n {\r\n\r\n}\r\n\r\nprotected void ButtonDoIt_Click(object sender, EventArgs e)\r\n {\r\n string st = &quot;&quot;;\r\n\r\n\/\/ideally we want to read this from a file in the App_Data directory or something but this is good test.\r\n st += &quot;$targetdirectory = \\&quot;\\\\\\\\cdc-ebley2-7\\\\temp\\\\dest\\&quot;\\n&quot;;\r\n\r\nst += &quot;$sourcedirectory = \\&quot;\\\\\\\\cdc-ebley2-7\\\\temp\\&quot;\\n&quot;;\r\n\r\nst += &quot;if (!(Test-Path -path $targetdirectory)) {New-Item $targetdirectory -Type Directory}\\n&quot;;\r\n st += &quot;Copy-Item -Path $sourcedirectory\\\\a.csv -Destination $targetdirectory\\n&quot;;\r\n st += &quot;echo this_is_an_echo_statement_after_the_copy_is_done Look at \\\\\\\\cdc-ebley2-7\\\\temp\\\\dest for a.csv&quot;;\r\n outputItem.InnerHtml = &quot;output: &quot;+RunScript(st);\r\n }\r\n\r\nprivate string RunScript(string scriptText)\r\n {\r\n \/\/ create Powershell runspace\r\n\r\nRunspace runspace = RunspaceFactory.CreateRunspace();\r\n\r\n\/\/ open it\r\n runspace.Open();\r\n \/\/runspace.SessionStateProxy.SetVariable(&quot;DemoForm&quot;, this);\r\n \/\/ create a pipeline and feed it the script text\r\n\r\nPipeline pipeline = runspace.CreatePipeline();\r\n pipeline.Commands.AddScript(scriptText);\r\n\r\n\/\/ add an extra command to transform the script\r\n \/\/ output objects into nicely formatted strings\r\n\r\n\/\/ remove this line to get the actual objects\r\n \/\/ that the script returns. For example, the script\r\n\r\n\/\/ &quot;Get-Process&quot; returns a collection\r\n \/\/ of System.Diagnostics.Process instances.\r\n\r\npipeline.Commands.Add(&quot;Out-String&quot;);\r\n\r\n\/\/ execute the script\r\n PSObject bob;\r\n Collection&lt;PSObject&gt; results = pipeline.Invoke();\r\n\r\n\/\/ close the runspace\r\n\r\nrunspace.Close();\r\n\r\n\/\/ convert the script result into a single string\r\n\r\nStringBuilder stringBuilder = new StringBuilder();\r\n foreach (PSObject obj in results)\r\n {\r\n stringBuilder.AppendLine(obj.ToString());\r\n }\r\n\r\nreturn stringBuilder.ToString();\r\n }\r\n }\r\n }\r\n\r\n<\/pre>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\n&lt;%@ Page Title=&quot;Home Page&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~\/Site.Master&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;RunPowerShellToCopyDb._Default&quot; %&gt;\r\n\r\n&lt;asp:Content runat=&quot;server&quot; ID=&quot;FeaturedContent&quot; ContentPlaceHolderID=&quot;FeaturedContent&quot;&gt;\r\n &lt;section class=&quot;featured&quot;&gt;\r\n &lt;div class=&quot;content-wrapper&quot;&gt;\r\n &lt;hgroup class=&quot;title&quot;&gt;\r\n &lt;h1&gt;&lt;%: Title %&gt;.&lt;\/h1&gt;\r\n &lt;h2&gt;Modify this template to jump-start your ASP.NET application.&lt;\/h2&gt;\r\n &lt;\/hgroup&gt;\r\n &lt;p&gt;\r\n &lt;asp:Button ID=&quot;ButtonDoIt&quot; runat=&quot;server&quot; Text=&quot;Jamie ... Press this button&quot; OnClick=&quot;ButtonDoIt_Click&quot; \/&gt;\r\n\r\n&lt;\/p&gt;\r\n &lt;\/div&gt;\r\n &lt;\/section&gt;\r\n &lt;\/asp:Content&gt;\r\n &lt;asp:Content runat=&quot;server&quot; ID=&quot;BodyContent&quot; ContentPlaceHolderID=&quot;MainContent&quot;&gt;\r\n &lt;h3&gt;Output:&lt;\/h3&gt;\r\n &lt;ol class=&quot;round&quot;&gt;\r\n &lt;li id=&quot;outputItem&quot; runat=&quot;server&quot;&gt;\r\n\r\n&lt;\/li&gt;\r\n\r\n&lt;\/ol&gt;\r\n &lt;\/asp:Content&gt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So, there is this process, that if the user could click a button themselves would save a lot of stress on everyone &#8211; the person who has to run the powershell script AND the people who have to ask (who wants to pester someone all the time?) So in prep &#8211; I did a test [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-608","post","type-post","status-publish","format-standard","hentry","category-general"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Making a powershell script run from aspx - it worked in 1\/2 hour! - ELB Solutions.com Inc.<\/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:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Making a powershell script run from aspx - it worked in 1\/2 hour! - ELB Solutions.com Inc.\" \/>\n<meta property=\"og:description\" content=\"So, there is this process, that if the user could click a button themselves would save a lot of stress on everyone &#8211; the person who has to run the powershell script AND the people who have to ask (who wants to pester someone all the time?) So in prep &#8211; I did a test [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/\" \/>\n<meta property=\"og:site_name\" content=\"ELB Solutions.com Inc.\" \/>\n<meta property=\"article:published_time\" content=\"2014-02-06T15:50:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-03T17:25:03+00:00\" \/>\n<meta name=\"author\" content=\"Etienne Bley\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Etienne Bley\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/\"},\"author\":{\"name\":\"Etienne Bley\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"headline\":\"Making a powershell script run from aspx &#8211; it worked in 1\\\/2 hour!\",\"datePublished\":\"2014-02-06T15:50:17+00:00\",\"dateModified\":\"2022-02-03T17:25:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/\"},\"wordCount\":805,\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/\",\"name\":\"Making a powershell script run from aspx - it worked in 1\\\/2 hour! - ELB Solutions.com Inc.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\"},\"datePublished\":\"2014-02-06T15:50:17+00:00\",\"dateModified\":\"2022-02-03T17:25:03+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/making-powershell-script-run-aspx-worked-12-hour\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Making a powershell script run from aspx &#8211; it worked in 1\\\/2 hour!\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\",\"name\":\"ELB Solutions.com Inc.\",\"description\":\"Bringing all your IT Pieces together\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\",\"name\":\"Etienne Bley\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"caption\":\"Etienne Bley\"},\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/author\\\/etienne-bley\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Making a powershell script run from aspx - it worked in 1\/2 hour! - ELB Solutions.com Inc.","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:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/","og_locale":"en_US","og_type":"article","og_title":"Making a powershell script run from aspx - it worked in 1\/2 hour! - ELB Solutions.com Inc.","og_description":"So, there is this process, that if the user could click a button themselves would save a lot of stress on everyone &#8211; the person who has to run the powershell script AND the people who have to ask (who wants to pester someone all the time?) So in prep &#8211; I did a test [&hellip;]","og_url":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/","og_site_name":"ELB Solutions.com Inc.","article_published_time":"2014-02-06T15:50:17+00:00","article_modified_time":"2022-02-03T17:25:03+00:00","author":"Etienne Bley","twitter_misc":{"Written by":"Etienne Bley","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/#article","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/"},"author":{"name":"Etienne Bley","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"headline":"Making a powershell script run from aspx &#8211; it worked in 1\/2 hour!","datePublished":"2014-02-06T15:50:17+00:00","dateModified":"2022-02-03T17:25:03+00:00","mainEntityOfPage":{"@id":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/"},"wordCount":805,"articleSection":["General"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/","url":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/","name":"Making a powershell script run from aspx - it worked in 1\/2 hour! - ELB Solutions.com Inc.","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/#website"},"datePublished":"2014-02-06T15:50:17+00:00","dateModified":"2022-02-03T17:25:03+00:00","author":{"@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"breadcrumb":{"@id":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/elbsolutions.com\/projects\/making-powershell-script-run-aspx-worked-12-hour\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/elbsolutions.com\/projects\/"},{"@type":"ListItem","position":2,"name":"Making a powershell script run from aspx &#8211; it worked in 1\/2 hour!"}]},{"@type":"WebSite","@id":"https:\/\/elbsolutions.com\/projects\/#website","url":"https:\/\/elbsolutions.com\/projects\/","name":"ELB Solutions.com Inc.","description":"Bringing all your IT Pieces together","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/elbsolutions.com\/projects\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39","name":"Etienne Bley","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","caption":"Etienne Bley"},"url":"https:\/\/elbsolutions.com\/projects\/author\/etienne-bley\/"}]}},"_links":{"self":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/comments?post=608"}],"version-history":[{"count":5,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/608\/revisions"}],"predecessor-version":[{"id":611,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/608\/revisions\/611"}],"wp:attachment":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/media?parent=608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/categories?post=608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/tags?post=608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}