Powershell IIS在命令行中工作,但不在asp.net中

我正尝试通过asp.net运行一个add-website脚本。 我希望能够以pipe理员身份轻松添加新网站。 但是我有这个问题,这个命令无法识别。

命令行输出:

 C:\Users\Administrator>powershell new-website cmdlet New-Website at command pipeline position 1 Supply values for the following parameters: Name: 

asp.net输出:

 The term 'new-website' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 

我的代码很简单,重点在于:

  Dim runspace As Runspace = RunspaceFactory.CreateRunspace ' open it runspace.Open() Dim pipeline As Pipeline = runspace.CreatePipeline pipeline.Commands.AddScript("new-website") ' add an extra command to transform the script ' output objects into nicely formatted strings ' remove this line to get the actual objects ' that the script returns. For example, the script ' "Get-Process" returns a collection ' of System.Diagnostics.Process instances. Dim results As Collection(Of PSObject) = pipeline.Invoke ' close the runspace runspace.Close() Dim stringBuilder As StringBuilder = New StringBuilder For Each obj As PSObject In results stringBuilder.AppendLine(obj.ToString) Next Throw New Exception(stringBuilder.ToString) 

任何人都有任何线索,为什么我可以通过命令行访问它,但不是asp.net的PowerShell?

如果我打开PowerShell,那么命令行程序New-Website不可用。 我需要

 Import-Module WebAdministration 

首先,所以我怀疑你需要在你的代码中做到这一点。 你有模块configuration为自动加载你的shell?