【发布时间】: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