Friday, April 27, 2012

IIS Application Pool Disable "Ping Enabled" Via Powershell

Probably one of the most frustrating features of debugging Visual Studio is that it will stop debugging an IIS process after a short amount of time and display a dialog box which says:
The web server process that was being debugged has been terminated by Internet Information Services (IIS). This can be avoided by configuring Application Pool ping settings in IIS. See help for further details.
To add insult to injury, it makes you click OK for every application pool that has timed out. In an environment where you have a 4-5 processes attached, this is an annoying waste of time.

It has been long known and pretty well documented on how to fix it in the IIS gui. It is a rather slow process and needs to be automated.

Here is my Powershell function. It will find all of the application pools that start with the specified string and disable the "Ping Enabled" field.

function Disable-Pinging($appPoolNameStartsWith = 'SharePoint - ') {
 Import-Module WebAdministration;
 sl iis:\AppPools;
 Get-ChildItem | ? { $_.Name.StartsWith($appPoolNameStartsWith); } | % { 
  $_.processModel.pingingEnabled = $false; 
 }
 Remove-Module WebAdministration;
}

Thursday, April 19, 2012

Attach Debugger To All Web Processes Using Powershell Inside Visual Studio 2010

Along time ago, when the Visual Studio 2010 Powershell Console was first released I started to play around with the new capabilities and I promptly forgot about it.

I recently ran across a need to have a faster way to attach to all the web processes on my computer. I remembered that I could use Powershell to access everything in Visual Studio 2010. A little digging later I ran across the "$dte" object and remembered that this was the key to accessing everything. Using "Get-Member" several times I quickly dug into
"$dte" and got a list of local processes and found an "Attach" function on the processes object.

Here is my one-line powershell command that attaches to all IIS processes and Development Web Server processes. Doing this doesn't force you to click the warning for attaching to processes that run as different users that normally appear when using the "Attach to Process..." dialog.

$dte.Debugger.LocalProcesses | ? { ($_.Name  -like "*w3wp.exe") -or ($_.Name  -like "*WebDev.WebSe*.exe") }  | % { $_.Attach(); }

I find it as a good practice to know how to undo what I do through scripting. I normally just hit the stop debug button in Visual Studio, but below is how to detach all the debugger from the processes. Perhaps someone will find it useful.

$dte.Debugger.DetachAll();

Monday, April 9, 2012

Using Powershell to update HOSTS file with IP Address of Server

My development requires me to work on a virtual machine, which has 2 sites hosted via host headers. Unfortunately the IP address changes once in a while when I start up the VM. The normal process goes:
  • On my vm, open cmd
  • Run ipconfig
  • Copy the ip address that starts with 192.168.1.
  • On my host machine, open notepad in administrator mode
  • Open the "HOSTS" file
  • Modify the ip addresses
  • Save

The new process with the PowerShell script:
  • Open PowerShell in Administrator mode
  • Run script
It seems like I should be able to run a script from PowerShell in Administrator mode, but I haven't figured that out yet. Perhaps there is a Windows Explorer context menu modification I can use.

The script gets the IP address of the server and gets the the IP address that I need to use. It proceeds to find and update any existing entries in the HOSTS file and then appends the updated line to the end of the file if it isn't in the HOSTS file.
#
# Modify HOSTS file with updated servers IP address
#

function Main {
 $ipAddrs = [System.Net.DNS]::GetHostAddresses("DEMO-VM-D") | ? { $_.IPAddressToString.StartsWith("192.168.1.") };
 $ipAddr = $ipAddrs.IPAddressToString;
 
 Modify-Host "dev.demo.local" $ipAddr;
 Modify-Host "dev.demo.com" $ipAddr;
}

function Modify-Host($hostDomain, $ip = "127.0.0.1") {
 $hostsPath = "$env:windir\System32\drivers\etc\hosts";
 $hosts = Get-Content $hostsPath;
 $escapedHost = $hostDomain -replace "[.]", "\.";
 $exists = $false;
 $hosts = $hosts | % {if ($_ -match "^\s*((\d{1,3}){3}.*?$escapedHost.*)")
                            {$exists = $true;Write-Host "Updating $hostDomain -> $ip"; "$ip`t$hostDomain";} else {$_}};

 if ($exists -eq $false) {
  Write-Host -NoNewline "Adding $hostDomain to the HOSTS file...";
  $hosts += "$ip`t$hostDomain";
 }
 $hosts | Out-File $hostsPath -enc ascii
 
 Write-Host "Done.";
}
& Main;

Wednesday, April 4, 2012

Start Development Web Server Using Powershell Inside Visual Studio 2010

I have a WCF service that is hosted in a Development Web Server (locally) that supports a SharePoint 2010 custom web application. It is a hassle to drive into large solutions to start the server, so I wrote a little script.

Here is the script that starts the Development Web Server (Cassini) inside Visual Studio 2010 using Powershell.

param(
 $rootpath,
 $port = 8383,
 $virtualpath = '/'
)

Kill -name WebDev.WebServer*;

& 'C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0\WebDev.WebServer40.EXE' /path:"$rootpath" /port:$port /vpath:"$virtualpath "


Here is an easy copy and paste line that you can throw into a Powershell console or Package Manager Console inside Visual Studio 2010.

Kill -name WebDev.WebServer*;& 'C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0\WebDev.WebServer40.EXE' /path:"C:\TFS\\Main\Source\" /port:8383 /vpath:"/";