找不到与参数名称“绑定”匹配的参数

时间:2018-07-22 16:55:29

标签: c# powershell

我正在尝试从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
}

1 个答案:

答案 0 :(得分:0)

我能够弄清楚使此脚本正常工作的问题所在。

  1. 首先,我必须在PowerShell脚本中包含“导入模块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();
        }