Powershell IIS 在命令行中有效,但在 asp.net 中无效

Powershell IIS 在命令行中有效,但在 asp.net 中无效

我正在尝试通过 asp.net 运行add-website脚本。我希望能够以管理员身份轻松添加新网站。但我遇到了命令无法识别的问题。

命令行输出:

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 访问它?

答案1

如果我打开 PowerShell,命令行开关New-Website不可用。我需要

Import-Module WebAdministration

首先,所以我怀疑您需要以某种方式在代码中执行此操作。 您是否已将模块配置为自动加载到 shell 中?

相关内容