我一直在为 IIS 构建服务器差异脚本,我发现使用 web-administration 模块比 WMI 获取相同信息要慢得多。我在处理 2003 年日落项目时首先构建了 WMI 模块,但我发现当我开始使用 IIS7 方法时它非常慢。
处理时间——IIS7:348.988,IIS6:10.309(秒)
最大的时间损失是检索 webapp 下每个目录的属性,每 10 个目录需要大约 5 秒或每个 2 秒!WMI 可以在 10 秒内完成整个网站。
. C:\scripts\PS\bits\Get-SelectPropertyArray.ps1
# a helper file that converts a field, alias and expression into a selectable property
. C:\scripts\PS\bits\Get-FlagAsList.ps1
# a helper function to tag a series of boolean properties and convert them into a single csv string with the "name" of the flags turned on
function Get-IIS7ConfigForPSPath
{
[CmdletBinding()]
param (
[string]$PSPath
)
$propsToSelect = @("Name","Path","PSPath")
<# Check the FlagValue datatype because when not configured it returns an object instead of an empty string! #>
$propsToSelect += Get-SelectPropertyArraySet "." "Handlers_AccessFlags" {
$flagValue = (get-webconfigurationProperty -filter /system.webServer/handlers -Name AccessPolicy -PSPath $PSPath)
if ($flagValue.GetType().Name -eq "ConfigurationAttribute")
{ "" } else {$flagValue}
}
<# Check the FlagValue datatype because when not configured it returns an object instead of an empty string! #>
$propsToSelect += Get-SelectPropertyArraySet "." "Access_sslFlags" {
$flagValue = (get-webconfigurationProperty -filter /system.webServer/security/access -Name sslFlags -PSPath $PSPath)
if ($flagValue.GetType().Name -eq "ConfigurationAttribute")
{ "" } else {$flagValue}
}
$propsToSelect += Get-SelectPropertyArraySet "." "Asp_AppAllowClientDebug" {
(get-webconfigurationProperty -filter /system.webServer/asp -Name AppAllowClientDebug -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "Asp_AppAllowDebugging" {
(get-webconfigurationProperty -filter /system.webServer/asp -Name AppAllowDebugging -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "Asp_limits_bufferingLimit" {
(get-webconfigurationProperty -filter /system.webServer/asp/limits -Name bufferingLimit -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "Asp_EnableParentPaths " {
(get-webconfigurationProperty -filter /system.webServer/asp -Name EnableParentPaths -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "Asp_limits_queueTimeout" {
(get-webconfigurationProperty -filter /system.webServer/asp/limits -Name queueTimeout -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "Asp_limits_requestQueueMax" {
(get-webconfigurationProperty -filter /system.webServer/asp/limits -Name requestQueueMax -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "Asp_limits_scriptTimeout" {
(get-webconfigurationProperty -filter /system.webServer/asp/limits -Name scriptTimeout -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "security_auth_Anonymous" {
(get-webconfigurationProperty -filter /system.webServer/security/authentication/anonymousAuthentication -Name Enabled -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "security_auth_Basic" {
(get-webconfigurationProperty -filter /system.webServer/security/authentication/basicAuthentication -name enabled -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "web_limits_ConnectionTimeout" {
(get-webconfigurationProperty -filter /system.applicationHost/webLimits -Name ConnectionTimeout -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "HttpCompression_DoDynamicCompression" {
(get-webconfigurationProperty -filter /system.webServer/httpCompression/scheme -Name DoDynamicCompression -PSPath $PSPath).Value }
$propsToSelect += Get-SelectPropertyArraySet "." "HttpCompression_DoStaticCompression" {
(get-webconfigurationProperty -filter /system.webServer/httpCompression/scheme -Name DoStaticCompression -PSPath $PSPath).Value }
# convert property array sets into selectable properties
$props = $propsToSelect | Get-SelectPropertyArray2
# retrieve the properties, no pipeline input is required as we are doing all the logic in the script blocks
$config = Get-Item $PSPath | select -Property $props
if ( !($config.Path) -or $config.Path -eq $null)
{
$config.Path = $PSPath
}
return $config
}
Get-IIS7ConfigForPSPath -PSPath ("IIS:Sites\{0}" -f $iis7Site.name)
这是杀死我的一个,因为每个虚拟目录我必须这样做 160 多次:
Get-IIS7ConfigForPSPath -PSPath $iis7vwebfolderpath
相比之下,这是我的 WMI 请求:
$start = [System.DateTime]::Now
$numanalyzed = 0
$iis6VWebDirConfig = @()
foreach ($vdir in $iis6VirDirConfig)
{
#retrieve the desired fields for the web directory
$iis6VWebDirConfig += Get-WmiObject -class IIsWebDirectorySetting -Namespace "root/MicrosoftIISv2" `
-Filter ("Name like '"+$vdir.Name+"%'") |
select -Property $props
$numAnalyzed++
$end = [System.DateTime]::Now
$timeSoFar = (NEW-TIMESPAN -Start $Start -End $End).TotalSeconds
$timeremaining = ($iis6VirDirConfig.Count - $numAnalyzed) * ($timeSoFar / $numanalyzed)
"Analyzed {0} so far... took {1} seconds, remaining time {2} seconds" -f $numanalyzed,$timeSoFar,$timeremaining | write-host
"Current Folder: {0}" -f $folder.FullName | Write-Host
}
"Web Directories Found: {0}" -f $iis6VWebDirConfig.Count | Write-Host
$end = [System.DateTime]::Now
"Processed web dirs: {0} took {1} seconds" -f $iis7VWebDirConfig.Count,(NEW-TIMESPAN -Start $Start -End $End).TotalSeconds | write-host | Write-Host
有人知道从 IIS7 元数据库中获取所有这些属性的更好方法吗?从长远来看,我担心 WMI 的前向支持,我发现 WMI 检索时有 1 或 2 个属性不正确。