我正在尝试使用 -
在计算机上运行一些IIS管理脚本OS - Windows 2003(with SP2)
IIS - V6.0
Powershell - V2
然而,当我运行以下命令时,我收到错误 -
- 导入模块网络管理
**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
- Add-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
我检查了哪些模块/管理单元可用 - 这是结果 -
Get-Module -ListAvailable
Result:
BitsTransfer
GET-PSSnapIn
Result:
Microsoft.PowerShell.Diagnostics
Microsoft.WSMan.Management
Microsoft.PowerShell.Core
Microsoft.PowerShell.Utility
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
请指导我该怎么做才能运行IIS管理脚本。
答案 0 :(得分:0)
以下链接表示Powershell SnapIn不适用于IIS 6.0:
http://forums.iis.net/p/1156851/1903821.aspx#1903821
WMI是IIS 6.0管理的选项 但是,我们很快就会升级到IIS 7.5
,WMI不是我的选择答案 1 :(得分:0)
在Windows 2k3平台上的IIS6中,我建议您尝试通过旧的WMI提供程序(“Microsoftiisv2”)或ADSI提供程序访问IIS,因为两者都可以从PowerShell访问。
对于IIS 6.0,snapins和WebAdministration模块都不可用,因此我们可以使用
从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
}
让我知道它是否对你有所帮助。
答案 2 :(得分: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
}