1

我正在尝试在机器上运行一些 IIS 管理脚本 -

OS - Windows 2003(with SP2)
IIS - V6.0
Powershell - V2

但是,当我运行以下命令时,出现错误 -

- 导入模块 WebAdministration

  **Error**:  
    Import-Module : The specified module 'WebAdministration' was not loaded because no valid module file was found in any module directory.
    At line:1 char:14 + Import-Module <<<<  WebAdministration
        + CategoryInfo          : ResourceUnavailable: (WebAdministration:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

- 添加PSSnapIn WebAdministration

  **Error:**  
      Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 2.
        At line:1 char:13 + Add-PSSnapIn <<<<  WebAdministration
        + CategoryInfo          : InvalidArgument: (WebAdministration:String) [Add-PSSnapin], PSArgumentException
        + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

我检查了哪些模块/snapin 可用 - 这是结果 -

获取模块 -ListAvailable

Result:
    BitsTransfer

获取 PSSnapIn

Result:
  Microsoft.PowerShell.Diagnostics
  Microsoft.WSMan.Management  
  Microsoft.PowerShell.Core
  Microsoft.PowerShell.Utility
  Microsoft.PowerShell.Host
  Microsoft.PowerShell.Management
  Microsoft.PowerShell.Security

请指导我应该如何运行 IIS 管理脚本。

4

3 回答 3

0

Below link says Powershell SnapIn is not available for IIS 6.0:

http://forums.iis.net/p/1156851/1903821.aspx#1903821

WMI is the option to go with IIS 6.0 administration.
But WMI is not the option for me as soon we will be upgrading to IIS 7.5

于 2012-08-21T16:12:32.853 回答
0

在 Windows 2k3 平台上的 IIS6 中,我建议您尝试通过旧的 WMI 提供程序(“Microsoftiisv2”)或 ADSI 提供程序访问 IIS,因为两者都可以从 PowerShell 访问。

snapins 和 WebAdministration 模块都不适用于 IIS 6.0,因此我们可以使用 PowerShell 从 PowerShell 访问 IIS6 元数据库

对于IIS7.0,我们可以“导入WebAdministration”模块。

例如,我必须为 IIS6 的虚拟目录设置物理路径,所以我在 c:/Windows/System32 中使用了与 IIS6 一起提供的 vbs 脚本 iisvdir。

复制代码片段

Function resetSiteLocation ($newPath)
{     

Write-Host "List of Virtual directories for the site Test123 before reset :"

C:\WINDOWS\system32\iisvdir /query Test/Test123

Write-Host "About to reset site location"

C:\WINDOWS\system32\iisvdir /delete Test/Test123/Test1

C:\WINDOWS\system32\iisvdir /create Test/Test123 Test1 C:\projects\Test\Test123\Test1

C:\WINDOWS\system32\iisvdir /delete Test/Test123/Test2

C:\WINDOWS\system32\iisvdir /create Test/Test123 Test2 C:\projects\Test\Test123\Test2

Write-Host "Finished to reset site location"

Write-Host "List of Virtual directories for the site Test123 after reset :"

C:\WINDOWS\system32\iisvdir /query Test/Test123

}

由于您将切换到更高版本的 IIS,因此您可以在代码中放置一个开关以确定 IIS 版本并采取适当的措施。

我这样做了:

Write-Host "Checking Installed IIS version:"

$iisVersion = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\InetStp";

Write-host IIS major version : $iisVersion.MajorVersion

Write-host IIS minor version : $iisVersion.MinorVersion

Write-Host "Finished the check."
## IIS inclusion module
## Neither snapins nor WebAdministration module is available for IIS 6.0, so we can access IIS6 metabase 
## from PowerShell using either old WMI provider ("Microsoftiisv2") or ADSI provider as both are accessible from the PowerShell.
## For IIS7.0, we can import WebAdministration module

if (($iisVersion.MajorVersion -eq 7 ) -or ($iisVersion.MajorVersion -ge 7 ))
{
Write-host Detected IIS Major Version : $iisVersion.MajorVersion and Minor version : $iisVersion.MinorVersion. Hence importing WebAdministration module.
Import-Module WebAdministration;


Write-Host "About to reset app pool"
Restart-WebAppPool("Application")
Write-Host "Finished resetting app pool" 
resetSiteLocation

Write-Host "About to reset site"
Restart-WebItem("IIS:\Sites\My application")
Write-Host "Finished to reset site"

}
elseif ($iisVersion.MajorVersion -eq 6) 
{
Write-host IIS version 6 detected. Hence accessing IIS metabase using old WMI provider 

##2. Reset App Pool
Write-Host "About to reset app pool"

Write-Host "Finished resetting app pool" 

##3. Reset site location
resetSiteLocation

##4.Reset site
Write-Host "About to reset site"

Write-Host "Finished to reset site"    
}
else
{
     Write-host Detected IIS $iisVersion.MajorVersion         
}

让我知道它是否对您有帮助。

于 2014-02-26T03:51:55.550 回答
0

以下是有关在 IIS 6 中使用 WMI 接口的一些有用信息:

http://network-nick.blogspot.com/2015/01/powershell-and-iis-6.html

他还指出了适用于此环境的 cmdlet 的 Microsoft 文档,如下所示:

https://docs.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525265(v=vs.90)

在本文中,他开发并解释了以下 PowerShell 脚本,用于列出服务器的网站及其虚拟目录。我实际上尝试过这个并且它有效。

$WebSiteID = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer | Select-Object -ExpandProperty Name

ForEach ( $Site in $WebSiteID ) {
    $WebSiteName = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServerSetting | Where-Object { $_.Name -like "$site" } `
                    | Select-Object -Expandproperty ServerComment

    write-host "`r`n" $WebSiteName

    $AppPath = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebVirtualDirSetting | Where-Object { $_.Name -like "$site/*" } `
                | select -expandproperty path

    $AppPath = $AppPath | select-object -unique | sort-object
    $AppPath
}
于 2020-06-29T19:53:19.700 回答