【问题标题】:In C# running the PowerShell script first and then invoke() error在 C# 中首先运行 PowerShell 脚本,然后调用()错误
【发布时间】:2021-12-18 18:54:16
【问题描述】:

我想用 c# 制作打印机安装程序 gui,但我给出了错误。 我的错误如下。enter image description here

System.Management.Automation.CommandNotFoundException: '术语'添加' 未被识别为 cmdlet、函数、脚本文件的名称,或 可运行的程序。检查名称的拼写,或者路径是否 包含,验证路径是否正确,然后重试。'

我的代码如下,我哪里做错了?我正在等待你的帮助。我苦苦挣扎了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!");

            

}
        }
    }

【问题讨论】:

  • 如果您可以将问题中的错误以文本的形式包含在内,这将使这里的人们比一个非常大的图像的链接更容易看到,而且它将帮助人们在未来寻找解决方案同样的错误。
  • 谢谢,已编辑。

标签: c# powershell


【解决方案1】:

该 API 比要求您手动输入每个字符串标记要复杂一些。

AddCommand() 一次获取整个命令名称:

powershell.AddCommand('Add-Printer');

对于命名参数参数,使用AddParameter() 代替AddArgument()

powershell.AddParameter("Name", ad);
powershell.AddParameter("PortName", ip)
// etc...

请注意,我们通常在 PowerShell 脚本中在参数名称前面使用的 - 实际上并不是 名称本身的一部分,因此请不要包含它。


如果您想将多个管道作为单独的语句执行,请在调用 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();
}

【讨论】:

  • 非常感谢。它的运行
猜你喜欢
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 2012-03-12
  • 2018-11-21
  • 2021-10-13
相关资源
最近更新 更多