IIS在Powershell中启用HTTP保持活动状态

IIS在Powershell中启用HTTP保持活动状态,iis,powershell,wmi,keep-alive,Iis,Powershell,Wmi,Keep Alive,该页说明如何启用HTTP保持活动响应标头(IIS 7) 我想通过WMI在Powershell中执行此操作 它说: 使用以下WMI类、方法或属性执行此操作 程序: HTTPProtocolSection.AllowKeepAlive属性 我试过: PS > Get-WmiObject -Class HTTPProtocolSection Get-WmiObject : Invalid class At line:1 char:14 + Get-WmiObject <<<&l

该页说明如何启用HTTP保持活动响应标头(IIS 7)

我想通过WMI在Powershell中执行此操作

它说:

使用以下WMI类、方法或属性执行此操作 程序: HTTPProtocolSection.AllowKeepAlive属性

我试过:

PS > Get-WmiObject -Class HTTPProtocolSection
Get-WmiObject : Invalid class
At line:1 char:14
+ Get-WmiObject <<<<  -Class HTTPProtocolSection
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
PS>Get WmiObject-Class-HTTPProtocolSection
获取WMIOObject:无效的类
第1行字符:14

+获取WmiObject以发现特定命名空间中的类尝试以下操作

PS c:\>Get-WmiObject -List * "root\webadministration" 
要想找到匹配的人,就这么做

PS c:\>Get-WmiObject -List * "root\webadministration" | Where-Object {$_.name -match "Http"}


PS C:\>Get-WmiObject -Namespace "root\webadministration" -class HttpProtocolSection | Get-Member

TypeName: System.Management.ManagementObject#root\webadministration\HttpProtocolSection

Name                MemberType    Definition
----                ----------    ----------
PSComputerName      AliasProperty PSComputerName = __SERVER
Add                 Method        System.Management.ManagementBaseObject Add(System.String CollectionName, System.Ma...
Clear               Method        System.Management.ManagementBaseObject Clear(System.String CollectionName)
Get                 Method        System.Management.ManagementBaseObject Get(System.String CollectionName, System.St...
Remove              Method        System.Management.ManagementBaseObject Remove(System.String CollectionName, System...
RevertToParent      Method        System.Management.ManagementBaseObject RevertToParent(System.String PropertyName)
AllowKeepAlive      Property      bool AllowKeepAlive {get;set;}
CustomHeaders       Property      System.Management.ManagementObject#CustomHeaderSettings CustomHeaders {get;set;}
Location            Property      string Location {get;set;}
Path                Property      string Path {get;set;}
RedirectHeaders     Property      System.Management.ManagementObject#RedirectHeaderSettings RedirectHeaders {get;set;}
SectionInformation  Property      System.Management.ManagementObject#SectionInformation SectionInformation {get;set;}
__CLASS             Property      string __CLASS {get;set;}
__DERIVATION        Property      string[] __DERIVATION {get;set;}
__DYNASTY           Property      string __DYNASTY {get;set;}
__GENUS             Property      int __GENUS {get;set;}
__NAMESPACE         Property      string __NAMESPACE {get;set;}
__PATH              Property      string __PATH {get;set;}
__PROPERTY_COUNT    Property      int __PROPERTY_COUNT {get;set;}
__RELPATH           Property      string __RELPATH {get;set;}
__SERVER            Property      string __SERVER {get;set;}
__SUPERCLASS        Property      string __SUPERCLASS {get;set;}
ConvertFromDateTime ScriptMethod  System.Object ConvertFromDateTime();
ConvertToDateTime   ScriptMethod  System.Object ConvertToDateTime();
然后可以执行类似的操作来获取AllowKeepAlive的值

PS C:\> (get-wmiobject -namespace "root\webadministration" -class HttpProtocolSection).AllowKeepAlive 
True

PS C:\>$a = Get-WmiObject -Namespace "root\webadministration" -class HttpProtocolSection
PS C:\>$a.AllowKeepAlive = $false
PS C:\>$a.Put()

要在特定命名空间中发现类,请尝试以下操作

PS c:\>Get-WmiObject -List * "root\webadministration" 
要想找到匹配的人,就这么做

PS c:\>Get-WmiObject -List * "root\webadministration" | Where-Object {$_.name -match "Http"}


PS C:\>Get-WmiObject -Namespace "root\webadministration" -class HttpProtocolSection | Get-Member

TypeName: System.Management.ManagementObject#root\webadministration\HttpProtocolSection

Name                MemberType    Definition
----                ----------    ----------
PSComputerName      AliasProperty PSComputerName = __SERVER
Add                 Method        System.Management.ManagementBaseObject Add(System.String CollectionName, System.Ma...
Clear               Method        System.Management.ManagementBaseObject Clear(System.String CollectionName)
Get                 Method        System.Management.ManagementBaseObject Get(System.String CollectionName, System.St...
Remove              Method        System.Management.ManagementBaseObject Remove(System.String CollectionName, System...
RevertToParent      Method        System.Management.ManagementBaseObject RevertToParent(System.String PropertyName)
AllowKeepAlive      Property      bool AllowKeepAlive {get;set;}
CustomHeaders       Property      System.Management.ManagementObject#CustomHeaderSettings CustomHeaders {get;set;}
Location            Property      string Location {get;set;}
Path                Property      string Path {get;set;}
RedirectHeaders     Property      System.Management.ManagementObject#RedirectHeaderSettings RedirectHeaders {get;set;}
SectionInformation  Property      System.Management.ManagementObject#SectionInformation SectionInformation {get;set;}
__CLASS             Property      string __CLASS {get;set;}
__DERIVATION        Property      string[] __DERIVATION {get;set;}
__DYNASTY           Property      string __DYNASTY {get;set;}
__GENUS             Property      int __GENUS {get;set;}
__NAMESPACE         Property      string __NAMESPACE {get;set;}
__PATH              Property      string __PATH {get;set;}
__PROPERTY_COUNT    Property      int __PROPERTY_COUNT {get;set;}
__RELPATH           Property      string __RELPATH {get;set;}
__SERVER            Property      string __SERVER {get;set;}
__SUPERCLASS        Property      string __SUPERCLASS {get;set;}
ConvertFromDateTime ScriptMethod  System.Object ConvertFromDateTime();
ConvertToDateTime   ScriptMethod  System.Object ConvertToDateTime();
然后可以执行类似的操作来获取AllowKeepAlive的值

PS C:\> (get-wmiobject -namespace "root\webadministration" -class HttpProtocolSection).AllowKeepAlive 
True

PS C:\>$a = Get-WmiObject -Namespace "root\webadministration" -class HttpProtocolSection
PS C:\>$a.AllowKeepAlive = $false
PS C:\>$a.Put()

您还可以使用
set-WebConfiguration
cmdlet进行设置:

Set-WebConfiguration -Filter system.webServer/httpProtocol -PSPath MACHINE/WEBROOT/APPHOST -Value @{allowKeepAlive=$true}

您还可以使用
set-WebConfiguration
cmdlet进行设置:

Set-WebConfiguration -Filter system.webServer/httpProtocol -PSPath MACHINE/WEBROOT/APPHOST -Value @{allowKeepAlive=$true}

非常感谢,谢伊。我最终使用了这个解决方案,尽管它并不像我最初问的那样基于WMI。这里要向像我这样的新手提及的唯一一件事是,您需要运行导入模块WebAdministration。另外,下面的命令可能更简单:Set WebConfigurationProperty“/system.webServer/httpProtocol”-name allowKeepAlive-value“true”是的,我应该提到导入模块:)我得到“在这个对象上找不到属性‘allowKeepAlive’。请验证该属性存在并且可以设置。”一切看起来都很好。思想?AllowKeepAlive属性bool AllowKeepAlive{get;set;}非常感谢,Shay。我最终使用了这个解决方案,尽管它并不像我最初问的那样基于WMI。这里要向像我这样的新手提及的唯一一件事是,您需要运行导入模块WebAdministration。另外,下面的命令可能更简单:Set WebConfigurationProperty“/system.webServer/httpProtocol”-name allowKeepAlive-value“true”是的,我应该提到导入模块:)我得到“在这个对象上找不到属性‘allowKeepAlive’。请验证该属性存在并且可以设置。”一切看起来都很好。思想?AllowKeepAlive属性bool AllowKeepAlive{get;set;}