Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
We have a requirement to change our application's IIS app pool password everytime it expires. Currently, we have to manually login to each server adn run a snippet of PowerShell code which changes the password.

Here is the code we run on each server on PS:

 Import-Module WebAdministration
 $applicationPools = Get-ChildItem IIS:AppPools | where { $_.processModel.userName -eq "Domain\XXXXX12345" }
  
 foreach($pool in $applicationPools)
 {
     $pool.processModel.userName = "Doma\XXXXX12345"
     $pool.processModel.password = "XXXXXXXXXXXXXXXXX"
     $pool | Set-Item
 }
  
 Write-Host "Application pool passwords updated..." -ForegroundColor Magenta 
 Write-Host "" 
 Read-Host -Prompt "Press Enter to exit"

But is there a way we can do the same for a list of servers/VMs at once instead of having to login to each server, open PowerShell or IIS and manually change it on each server?

Any help would be greatly appreciated!


What I have tried:

Here is the code we run on each server on PS:

 Import-Module WebAdministration
 $applicationPools = Get-ChildItem IIS:AppPools | where { $_.processModel.userName -eq "Domain\XXXXX12345" }
  
 foreach($pool in $applicationPools)
 {
     $pool.processModel.userName = "Doma\XXXXX12345"
     $pool.processModel.password = "XXXXXXXXXXXXXXXXX"
     $pool | Set-Item
 }
  
 Write-Host "Application pool passwords updated..." -ForegroundColor Magenta 
 Write-Host "" 
 Read-Host -Prompt "Press Enter to exit"

But is there a way we can do the same for a list of servers/VMs at once instead of having to login to each server, open PowerShell or IIS and manually change it on each server?
Posted

1 solution

Wrap this in a function (script block). 
<pre lang="PowerShell">
$block = { ... your custom method ... }

Then put the function call in a loop that runs on a list of servers as shown here:
PowerShell
foreach ($Server in $Servers) {
     if (Test-Connection -ComputerName $Server -Quiet -Count 1) {
          Invoke-Command -ComputerName $Server -ScriptBlock { $block }
     }
     else {
          Write-Host "$Server appears to be offline!"
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900