Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 找不到与参数名称';绑定';_C#_Powershell - Fatal编程技术网

C# 找不到与参数名称';绑定';

C# 找不到与参数名称';绑定';,c#,powershell,C#,Powershell,我正在尝试从C#应用程序调用此PowerShell脚本。我已经包括了系统.管理.自动化参考资料。这些参数作为脚本参数的一部分传入。虽然我传入了所有必需的参数,但它似乎没有设置-Bindings参数,尽管我传入了在执行期间在脚本中构建该参数值所需的值 下面是简单的PowerShell脚本: Param( $WebSiteName, $Type, $HostName, $Port, $IPAddress=“*”, $PhysicalPath ) $BindInfo=$IPAddress+“:”+$p

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

下面是简单的PowerShell脚本:

Param(
$WebSiteName,
$Type,
$HostName,
$Port,
$IPAddress=“*”,
$PhysicalPath
)
$BindInfo=$IPAddress+“:”+$port+“:”+$hostname
新项目IIS:\Sites\$WebSiteName-绑定@{
协议=“http”;
bindingInformation=$BindInfo
}-PhysicalPath$PhysicalPath
新的WebBinding-Name$WebSiteName-Protocol$Type-HostHeader(“www.”+
$HostName)-IPAddress$IPAddress-Port$Port
以下是C#控制台应用程序代码:

string text=@“{path}..\CreateSite.ps1-网站testPowershell-类型http-主机名testPowershell.com-端口80-IP地址10.0.0.4-物理路径C:\inetpub\testPowershell-绑定10.0.0.4:80:testPowershell.com”;
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
//使用“AddScript”将脚本文件的内容添加到执行管道的末尾。
//使用“AddCommand”将单个命令/cmdlet添加到执行管道的末尾。
PowerShellInstance.AddScript(文本);
集合PSOutput=PowerShellInstance.Invoke();
foreach(PSOutput中的PSObject输出项)
{
//如果在脚本期间将null对象转储到管道中,则为null
//对象可能存在于此处。请检查null以防止潜在的NRE。
if(outputItem!=null)
{
Console.WriteLine(outputItem.BaseObject.ToString()+“\n”);
}
}
如果(PowerShellInstance.Streams.Error.Count>0)
{
控制台。写入(“错误”);
}
Console.ReadKey();
我不确定为什么没有设置
-Bindings
参数,尽管我在调用脚本时传入了必要的信息

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

我已经能够找出让这个脚本工作的问题所在

  • 首先,我必须在PowerShell脚本中包含“导入模块WebAdministration”
  • 我不得不使用“PowerShellInstance.Commands.AddParameter()”而不是“PowerShellInstance.AddParameter()”。这实际上是成功地将参数传递到脚本执行中的原因

    字符串文本=@“C:\CreateSite.ps1”

    使用(PowerShell PowerShellInstance=PowerShell.Create())
    {
    //使用“AddScript”将脚本文件的内容添加到执行管道的末尾。
    //使用“AddCommand”将单个命令/cmdlet添加到执行管道的末尾。
    PowerShellInstance.AddCommand(文本);
    PowerShellInstance.Commands.AddParameter(“WebSiteName”,@“RichSite69”);
    PowerShellInstance.Commands.AddParameter(“类型”@“http”);
    PowerShellInstance.Commands.AddParameter(“主机名”@“RichSite69.com”);
    PowerShellInstance.Commands.AddParameter(“端口”@“80”);
    PowerShellInstance.Commands.AddParameter(“PhysicalPath”,@“C:\inetpub\richsite69”);
    PowerShellInstance.Commands.AddParameter(“IPAddress”,@“10.0.0.4”);
    //-网站“testPowershell”-类型“http”-主机名“testPowershell.com”-端口80-IP地址“10.0.0.4”-地址
    集合PSOutput=PowerShellInstance.Invoke();
    foreach(PSOutput中的PSObject输出项)
    {
    //如果在脚本期间将null对象转储到管道中,则为null
    //对象可能存在于此处。请检查null以防止潜在NRE。
    if(outputItem!=null)
    {
    Console.WriteLine(outputItem.BaseObject.ToString()+“\n”);
    }
    }
    Console.WriteLine();
    foreach(PowerShellInstance.Streams.error中的var错误)
    {
    Console.Write(error.ToString());
    }
    Console.ReadKey();
    }
    

  • 因为
    -bindings
    不是cmdlet的参数之一。@VivekKumarSingh是的,如果使用WebAdministration模块。Op:您是否将该模块导入到powershell实例中?看起来不像。您也可以在创建新的Web绑定后尝试使用New WebBindingsite@pinkfloydx33我已经更新了脚本,包括“导入Modu”le“WebAdministration”位于脚本中的参数列表之后。现在错误是:“{找不到接受参数“New WebBinding.”的位置参数。}”@pinkfloydx33感谢您的输入,因为它是答案的“一部分”。
        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();
        }