首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中,首先运行PowerShell脚本,然后调用()错误

在C#中,首先运行PowerShell脚本,然后调用()错误
EN

Stack Overflow用户
提问于 2021-11-04 14:41:14
回答 1查看 91关注 0票数 0

我想用c#制作打印机安装程序gui,但是我给出了错误。我的错误如下。在这里输入图像描述

System.Management.Automation.CommandNotFoundException:'The term 'Add‘不被识别为cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后再试一次。

我的密码在下面,我在哪里做错了?我在等你的帮助。我已经挣扎了3天,我查看了所有的资源,但我无法找到解决方案。

代码语言:javascript
复制
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!");

            

}
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-04 14:45:45

API比要求您手动输入每个字符串令牌要复杂一些。

AddCommand()立即取下整个命令名:

代码语言:javascript
复制
powershell.AddCommand('Add-Printer');

对于命名参数参数,请使用AddParameter()而不是AddArgument()

代码语言:javascript
复制
powershell.AddParameter("Name", ad);
powershell.AddParameter("PortName", ip)
// etc...

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

如果希望将多个管道作为单独的语句执行,请在调用下一个管道中的第一个命令的AddStatement()之间调用AddCommand()

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69841029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档