I am importing a CSV and recycling that site based on the values in the CSV. The csv is dynamically buildup based on the alerts we receive. When I try the following code I receive an values printed and errors
import-csv d:\emscripts\recy\recycle.txt -Header "site","IP","Log" | foreach {
$_.IP
$_.site
$appPool = Get-WmiObject -Authentication PacketPrivacy -Impersonation Impersonate -ComputerName $_.IP -namespace "root/MicrosoftIISv2" -class IIsApplicationPool | Where-Object {$_.Name -eq "W3SVC/AppPools/$($_.site)" }
$appPool.Recycle()
}
*192.168.100.182 Testsite1 You cannot call a method on a null-valued expression. At line:5 char:5 + $appPool.Recycle() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
192.168.100.131 testsite2 You cannot call a method on a null-valued expression. At line:5 char:5 + $appPool.Recycle() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull*
You can see that the values are printed but it is failing to get anything into the $appPool.
I have changed it to the following and it is working as expected.
$RecyleSites = import-csv recycle.txt -Header "site","IP","Log"
foreach ($site in $RecyleSites) {
$site.IP
$site.site
$appPool = Get-WmiObject -Authentication PacketPrivacy -Impersonation Impersonate -ComputerName $site.IP -namespace "root/MicrosoftIISv2" -class IIsApplicationPool | Where-Object {$_.Name -eq "W3SVC/AppPools/$($site.site)" }
$appPool.Recycle()
}
I am trying understand what I am doing wrong in the first set of code ?