Thursday 23 July 2015

PowerShell IIS Web Farm Administration


If you work in an environment where your responsible for an IIS Web farm, your role can revolve around dozens of administrative tasks. If your dealing with 10's of web servers in your web farm it can be time consuming to manually maintain and support your servers. Such as configuring application pools, changing web.config settings or creating virtual directories etc.

With the advent of Powershell this has made the tasks faster and more accurate when implementing web server changes.

Below is a series of Powershell scripts I written to automate the tasks of maintaining web servers.



Backup a File on All Web Servers


#Backup a File on All Web Servers 
$Source = "C:\inetpub\xyz.asp"
$BackupFile = "C:\inetpub\zxc.asp-BACKUP"

$servers='MyWebServer01','MyWebServer02','MyWebServer03','MyWebServer04','MyWebServer05'

$Username  = read-Host  "Enter your Windows Login: "
$Password  = read-Host -AsSecureString   "Windows Server Password:"
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

ForEach ($server in $servers)
{
       invoke-command -ComputerName $server -Credential $Cred -ScriptBlock 
       {
          import-module -Name webadministration
          Copy-Item -Path $Source  -Destination  $BackupFile                             
       }
}


Backup All Web Config Files
# Backs up all the Web config files that have a connection string on the Web Server $BackupFile = "-BACKUP" $Files =  
'C:\inetpub\MyWebSite\MyFolder01\Web.Config'
' C:\inetpub\MyWebSite\MyFolder02\Web.Config'
' C:\inetpub\MyWebSite\MyFolder03\Web.config',
' C:\inetpub\MyWebSite\MyFolder04\Web.config',
' C:\inetpub\MyWebSite\MyFolder05\Web.config',
' C:\inetpub\MyWebSite\MyFolder06\Web.config',
' C:\inetpub\MyWebSite\MyFolder07\Web.config',
' C:\inetpub\MyWebSite\MyFolder08\Web.config'
' C:\inetpub\MyWebSite\MyFolder09\Web.config' 

ForEach ($server in $servers)
{   
  Write-host $Server$BackupFile  
  Copy-Item -Path $server  -Destination  $Server$BackupFile        
}




Change Part of Connection Strings In Web Config Files
$Files =  
'C:\inetpub\MyWebSite\MyFolder01\Web.Config'
' C:\inetpub\MyWebSite\MyFolder02\Web.Config'
' C:\inetpub\MyWebSite\MyFolder03\Web.config',
' C:\inetpub\MyWebSite\MyFolder04\Web.config',
' C:\inetpub\MyWebSite\MyFolder05\Web.config',
' C:\inetpub\MyWebSite\MyFolder06\Web.config',
' C:\inetpub\MyWebSite\MyFolder07\Web.config',
' C:\inetpub\MyWebSite\MyFolder08\Web.config'
' C:\inetpub\MyWebSite\MyFolder09\Web.config' 

  
ForEach ($File in $Files)
{
 write-host 'Processing Server: '   $File 
 (Get-Content -Path $File) | ForEach-Object {$_ -replace 'MyOldSQLINSTANCE, 1234', 'MyNewSQLINSTANCE, 5678' } | Set-Content $File
}
write-host ' Finished'




Get All Web Server Names
$servers = 'MyWebServer01','MyWebServer02','MyWebServer03',' MyWebServer04','MyWebServer05' set-item WSMan:\localhost\Client\TrustedHosts -Value "MyWebServer01, MyWebServer02, MyWebServer03, MyWebServer04, MyWebServer05" set-executionpolicy remotesigned $Username = read-Host "Enter Web Server Login" $Password = read-Host -AsSecureString "Server Password" $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$password ForEach ($server in $servers) { invoke-command -ComputerName $server -Credential $Cred -ScriptBlock 
    {
   
     import-module -Name webadministration
     Get-WebConfiguration -Filter "/system.webServer/httpProtocol/customHeaders/Add/." 
     -Location "C:\Windows\System32\inetsrv\config" | Where-Object { $_.name -eq 
     "X-serverName" } | Select-Object value
                        
    }
}

 

Get WebSite Application Pool
$servers ='MyWebServer01','MyWebServer02','MyWebServer03','MyWebServer04','MyWebServer05' $Username = read-Host "Enter Server Login" $Password = read-Host -AsSecureString "Server Password" $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password ForEach ($server in $servers) { invoke-command -ComputerName $server -Credential $Cred -ScriptBlock 
       {
   
         # Load IIS module:
         Import-Module WebAdministration
         # Set a name of the site we want to recycle the pool                  
         $site = "www.myWebSite.com/MyVirtualFolder/"
         # Get pool name by the site name:
         $pool=(Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool
         write-host $using:server , $Site,  $pool  -f Cyan                        
       
       }
    }

  

Search File Contents on All Web Servers
set-item WSMan:\localhost\Client\TrustedHosts Value "MyWebServer01, MyWebServer02, MyWebServer03, MyWebServer04, MyWebServer05" set-executionpolicy remotesigned $servers = MyWebServer01','MyWebServer02',' MyWebServer03',' MyWebServer04','MyWebServer05' $Username = read-Host "Enter your Windows Login: " $Password = read-Host -AsSecureString "Windows Server Password:" $file = "C:\inetpub\MyWebsite\MyPhysicalFolder\web.config" #File to search#> $SearchString = "add type" #Text to find in file#> $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$password ForEach ($server in $servers) { invoke-command -ComputerName $server -Credential $Cred -ScriptBlock 
       {
   
        import-module -Name webadministration
        Get-Content -Path $using:file  | WHERE { $_ -Match $using:SearchString }
                        
       }

    }
____