1

I am trying to restart multiple app pools from multiple servers.

Example:- Server file - Server1,Server2; AppPool file - AppPool1,AppPool2; Server 1 should check AppPool1 and AppPool2. Server 2 should check and restart AppPool1 and AppPool2.

$Fetch_Details_AppPools = [string[]](Get-Content D:\Users\VRao\Desktop\AUtomation\AppPoolName.csv)
$Fetch_Details_Server = [string[]](Get-Content D:\Users\VRao\Desktop\AUtomation\ServerName.txt)

foreach ($i in $Fetch_Details_Server) {
    foreach ($j in $i.$Fetch_Details_AppPools) {
        Invoke-Command -ComputerName $i -ScriptBlock {
            Import-Module WebAdministration;
            If ((Get-WebAppPoolState(@using:j)).Value -eq "Stopped") {
                write-host("The App pool $using:j is getting started")
                Restart-WebAppPool -Name "$using:j"
            }
            else {
                Write-Host"$using:j is already in start state"
            }
            write-host("The App pool $using:j is started in $using:i")
        }
    }
}

Any errors or help on this?

venkatesh
  • 151
  • 13
  • `Invoke-Command` script blocks don't automatically see variables of surrounding scopes. Try: `Restart-WebAppPool -Name "$using:j"` – zett42 Nov 08 '21 at 16:12
  • Does this answer your question? [How do I pass variables with the Invoke-Command cmdlet?](https://stackoverflow.com/questions/36328690/how-do-i-pass-variables-with-the-invoke-command-cmdlet) – zett42 Nov 08 '21 at 16:14
  • got it, This one is untested file, Do i need to change anything apart from this? – venkatesh Nov 08 '21 at 16:49
  • Please refer to this article https://devblogs.microsoft.com/powershell/how-to-pass-arguments-for-remote-commands/ use param – Bruce Zhang Nov 09 '21 at 03:17
  • @zett42 I have edited, like this ? – venkatesh Nov 09 '21 at 05:59
  • @BruceZhang Our clients will block modules for windows env in future. – venkatesh Nov 09 '21 at 06:00

0 Answers0