Make Your PowerShell Script Environment Aware

In place of hard coding URLs for each environment, I decided to make a single script that is environment aware. Why? Cuts down on the number of scripts that have to be supported for a single development cycle. To make this more dynamic, you could move this to a function script and reference it from all your scripts.

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
	Add-PsSnapin Microsoft.SharePoint.PowerShell

#get config database server
$ConfigDB = Get-SPDatabase | where-Object{$_.Type -eq "Configuration Database"}
$serverName = $ConfigDB.Server.Displayname

#replace this with the web app you want to target.  taco, burrito, nacho...
$webApp = "taco"

#set variable equal to the environment url
$siteURL = switch ($serverName.ToLower())
	"dev_db" {"http://$"}
	"test_db" {"http://test$webApp.sharepointed.fcbt/"}
	"build_db" {"http://build$"}
	"prod_db" {"http://$"}