我正在制作一个应用程序,将您的powershell脚本发送到您想要的计算机并运行它,如下所示:
这是我的c#脚本:
textBox1.Text = RunScript("Invoke-Command -ComputerName"+ ComputerName +" -ScriptBlock {"+ textBox2.Text +"} -credential"+ textBox5.Text);
我也在powershell中尝试过,所有内容都已填写并且可以正常工作,但它要求获得这样的授权:
我想基本绕过这个并连接到计算机而没有提示或手动完成,如
-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 :(得分:0)
仅提供用户名时,提示凭据是-Credential
的默认行为。
您可以提供凭据对象,而不会提示ConvertTo-SecureString
和New-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))";