0
我正在爲您發送PowerShell腳本到要運行它的計算機上,按照指示在這個應用程序:C#的PowerShell遠程授權提示
這裏是我的C#腳本:
textBox1.Text = RunScript("Invoke-Command -ComputerName"+ ComputerName +" -ScriptBlock {"+ textBox2.Text +"} -credential"+ textBox5.Text);
我也一切試圖在PowerShell中填寫和它的作品,但它要求這樣的授權:
我想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;
}
}
}
UR一個傳奇感謝SOOOO很多 – Squeek
@Squeek使用';'分隔語句,或者使用我給出的嵌套子表達式('$()')的例子,或者在C#代碼中使用逐字字符串文字('@「」')線)。如果答案解決了您的問題,請點擊左邊的複選標記「接受」 –
我試過這個,但是它提出了這個術語'p4swrd'不被識別爲cmdlet,函數,腳本文件或可操作程序。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後重試。 – Squeek