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

Missing 'in' after variable in foreach loop

我正在尝试执行以下 powershell 脚本以获取应用程序池 status.But 我遇到以下错误。有人可以帮我看看我错过了什么吗??

代码:

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, 欢乐

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," 
}

我的意思是:

import-module webadministration
$applicationPools = Get-ChildItem IIS:\AppPools
ForEach ($pool in $applicationPools)
{
$appPool = $pool.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," 
}

我通过对 Get-ItemProperty 返回的对象使用 .dotted not notation 删除了你的 select ... 这样,如果其中一项为 null,你将在输出中得到 null .