title: How to use powershell invoke-command within C#?
body: <p>If I run the below command inside of Powershell, it works as expected</p>
  
  <p><code>invoke-command -computername [name] -scriptblock { ipconfig.exe &gt; c:\ipconfig.txt }</code></p>
  
  <p>But when I try to incorporate this into a c# function, I'm getting this error</p>
  
  <blockquote>
    <p>{"Cannot bind parameter 'ScriptBlock'. Cannot convert the
    \"ipconfig.exe > c:\ipconfig.txt\" value of type \"System.String\" to
    type \"System.Management.Automation.ScriptBlock\"."}</p>
  </blockquote>
  
  <p>even though I'm converting the scriptblock parameter value to a System.Management.Automation.ScriptBlock object? What am I doing incorrect?</p>
  
  <pre><code>    private void btnInstallTest_Click(object sender, EventArgs e)
      {
          List&lt;string&gt; commands = new List&lt;string&gt;();
  
          commands.Add("invoke-command");
          List&lt;Tuple&lt;String, String&gt;&gt; parameters = new List&lt;Tuple&lt;string, string&gt;&gt;();
          parameters.Add(new Tuple&lt;string, string&gt;("computername", @"server"));
          parameters.Add(new Tuple&lt;string, string&gt;("ScriptBlock", @"ipconfig.exe &gt; c:\ipconfig.txt"));
  
          Collection&lt;PSObject&gt; psobject = runRemotePowerShellCommands(commands, parameters, "server", @"domain\user", convertToSecureString("password"));          
      }
  
      private Collection&lt;PSObject&gt; runRemotePowerShellCommands(List&lt;string&gt; commands, List&lt;Tuple&lt;String, String&gt;&gt; parameters, string remoteMachineName, string domainAndUsername, SecureString securePW)
      {
          Collection&lt;PSObject&gt; psobjs = new Collection&lt;PSObject&gt;();
          string result = "";
          string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
          PSCredential psCredential = new PSCredential(domainAndUsername, securePW);
          WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, remoteMachineName, 5985, "/wsman", shellUri, psCredential);
          connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; //.Basic;
  
          using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
          {
              PowerShell powershell = PowerShell.Create();
              for (int i = 0; i &lt; commands.Count; i++)
              {
                  if (commands[i].Contains(";"))
                  {
  
                      string[] commandsSplit = commands[i].Split(';');
                  }
                  else
                  {
                      powershell.AddCommand(commands[i]);
                  }
  
                  System.Management.Automation.ScriptBlock sb = null;
                  if (parameters != null)
                  {
                      foreach (Tuple&lt;string, string&gt; param in parameters)
                      {
                          if (param.Item1.ToLower() == "scriptblock")
                          {
                              sb = ScriptBlock.Create(param.Item2);                                
                              powershell.AddParameter(param.Item1, sb);
                              //powershell.AddParameter(param.Item1, param.Item2);
                          }
                          else
                          {
                              powershell.AddParameter(param.Item1, param.Item2);
                          }
                      }
                  }
  
                  if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                  {
                      // do nothing, the runspace is already open
                  }
                  else
                  {
                      runspace.Open();                        
                      powershell.Runspace = runspace;
                  }
  
                  try
                  {
                      psobjs = powershell.Invoke();
  
                      if (powershell.HadErrors == true)
                      {
                          result = "Failed - " + powershell.Streams.Error[0].ToString();
                          result = result.Replace("\"", "*");
                          psobjs.Add(result);
                      }
                  }
                  catch (Exception ex)
                  {
                      result = "Failed: " + ex.Message;
                      psobjs.Add(result);
                  }
              }
  
              powershell.Commands.Clear();                
          }
  
          return psobjs;
      }
  </code></pre>