在foreach循环中的变量之后缺少'in'

时间:2017-12-12 18:49:58

标签: asp.net powershell iis foreach application-pool

我正在尝试执行以下powershell脚本以获取应用程序池状态。但是我得到了以下错误。有人可以在这里帮助我,我错过了吗?

代码:

import-module webadministration
$applicationPools = Get-ChildItem IIS:\AppPools
ForEach ($_.name in $applicationPools)
{
$appPool = $_.name
$appmemversion = get-ItemProperty "IIS:\AppPools\$appPool" ManagedRuntimeVersion.value
$appmem = get-ItemProperty "IIS:\AppPools\$appPool" recycling.periodicrestart.privateMemory.value
$apptimeinv = get-ItemProperty "IIS:\AppPools\$appPool" recycling.periodicRestart.time | select-object value
$appsettime = get-ItemProperty "IIS:\AppPools\$appPool" Recycling.periodicRestart.schedule.collection[0].value | select-object value 
Write-Output "$appPool,$appmemversion,$appmem,$apptimeinv,$appsettime," 
}

错误:

+ ForEach ($_.name in $applicationPools)
+            ~
Missing 'in' after variable in foreach loop.
At C:\Users\JoyMathew\Desktop\App.ps1:9 char:38
+ ForEach ($_.name in $applicationPools)
+                                      ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingInInForeach

Regads, 喜悦

2 个答案:

答案 0 :(得分:1)

这就是我的意思:

<?php phpinfo();

我通过使用.dotted not notation获取了Get-ItemProperty返回的对象的选择...这样,如果其中一个项为null,则输出中将为null。

答案 1 :(得分:0)

import-module webadministration
$applicationPools = Get-ChildItem IIS:\AppPools
ForEach ($Name in $applicationPools)
{
    $appPool = $Name
    $appmemversion = get-ItemProperty "IIS:\AppPools\$appPool" ManagedRuntimeVersion.value
    $appmem = get-ItemProperty "IIS:\AppPools\$appPool" recycling.periodicrestart.privateMemory.value
    $apptimeinv = get-ItemProperty "IIS:\AppPools\$appPool" recycling.periodicRestart.time | select-object value
    $appsettime = get-ItemProperty "IIS:\AppPools\$appPool" Recycling.periodicRestart.schedule.collection[0].value | select-object value 
    Write-Output "$appPool,$appmemversion,$appmem,$apptimeinv,$appsettime," 
}

# OR

import-module webadministration
$applicationPools = Get-ChildItem IIS:\AppPools
$applicationPools | ForEach-Object {
    $appPool = $_.name
    $appmemversion = get-ItemProperty "IIS:\AppPools\$appPool" ManagedRuntimeVersion.value
    $appmem = get-ItemProperty "IIS:\AppPools\$appPool" recycling.periodicrestart.privateMemory.value
    $apptimeinv = get-ItemProperty "IIS:\AppPools\$appPool" recycling.periodicRestart.time | select-object value
    $appsettime = get-ItemProperty "IIS:\AppPools\$appPool" Recycling.periodicRestart.schedule.collection[0].value | select-object value 
    Write-Output "$appPool,$appmemversion,$appmem,$apptimeinv,$appsettime," 
}