[英]How do I recycle an IIS AppPool with Powershell?
I haven't really done any Windows scripting at all, so I am at a loss on how to pull this one off. 我根本没有真正完成任何Windows脚本,所以我对如何解决这个问题感到茫然。 Anyway, basically what we want to do is have a script that will take an argument on which IIS AppPool to recycle.
无论如何,基本上我们想要做的是有一个脚本,它将对哪个IIS AppPool进行回收。 I have done some research on Google and haven't had much success on getting things to work.
我已经对谷歌进行了一些研究,并且在开展工作方面没有取得多大成功。
Here is what I am trying now: 这是我现在正在尝试的:
$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPools" Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"}
$appPool.Recycle()
and the error I get: 我得到的错误:
Get-WmiObject : A parameter cannot be found that matches parameter name '$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"'.
Anyway, it would be nice if I also knew how to debug things like this. 无论如何,如果我也知道如何调试这样的事情会很好。 I already fixed one bug with the original script by doing gwmi -namespace "root\\MicrosoftIISv2" -list.
我已经通过执行gwmi -namespace“root \\ MicrosoftIISv2”-list修复了原始脚本的一个错误。 Any other tips like that one would be great.
像那样的任何其他提示都会很棒。
Thanks! 谢谢!
Update : Here is some more info 更新 :这里有更多信息
$appPool = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPools" | Get-Member
. TypeName: System.Management.ManagementObject#root\MicrosoftIISv2\IIsApplicationPools
Name MemberType Definition
---- ---------- ----------
Caption Property System.String Caption {get;set;}
Description Property System.String Description {get;set;}
InstallDate Property System.String InstallDate {get;set;}
Name Property System.String Name {get;set;}
Status Property System.String Status {get;set;}
__CLASS Property System.String __CLASS {get;set;}
__DERIVATION Property System.String[] __DERIVATION {get;set;}
__DYNASTY Property System.String __DYNASTY {get;set;}
__GENUS Property System.Int32 __GENUS {get;set;}
__NAMESPACE Property System.String __NAMESPACE {get;set;}
__PATH Property System.String __PATH {get;set;}
__PROPERTY_COUNT Property System.Int32 __PROPERTY_COUNT {get;set;}
__RELPATH Property System.String __RELPATH {get;set;}
__SERVER Property System.String __SERVER {get;set;}
__SUPERCLASS Property System.String __SUPERCLASS {get;set;}
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime ScriptMethod System.Object ConvertToDateTime();
Delete ScriptMethod System.Object Delete();
GetType ScriptMethod System.Object GetType();
Put ScriptMethod System.Object Put();
gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPools"
__GENUS : 2
__CLASS : IIsApplicationPools
__SUPERCLASS : CIM_LogicalElement
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : IIsApplicationPools.Name="W3SVC/AppPools"
__PROPERTY_COUNT : 5
__DERIVATION : {CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : IRON
__NAMESPACE : root\MicrosoftIISv2
__PATH : \\IRON\root\MicrosoftIISv2:IIsApplicationPools.Name="W3SVC/A
ppPools"
Caption :
Description :
InstallDate :
Name : W3SVC/AppPools
Status :
Where-Object is a filter that expects something as in input. Where-Object是一个过滤器,它在输入中需要某些东西。 There seems to be a missing pipe , before the where filter .
在过滤器之前,似乎缺少管道 。
Try: 尝试:
$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"}
$appPool.Recycle()
Edit : I noticed that the WMI class was IISApplicationPools , which as you saw, did not show us the Recycle method when piped to Get-Member . 编辑 :我注意到WMI类是IISApplicationPools ,正如您所见,在向Get-Member传送时没有向我们展示Recycle方法。 This needs to be changed to IISApplicationPool (non-plural).
这需要更改为IISApplicationPool (非复数)。 With that change, you are able to use the Recycle method.
通过该更改,您可以使用Recycle方法。 The code above has been updated.
上面的代码已更新。
Using the data from this question I was able to create 2 very useful functions. 使用这个问题的数据,我能够创建2个非常有用的功能。
The code: 编码:
function Get-IisAppPools {
Get-WmiObject -Namespace "root\MicrosoftIISv2" -Class "IIsApplicationPool" -Filter 'name like "W3SVC/APPPOOLS/%"'
| ForEach-Object { $_.Name.ToString().SubString(15) }
}
function Recycle-IisAppPool([string]$appPoolName) {
Invoke-WmiMethod -Name Recycle -Namespace "root\MicrosoftIISv2" -Path "IIsApplicationPool.Name='W3SVC/APPPOOLS/$appPoolName'"
}
You can use these functions like this 你可以使用这样的功能
Recycle-IisAppPool DefaultAppPool
Get-IisAppPools | ? { $_ -match "v4.0$" } | % { Recycle-IisAppPool $_ }
When using get-WMIObject you should probably use -filter instead of piping to Where-Object. 使用get-WMIObject时,您应该使用-filter而不是使用Where-Object。 the filter parameter uses WQL syntax language instead of PowerShell's, so don't let that trip you up.
filter参数使用WQL语法语言而不是PowerShell,所以不要让你绊倒。
$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPools" -filter 'name="W3SVC/APPPOOLS/$appPoolName"'
Having said that putting the pipe there should work, and certainly makes it easier to work with unless you already know WQL. 说过把管道放在那里应该可以工作,除非你已经知道WQL,否则肯定会更容易使用。
这不是特定于Powershell的答案,但iisapp.vbs将列出正在运行的应用程序池,并且有一个/ r标志来回收特定的应用程序池。
You can also use a WQL query to get just the AppPool you want; 您还可以使用WQL查询来获取所需的AppPool; this has the advantage of filtering the results on the WMI side, which is especially handy when getting objects from a remote machine.
这样做的好处是可以在WMI端过滤结果,这在从远程计算机获取对象时尤其方便。
(Get-WmiObject -Query "SELECT * FROM IIsApplicationPool WHERE Name = 'W3SVC/AppPools/$appPoolName'" -Namespace 'root\MicrosoftIISv2').Recycle()
With IIS 8.0 I've found I had to use -namespace root\\webadministration -class ApplicationPool
使用IIS 8.0,我发现我必须使用
-namespace root\\webadministration -class ApplicationPool
For example, to recycle an Application Pool in IIS 8 remotely using PowerShell: 例如,要使用PowerShell远程回收IIS 8中的应用程序池:
As always, please test this first by listing the application pools. 与往常一样,请首先列出应用程序池进行测试。 Just remove the
| where
只需删除
| where
| where
and the first (
from the command: | where
和第一个(
来自命令:
gwmi -comp WebServer01 -namespace root\webadministration -class ApplicationPool
#Recycle app pool by name.
(gwmi -comp WebServer01 -namespace root\webadministration -class ApplicationPool | `
where {$_.Name -eq 'YourAppPool'}).recycle()
And on one line: 在一条线上:
(gwmi -comp WebSserver01 -namespace root\webadministration -class ApplicationPool | where {$_.Name -eq 'YourAppPool'}).recycle()
In Powershell: 在Powershell中:
$pool = Get-IISAppPool -Name <name>
$pool.recycle()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.