2016-06-07 88 views
0

我正在为您发送PowerShell脚本到要运行它的计算机上,按照指示在这个应用程序:C#的PowerShell远程授权提示

my powershell app

这里是我的C#脚本:

textBox1.Text = RunScript("Invoke-Command -ComputerName"+ ComputerName +" -ScriptBlock {"+ textBox2.Text +"} -credential"+ textBox5.Text); 

我也一切试图在PowerShell中填写和它的作品,但它要求这样的授权:

authorization prompt

我想basiclly绕过这个连接到计算机没有提示或手动完成像
-credentials用户名,但也密码。

或者发生这种情况时,打开此窗体的ontop提示。下面是完整的脚本:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Collections.ObjectModel; 
using System.Management.Automation; 
using System.Management.Automation.Runspaces; 
using System.IO; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 

namespace PowershellOpen 
{ 
    public partial class Florm1 : Form 
    { 
     public bool gg = false; 
     public string ComputerName; 
     public string CustomScript; 
     public string password; 
     public string user; 

     public Florm1() 
     { 
      InitializeComponent(); 
     } 
     private string RunScript(string scriptText) 
     { 
      // create Powershell runspace 
      Runspace runspace = RunspaceFactory.CreateRunspace(); 

      // open it 
      runspace.Open(); 

      // create a pipeline and feed it the script text 
      Pipeline pipeline = runspace.CreatePipeline(); 
      pipeline.Commands.AddScript(scriptText); 

      pipeline.Commands.Add("Out-String"); 

      // execute the script 
      Collection<PSObject> results = pipeline.Invoke(); 

      // close the runspace 
      runspace.Close(); 

      // convert the script result into a single string 
      StringBuilder stringBuilder = new StringBuilder(); 
      foreach (PSObject obj in results) 
      { 
       stringBuilder.AppendLine(obj.ToString()); 
      } 

      // return the results of the script that has 
      // now been converted to text 
      return stringBuilder.ToString(); 
     } 
     // helper method that takes your script path, loads up the script 
     // into a variable, and passes the variable to the RunScript method 
     // that will then execute the contents 
     private string LoadScript(string filename) 
     { 
      try 
      { 
       // Create an instance of StreamReader to read from our file. 
       // The using statement also closes the StreamReader. 
       using (StreamReader sr = new StreamReader(filename)) 
       { 

        // use a string builder to get all our lines from the file 
        StringBuilder fileContents = new StringBuilder(); 

        // string to hold the current line 
        string curLine; 

        // loop through our file and read each line into our 
        // stringbuilder as we go along 
        while ((curLine = sr.ReadLine()) != null) 
        { 
         // read each line and MAKE SURE YOU ADD BACK THE 
         // LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF 
         fileContents.Append(curLine + "\n"); 
        } 


        return fileContents.ToString(); 
       } 
      } 
      catch (Exception e) 
      { 
       string errorText = "The file could not be read:"; 
       errorText += e.Message + "\n"; 
       return errorText; 
      } 

     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      textBox1.Text = RunScript("Invoke-Command -ComputerName"+ ComputerName +" -ScriptBlock {"+ textBox2.Text +"} -credential"+ textBox5.Text); 
      textBox1.BackColor = Color.DeepSkyBlue; 


     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      textBox3.Text = ComputerName; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      textBox1.Text = "Succesfully Cleared"; 
      textBox1.BackColor = Color.LightGray; 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      textBox1.Text = "HELP: The way this launcher works is it launches the powershell scripts,(you can find these in the folder this comes with) this makes using powershell alot easier and accesible. This is a easy place to find all your scripts."; 
      textBox1.BackColor = Color.LightSeaGreen; 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void textBox2_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void label3_Click(object sender, EventArgs e) 
     { 

     } 

     private void button5_Click(object sender, EventArgs e) 
     { 
      textBox1.Text = RunScript(LoadScript(@textBox3.Text)); 
       textBox1.BackColor = Color.DeepSkyBlue; 


     } 

     private void textBox2_TextChanged_1(object sender, EventArgs e) 
     { 

     } 
     [DllImport("user32.dll")] 
     static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

     private void label4_Click(object sender, EventArgs e) 
     { 

     } 

     private void textBox3_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void textBox5_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void textBox4_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      textBox1.Text = RunScript("Test-WsMan "+textBox3.Text); 
      textBox1.BackColor = Color.DeepSkyBlue; 
     } 
    } 
} 

回答

0

提示输入凭据时,只需用户名提供的-Credential的默认行为。

你可以提供一个凭据对象而不ConvertTo-SecureStringNew-Object提示输入凭据:

$Username = 'squeek' 
$Password = 'p4swrd' |ConvertTo-SecureString -AsPlainText -Force 

$Credential = New-Object pscredential -ArgumentList $Username,$Password 

在您压实单行的例子,这将是这样的:

"Invoke-Command -ComputerName"+ ComputerName +" -ScriptBlock {"+ textBox2.Text +"} -credential $(New-Object System.Management.Automation.PSCredential -ArgumentList '" + usernameTextBox.Text + "',$('" + passwordTextBox.Text + "' |ConvertTo-SecureString -AsPlainText -Force))"; 
+0

UR一个传奇感谢SOOOO很多 – Squeek

+0

@Squeek使用';'分隔语句,或者使用我给出的嵌套子表达式('$()')的例子,或者在C#代码中使用逐字字符串文字('@“”')线)。如果答案解决了您的问题,请点击左边的复选标记“接受” –

+0

我试过这个,但是它提出了这个术语'p4swrd'不被识别为cmdlet,函数,脚本文件或可操作程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 – Squeek