如何使用Powershell回收IIS AppPool? [英] How do I recycle an IIS AppPool with Powershell?

查看:107
本文介绍了如何使用Powershell回收IIS AppPool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根本还没有完成任何Windows脚本编写工作,因此我对如何实现这一功能一无所知.无论如何,基本上我们想要做的是有一个脚本,该脚本将带有一个参数,以决定对哪个IIS AppPool进行回收.我已经在Google上进行了一些研究,但在使事情正常进行方面并没有取得太大的成功.

I haven't really done any Windows scripting at all, so I am at a loss on how to pull this one off. Anyway, basically what we want to do is have a script that will take an argument on which IIS AppPool to recycle. 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()

和我得到的错误:

Get-WmiObject : A parameter cannot be found that matches parameter name '$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"'.

无论如何,如果我也知道如何调试这样的事情,那就太好了.我已经通过执行gwmi -namespace"root \ MicrosoftIISv2" -list修复了原始脚本的一个错误.像这样的任何其他技巧都很好.

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. Any other tips like that one would be great.

谢谢!

更新:这是更多信息

$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 是一个过滤器,期望输入中包含某些内容.在 where过滤器之前,似乎缺少了管道.

Where-Object is a filter that expects something as in input. There seems to be a missing pipe, before the where filter.

尝试:

$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"}
$appPool.Recycle()

编辑:我注意到WMI类是 IISApplicationPools ,如您所见,当通过管道传输到 Get-Member .这需要更改为 IISApplicationPool (非复数).进行此更改后,您就可以使用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. This needs to be changed to IISApplicationPool (non-plural). With that change, you are able to use the Recycle method. The code above has been updated.

这篇关于如何使用Powershell回收IIS AppPool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆