title: Running powershell script on asp.net site
body: <p>I am trying to run a powershell script and have it output to my asp.net site. I have made it work with a very simple script where the only command in the script was </p>
  
  <p><code>Get-Service | Out-String</code></p>
  
  <p>and this output onto my site everything I expected</p>
  
  <p>but when I use the script I actually want info from it doesn't output anything
  I can tell it runs (or trys to run) because when my site hits the code that invokes the script it hangs about 10 seconds.</p>
  
  <p>The script I am trying to run is</p>
  
  <pre><code>$user = "user"
  $token = "token"
  
  $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
  
  $result = Invoke-WebRequest -Method Get -Uri 'https://site.vsrm.visualstudio.com/defaultcollection/product/_apis/release/releases?definitionId=1&amp;api-version=3.0-preview.2&amp;$expand=environments' -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
  
  $releaseArr = $result.Content | ConvertFrom-Json
  [System.Collections.ArrayList]$enviromentName = @()
  [System.Collections.ArrayList]$latestRelease = @()
  
  
  foreach($env in $releaseArr.value[0].environments)
  {
      $enviromentName.Add($env.name) | Out-Null
  }
  
  
  foreach($releaseValue in $releaseArr.value)
  {
      For($i = 0; $i -lt $enviromentName.Count; $i++)
      {
          if($latestRelease[$i] -eq $null)
          {
              foreach($release in $releaseValue.environments)
              {
                  if($release.name -eq $enviromentName[$i] -and $release.status -eq "succeeded")
                  {
                      $latestRelease.Add($releaseValue.name) | Out-Null
                  }
              }
          }
      }
  }
  
  For($i = 0; $i -lt $enviromentName.Count; $i++)
  {
      Write-Host $enviromentName[$i] " : " $latestRelease[$i]
  }
  </code></pre>
  
  <p>I know this script runs and outputs, but is there some code in this script that would cause it to not output properly.</p>
  
  <p>The code in my asp.net site I am using to call the script is</p>
  
  <pre><code>ResultBox.Text = string.Empty;
  
          // Initialize PowerShell engine
          var shell = PowerShell.Create();
  
          // Add the script to the PowerShell object
          shell.Commands.AddScript(@"C:\Users\user\Desktop\script.ps1");
  
          // Execute the script
          var results = shell.Invoke();
  
          // display results, with BaseObject converted to string
          // Note : use |out-string for console-like output
          if (results.Count &gt; 0)
          {
              // We use a string builder ton create our result text
              var builder = new StringBuilder();
  
              foreach (var psObject in results)
              {
                  // Convert the Base Object to a string and append it to the string builder.
                  // Add \r\n for line breaks
                  builder.Append(psObject.BaseObject.ToString() + "\r\n");
              }
  
              // Encode the string in HTML (prevent security issue with 'dangerous' caracters like &lt; &gt;
              ResultBox.Text = Server.HtmlEncode(builder.ToString());
          }
  </code></pre>