【问题标题】:A parameter cannot be found that matches parameter name 'bindings'找不到与参数名称“绑定”匹配的参数
【发布时间】:2018-07-22 16:55:29
【问题描述】:

我正在尝试从 C# 应用程序调用此 PowerShell 脚本。我已经包含了System.Management.Automation 参考。参数作为脚本参数的一部分传入。虽然我传入了所有必需的参数,但它似乎没有设置-Bindings 参数,尽管我传入了在执行期间在脚本中构建该参数值所需的值。

这是简单的 PowerShell 脚本:

Param(
    $WebSiteName,
    $Type,
    $HostName,
    $Port,
    $IPAddress="*",
    $PhysicalPath
)

$BindInfo = $IPAddress + ":" + $port + ":" + $hostname
New-Item IIS:\Sites\$WebSiteName -Bindings @{
    protocol="http";
    bindingInformation=$BindInfo
} -PhysicalPath $PhysicalPath
New-WebBinding -Name $WebSiteName -Protocol $Type -HostHeader ("www." +
    $HostName) -IPAddress $IPAddress -Port $port

这是 C# 控制台应用程序代码:

string text = @"{path}..\CreateSite.ps1 -WebSite testPowershell -Type http - HostName testpowershell.com -Port 80 -IPAddress 10.0.0.4 -PhysicalPath C:\inetpub\testpowershell -bindings 10.0.0.4:80:testpowershell.com";
using (PowerShell PowerShellInstance = PowerShell.Create())
{
    // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
    // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
    PowerShellInstance.AddScript(text);
    Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
    foreach (PSObject outputItem in PSOutput)
    {
        // if null object was dumped to the pipeline during the script then a null
        // object may be present here. check for null to prevent potential NRE.
        if (outputItem != null)
        {
            Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
        }
    }
    if (PowerShellInstance.Streams.Error.Count > 0)
    {
        Console.Write("Error");
    }
    Console.ReadKey();

我不确定为什么没有设置 -Bindings 参数,尽管我在调用脚本时传递了必要的信息。

-Bindings @{
    protocol="http";
    bindingInformation=$BindInfo
}

【问题讨论】:

  • 因为-bindings不是New-Item cmdlet的参数之一。
  • @VivekKumarSingh,如果使用 WebAdministration 模块。 docs.microsoft.com/en-us/iis/manage/powershell/… Op:您是否将该模块导入到您的 powershell 实例中?看起来不像。您也可以在创建新站点后尝试仅使用 New-WebBinding
  • @pinkfloydx33 我已经更新了脚本,在脚本中的参数列表之后包含“Import-Module "WebAdministration"'。现在错误是:“{找不到接受参数'New-WebBinding'的位置参数。}”
  • @pinkfloydx33 感谢您的意见,因为它是答案的“一部分”。

标签: c# powershell


【解决方案1】:

我已经能够弄清楚让这个脚本工作的问题所在。

  1. 首先我必须在 PowerShell 脚本中包含“Import-Module WebAdministration”。
  2. 我必须使用“PowerShellInstance.Commands.AddParameter()”而不是“PowerShellInstance.AddParameter()”。这就是实际将参数成功传递到脚本执行的原因。

    字符串文本 = @"C:\CreateSite.ps1";

        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
            // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
            PowerShellInstance.AddCommand(text);
            PowerShellInstance.Commands.AddParameter("WebSiteName", @"RichSite69");
            PowerShellInstance.Commands.AddParameter("Type", @"http");
            PowerShellInstance.Commands.AddParameter("HostName", @"RichSite69.com");
            PowerShellInstance.Commands.AddParameter("Port", @"80");
            PowerShellInstance.Commands.AddParameter("PhysicalPath", @"C:\inetpub\richsite69");
            PowerShellInstance.Commands.AddParameter("IPAddress", @"10.0.0.4");
            //  -WebSite 'testPowershell' -Type 'http' -HostName 'testpowershell.com' -Port 80 -IPAddress '10.0.0.4' -'
            Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
            foreach (PSObject outputItem in PSOutput)
            {
                // if null object was dumped to the pipeline during the script then a null
                // object may be present here. check for null to prevent poDtential NRE.
                if (outputItem != null)
                {
                    Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
                }
            }
            Console.WriteLine();
            foreach (var error in PowerShellInstance.Streams.Error)
            {
                Console.Write(error.ToString());
            }
            Console.ReadKey();
        }
    

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2019-10-17
    • 2021-05-11
    相关资源
    最近更新 更多