2017-12-12 4 views
0

Ich versuche, Powershell-Skript ausführen, um den Status des Anwendungspools zu erhalten. Aber ich bekomme unter Fehler. Kann mir bitte jemand hier helfen was ich vermisse?Fehlende 'in' nach Variable in foreach Schleife

Code:

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

Fehler:

+ 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, Joy

+1

$ _ wird in Pipelines verwendet ... Sie haben die $ -Anwendungspools von der foreach-Schleife getrennt, wenn Sie den Namen $ _. In einen Variablennamen wie $ pool ändern, der Ihr Problem beheben sollte. –

+0

@thomschumacher Sie sollten eine Antwort geben und ein kurzes Beispiel für korrigierten Code geben. – TheMadTechnician

+0

Ich habe versucht, $ ($ _. Name) einzupacken, aber ich bekomme immer noch denselben Fehler. –

Antwort

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

hier ist, was ich meinte:

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

Ich nahm Ihre Auswahl heraus, indem Sie die .dotted nicht-Notation für das Objekt verwenden, das Get-ItemProperty zurückgibt ... Auf diese Weise erhalten Sie null in der Ausgabe, wenn eines der Elemente null ist.

+0

froh, dass ich helfen konnte –