我需要创建一个运行Power-Shell并运行命令Get-Dedupjob
的应用程序,但是我收到了以下错误:
术语" Get-Dedupjob"不被识别为cmdlet的名称, 功能,脚本文件或可操作程序。
虽然在power-shell(显示重复数据删除作业)中运行此命令时提到的命令正常工作。
我认为问题可能是程序没有以管理员身份运行命令。 是否有一些解决方案可以作为管理员运行它?或问题出在哪里?
private void button5_Click(object sender, EventArgs e)
{
// Get-Dedupjob
// 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("Get-Dedupjob");
// 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());
}
textBox1.Text = Convert.ToString(stringBuilder);
}
答案 0 :(得分:3)
您应该导入相应的psd1文件
答案 1 :(得分:0)
private void button5_Click(object sender, EventArgs e)
{
// Get-Dedupjob
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Get-Dedupjob");
// execute the script
Collection<PSObject> results = ps.Invoke();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
textBox1.Text = Convert.ToString(stringBuilder);
// close the runspace
runspace.Close();
}
您需要创建powershell对象才能运行powershell命令。以下是上述工作所需的using语句导入。
using System.Management.Automation; // Windows PowerShell namespace.
using System.Management.Automation.Runspaces; // Windows PowerShell namespace.