title: Running Powershell script with if condition from a .NET app
body: <p>I have a .net application that successfully runs Powershell commands that it pulls from text files- until I tried doing one that is more complicated and contains an if condition. The script works correctly from a PS console but in .NET I only know how to pass in a string for the script, which after reading the file, it adds extra stuff like vblf and even if I take it out, it won't work. Is this even possible?</p>
  
  <blockquote>
    <p>.NET Runtime Error: Server was unable to process request. ---> The
    term 'False' is not recognized as the name of a cmdlet, function,
    script file, or operable program. Check the spelling of the name, or
    if a path was included, verify that the path is correct and try again.</p>
  </blockquote>
  
  <p><strong>.NET code:</strong></p>
  
  <pre class="lang-vb prettyprint-override"><code>'Grab Powershell script from text (.ps1) file
  strScript = File.ReadAllText(ScriptFileName)
  
  'inject the arguments into the script
  strScript = InsertArguments(strScript, Arguments)
  
  'Open the runspace and create a pipeline if it's not already open
  If psRunspace.RunspaceStateInfo.State = RunspaceState.BeforeOpen Then
    psRunspace.Open()
  End If
  
  Dim MyPipeline As Pipeline = psRunspace.CreatePipeline()
  MyPipeline.Commands.AddScript(strScript)
  Dim psResults As Collection(Of PSObject) = MyPipeline.Invoke()
  </code></pre>
  
  <p><strong>Powershell Script</strong>, stored in ps1 file:</p>
  
  <pre><code>new-mailbox -name $argument1 -DisplayName $argument1 -UserPrincipalName $argument2 Room -DomainController $argument5
  if ($argument4 -eq "False") {
     Set-CalendarProcessing $argument1 -BookingWindowInDays 400 -DeleteSubject $false -AutomateProcessing autoaccept -AllBookInPolicy $false -BookInPolicy $argument3 -DomainController $argument5        
  } else {
     Set-CalendarProcessing $argument1 -BookingWindowInDays 400 -DeleteSubject $false -AutomateProcessing autoaccept -AllBookInPolicy $true -DomainController $argument5
  }
  </code></pre>
  
  <p>When this script is read in, here is a substring of what gets pulled into <code>strScript</code>:</p>
  
  <pre><code>-Room -DomainController mcexdct1" &amp; vbLf &amp; "if (False -eq "False") {"
  </code></pre>