简体   繁体   English

Powershell调用命令和IISConfigCollection

[英]Powershell Invoke-Command & IISConfigCollection

I want to get Informations about IIS Application Pools from another Server. 我想从另一台服务器获取有关IIS应用程序池的信息。

This should be possible with Invoke-Command. 使用Invoke-Command应该可以做到。 But theres a behavior i do not unterstand... 但是有一种我不理解的行为...

When i execute on the local machine the script block looks like this: 当我在本地计算机上执行时,脚本块如下所示:

# get the values recorded under Recycle
$ConfigSection = Get-IISConfigSection -SectionPath "system.applicationHost/applicationPools"
$SitesCollection = Get-IISConfigCollection -ConfigElement $ConfigSection
$Site = Get-IISConfigCollectionElement -ConfigCollection $SitesCollection -ConfigAttribute @{"name" = "$AppPool"}
$recycling = Get-IISConfigElement -ConfigElement $Site -ChildElementName "recycling"
$flags = $recycling.Attributes["logEventOnRecycle"].Value

$onRecycle = @{
    'Time'           = [bool]($flags -band 1)     # Specific Time
    'Requests'       = [bool]($flags -band 2)     # Request Limit Exceeded
    'Schedule'       = [bool]($flags -band 4)     # Regular Time Interval
    'Memory'         = [bool]($flags -band 8)     # Virtual Memory Limit Exceeded
    'IsapiUnhealthy' = [bool]($flags -band 16)    # Isapi Reported Unhealthy
    'OnDemand'       = [bool]($flags -band 32)    # Manual Recycle
    'ConfigChange'   = [bool]($flags -band 64)    # Application Pool Configuration Changed
    'PrivateMemory'  = [bool]($flags -band 128)   # Private Memory Limit Exceeded
}

This works, i can use this as "$onRecycle.Time" ... 这有效,我可以将其用作"$onRecycle.Time" ...

But no i want to get the Information from a Remote computer. 但是,我不希望从远程计算机获取信息。

My first thought was to just wrap the whole thing into a $command variable. 我的第一个想法是将整个内容包装到$ command变量中。 Of course it didnt work.. It generates the following errors: 当然它没有用..它会产生以下错误:

  • Cannot validate argument on parameter 'ConfigElement'. 无法验证参数“ ConfigElement”上的参数。 The argument is null. 参数为空。 Provide a valid value for the argument, and then try running the command again. 为参数提供有效值,然后尝试再次运行命令。

  • Cannot index into a null array. 无法索引为空数组。

Then i thought ill do it step for step. 然后我以为病就一步一步来。 Starting with: 从...开始:

$s = "chvmes01"    
$command1 = { Get-IISConfigSection -SectionPath "system.applicationHost/applicationPools" }
    $ConfigSection = Invoke-Command -ComputerName $s -ScriptBlock $command1

This works and gets the desired information, but already on the next step there are troubles again: 这可以正常工作并获取所需的信息,但是在下一步中又遇到了麻烦:

$command2 = { param($ConfigSection)(Get-IISConfigCollection -ConfigElement $ConfigSection) }
$SitesCollection = Invoke-Command -ComputerName $s -ScriptBlock $command2 -ArgumentList $ConfigSection

Here i try to include the Variable $ConfigSection but its giving me the following error back: 在这里,我尝试包含变量$ ConfigSection,但它给了我以下错误:

Cannot bind parameter 'ConfigElement'. 无法绑定参数“ ConfigElement”。 Cannot convert the "Microsoft.Web.Administration.ConfigurationSection" value of type "Deserialized.Microsoft.Web.Administration.ConfigurationSection" to type "Microsoft.Web.Administration.ConfigurationElement". 无法将类型为“ Deserialized.Microsoft.Web.Administration.ConfigurationSection”的“ Microsoft.Web.Administration.ConfigurationSection”值转换为类型“ Microsoft.Web.Administration.ConfigurationElement”。

I thought maybe the reason is that i need Import-Module WebAdministration , but it has no effect. 我认为可能是因为我需要Import-Module WebAdministration ,但它没有任何效果。 Still the same error.. As already the second step doenst work i cant continue.. 仍然是同样的错误..由于第二步已经完成,我无法继续..

Any ideas? 有任何想法吗?

The Get-IISConfigCollection cmdlet gets a ConfigurationCollection object from either a ConfigurationSection or a ConfigurationElement . Get-IISConfigCollection cmdlet从ConfigurationSectionConfigurationElement获取一个ConfigurationCollection对象。

It is advisable to not to assign this value to a parameter and pass it in the pipeline to the next cmdlet since Windows PowerShell cannot interpret this object. 由于Windows PowerShell无法解释此对象,因此建议不要将此值分配给参数并将其在管道中传递给下一个cmdlet。 This is due to the fact that ConfigurationCollection implements IEnumarable and the pipeline processor enumrates each single element when used this way. 这是由于ConfigurationCollection实现IEnumarable,并且当使用这种方式时,管道处理器会枚举每个单个元素。 Instead either pass the whole Get-IISConfigCollection cmdlet in the pipeline or pass it as a parameter. 而是在管道中传递整个Get-IISConfigCollection cmdlet或将其作为参数传递。

Example: 例:

$ScriptBlock= { Get-IISConfigSection -SectionPath "system.applicationHost/applicationPools" | Get-IISConfigCollection }
$SitesCollection = Invoke-Command -ComputerName $s -ScriptBlock $ScriptBlock

Suggestion : You can use Enter-PsSession command to enter in Remote Computer and then you can run commands over there. 建议 :您可以使用Enter-PsSession命令在“远程计算机”中输入,然后可以在该计算机上运行命令。

Enter-PSSession -ComputerName "Remote-Computer-name" Enter-PSSession -ComputerName“远程计算机名称”

** your Commands like local. **您的命令就像本地命令一样。

Now i found the solution for my problem, thanks to @Kirill Pashkov who led me in the right direction! 现在,由于@Kirill Pashkov带领我朝着正确的方向前进,我找到了解决问题的方法!

It now looks like this: 现在看起来像这样:

$s = "chvmes01"
$AppPool = "DefaultAppPool"       

$command1 = {
param($AppPool)
Get-IISConfigSection -SectionPath "system.applicationHost/applicationPools" |
Get-IISConfigCollection |
Get-IISConfigCollectionElement -ConfigAttribute @{"name" = $AppPool} |
Get-IISConfigElement -ChildElementName "recycling"
}

$recycling = Invoke-Command -ComputerName $s -ScriptBlock $command1 -ArgumentList $AppPool

$flags = Invoke-Command -ComputerName $s -ScriptBlock {param($recycling)$recycling.RawAttributes.logEventOnRecycle} -ArgumentList $recycling

$command2 = {
    param($flags)
    @{
    'Time'           = [bool]($flags -band 1)     # Specific Time
    'Requests'       = [bool]($flags -band 2)     # Request Limit Exceeded
    'Schedule'       = [bool]($flags -band 4)     # Regular Time Interval
    'Memory'         = [bool]($flags -band 8)     # Virtual Memory Limit Exceeded
    'IsapiUnhealthy' = [bool]($flags -band 16)    # Isapi Reported Unhealthy
    'OnDemand'       = [bool]($flags -band 32)    # Manual Recycle
    'ConfigChange'   = [bool]($flags -band 64)    # Application Pool Configuration Changed
    'PrivateMemory'  = [bool]($flags -band 128)   # Private Memory Limit Exceeded
    }
}

$onRecycleLog = Invoke-Command -ComputerName $s -ScriptBlock $command2 -ArgumentList $flags

Finally i got the right output saved in the $onRecycleLog Variable which looks like this: 最后,我将正确的输出保存在$ onRecycleLog变量中,如下所示: 在此处输入图片说明

Thank you Kirill for your support! 谢谢基里尔的支持!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM