在命令行中找不到但在 ISE 中可用的 Cmdlet

Ada*_*mon 5 iis powershell cmdlets batch-file powershell-ise

我正在尝试在 Windows Server 2008 R2 VM 上使用 PowerShell 创建 IIS 应用程序和应用程序池。powershell脚本如下:

Param(
    [string] $branchName,
    [string] $sourceFolder
)

if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator"))
{ 
    Write-Warning "You do not have Administrator rights to run this script.     `nPlease re-run this script as an Administrator."
    Exit
}

$appPool = $branchName
$site = "Default Web Site"

#Add APPPool
New-WebAppPool -Name $appPool -Force 

#Create Applications
New-WebApplication -Name $branchName -Site $site -PhysicalPath $sourceFolder -   ApplicationPool $appPool -Force    
Run Code Online (Sandbox Code Playgroud)

如果我在 PowerShell ISE 中运行脚本,它运行良好,但如果我从命令行(或使用命令行的批处理文件)运行它,我会收到错误

术语 New-WebAppPool 不被识别为 cmdlet 的名称...等。

有没有办法在 ISE 中安装 Web 管理 cmdlet 而不是在命令行中?

Ans*_*ers 4

假设您使用的是 PowerShell v2(随 Server 2008 R2 安装的默认版本),正如 @jisaak 所怀疑的那样:您需要WebAdministration在代码中显式导入该模块:

Import-Module WebAdministration
Run Code Online (Sandbox Code Playgroud)

ISE 似乎会自动执行此操作。

另一种选择是升级到 PowerShell v4,当使用其中一个导出的 cmdlet 时,它也会自动导入模块。