Showing posts with label IIS. Show all posts
Showing posts with label IIS. Show all posts

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

    }
____

Tuesday, 25 September 2012

How to capture Memory Dumps of slow performing Url requests using Debug Diagnostic 1.2.



You may come across scenarios when end users report intermittent performance degradation of your web application or web site but you have been unable to identify the cause.

One technique of capturing performance issues is to use the Microsoft trouble shooting tool






Action Plan

1. If you haven’t already, please download and install DebugDiag 1.2 onto the target web server:


http://www.microsoft.com/en-us/download/details.aspx?id=26798
2. Open up DebugDiag from the start menu.
3. When it launches the new rule wizard should automatically start, if it doesn’t then click the Add Rule button on the Rules tab.
4. In the Select Rule Type page select the second radio button named Performance and click Next.







5. Select Http Response Times and click next.



6. Now click the Add Url button. This opens the Properties of URL to monitor dialog box. Leave the top radio button selected, to use ETW, then in the Partial URL segment to match text box type in "/MyVirtualDirectory/" without quotes, then change the timeout after value to 20 seconds as shown in the screenshot below, then click OK.

7.





8. Back on the Select URLs to monitor wizard page click Next to continue.
9. Click the Add Dump Target button. Change the Target Type dropdown menu to Web Application Pool, then select the application pool that hosts your Web application\Web site, then click OK.






 



10. Click the Add Dump Target button again. Change the Target Type dropdown menu to Web Application Pool, then select your application pool which hosts your front end ASPX pages. We want to dump both the client and the web service processes when there is a delay so we’ve added two dump targets to this rule. Simply click Next to continue.


11. Change the generate a UserDump every value to 5 seconds, change the stop after generating value from 10 to 6 user dumps, then in the bottom section of the "Configure UserDump Series" wizard page, please select the radio button to "Collect Full UserDumps", like in the screenshot below. Then click Next.



 



12. Note, and change as needed, the location of where the Userdump files will get written to when the triggers fire and dumps are collected. Then click next.


13. Leave the Activate the rule now radio button selected and click Finish.

 

At this stage DebugDiag is attached to the application pool hosting your web application\web site. Any time one of those requests execute longer than 20 seconds then DebugDiag will generate full user dumps of that app pool, wait 5 seconds and dump the same two processes again.

When we have those dumps collected, then we can verify that the app pool still shows the ASPX request waiting and then we should be able to figure out what is causing the delay in the request.