【问题标题】:Cmdlets not found in command line but available in ISE在命令行中找不到但在 ISE 中可用的 Cmdlet
【发布时间】:2016-04-23 09:26:35
【问题描述】:

我正在尝试在 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    

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

New-WebAppPool 一词未被识别为 cmdlet 的名称...等等。

有没有办法可以在 ISE 中安装 Web 管理 cmdlet,但不能在命令行中安装?

【问题讨论】:

  • 请显示您正在运行的确切代码以及代码产生的确切错误。同时显示$PSVersionTable的输出。
  • 您可能需要显式导入 cmdlet:Import-Module WebAdministration

标签: powershell batch-file iis powershell-ise cmdlets


【解决方案1】:

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

Import-Module WebAdministration

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

另一个选项是upgrade to PowerShell v4,它还会在使用其中一个导出的 cmdlet 时自动导入模块。

【讨论】:

    【解决方案2】:

    对于任何感兴趣的人来说,答案都非常简单。正如建议的那样,解决方案是导入模块,但有一个问题是在关闭控制台并重新打开它后它不可用。为了解决这个问题,我只需将该行添加到 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"
    
    *Import-Module WebAdministration*
    
    #Add APPPool
    New-WebAppPool -Name $appPool -Force 
    
     #Create Applications
    New-WebApplication -Name $branchName -Site $site -PhysicalPath $sourceFolder -   ApplicationPool $appPool -Force    
    

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2016-12-12
      • 2019-07-31
      • 2022-01-27
      • 2021-12-17
      • 2014-01-30
      相关资源
      最近更新 更多