[英]In C# running the PowerShell script first and then invoke() error
I would like to make printer-installer gui with c#, but I given error.我想用 c# 制作打印机安装程序 gui,但我给出了错误。 my error is as below.我的错误如下。 enter image description here在此处输入图片说明
System.Management.Automation.CommandNotFoundException: 'The term 'Add' is not recognized as the name of a cmdlet, function, script file, or operable program. System.Management.Automation.CommandNotFoundException:“术语“添加”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
my codes are below, where could i be doing wrong?我的代码在下面,我哪里做错了? I'm waiting for your help please.我正在等待你的帮助。 I've been struggling for 3 days, I looked at all the resources but I couldn't find a solution.我已经挣扎了 3 天,我查看了所有资源,但找不到解决方案。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace son1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ekle_Click(object sender, EventArgs e)
{
using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
{
powershell.AddCommand("Add");
powershell.AddArgument("-");
powershell.AddArgument("PrinterPort");
powershell.AddArgument("-");
powershell.AddArgument("name");
powershell.AddArgument(printer_ip);
powershell.AddArgument("-");
powershell.AddArgument("PrinterHostAddress");
powershell.AddArgument(printer_ip);
powershell.Invoke();
}
using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
{
powershell.AddCommand("Add");
powershell.AddArgument("-");
powershell.AddArgument("Printer");
powershell.AddArgument("-");
powershell.AddArgument("Name");
powershell.AddArgument(printer_name);
powershell.AddArgument("-");
powershell.AddArgument("PortName");
powershell.AddArgument(printer_ip);
powershell.AddArgument("-");
powershell.AddArgument("DriverName");
powershell.AddArgument("Canon Generic Plus PCL6");
powershell.Invoke();
}
System.Windows.MessageBox.Show("Success!");
}
}
}
The API is a little more sophisticated than requiring you to input every single string token manually.该 API 比要求您手动输入每个字符串标记要复杂一些。
AddCommand()
takes the whole command name at once: AddCommand()
获取整个命令名称:
powershell.AddCommand('Add-Printer');
For named parameter arguments, use AddParameter()
instead of AddArgument()
:对于命名参数参数,使用AddParameter()
而不是AddArgument()
:
powershell.AddParameter("Name", ad);
powershell.AddParameter("PortName", ip)
// etc...
Note that the -
that we usually use in front of parameter names in PowerShell scripts is not actually part of the name itself , so don't include that.请注意,我们通常在 PowerShell 脚本中的参数名称前面使用的-
实际上并不是名称本身的一部分,因此不要包含它。
If you want to execute multiple pipelines as separate statements, call AddStatement()
in between the call to AddCommand()
for the first command in the next pipeline:如果要将多个管道作为单独的语句执行,请在对下一个管道中的第一个命令的AddCommand()
调用之间调用AddStatement()
:
using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
{
// call `Add-PrinterPort ...`
powershell.AddCommand("Add-PrinterPort");
powershell.AddParameter("Name", printer_ip);
powershell.AddParameter("PrinterHostAddress", printer_ip);
// terminate previous statement (equivalent to a newline or `;` in powershell)
powershell.AddStatement();
// then call `Add-Printer ...`
powershell.AddCommand("Add-Printer");
powershell.AddParameter("Name", printer_name);
powershell.AddParameter("PortName", printer_ip);
powershell.AddParameter("DriverName", "Canon Generic Plus PCL6");
// Invoke the whole thing at once
powershell.Invoke();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.