If you are reading this post then chances are that you are already aware of the SharePoint 2013 App Model and especially the SharePoint 2013 Provider Hosted Apps.
A provider-hosted app for SharePoint consists of both an app for SharePoint that is deployed directly to a SharePoint 2013 site and a separately deployed web application. If the provider-hosted web application is an ASP.NET web application, you can use the Office Developer Tools for Visual Studio 2013 to create both components of a provider-hosted app for SharePoint.
Packaging and publishing a SharePoint 2013 provider-hosted app can be a lengthy process. MSDN has a detailed article here explaining the process and steps required to publish a SharePoint provider-hosted App.
The standard process of registering an app on a SharePoint 2013 environment is by using the appregnew.aspx page (http://sitecollection/_layouts/15/appregnew.aspx) and generating a Client Id which can be used to communicate between the SharePoint and the provider-hosted app to establish a high-trust.
However, this approach of generating Client Id can be cumbersome when you have different environments (DTAP street) with-in your organization. I did not want to burden my application administrators by creating complexity of the installation process to generate the Client Id per environment and change it in several places (the .app package, web.config etc.)
Not only this, it also adds a lot of confusion when multiple developers are working together and all of them have to generate their own Client Ids for respective machines. The checked-in web.config, app packages in source controller can really create unnecessary confusion.
A similar blog about this process is posted here.
However, PowerShell can come to your rescue to generate the Client Id once and use the same Client Id to register your SharePoint App in different environment. Yes, not only in DTAP street but also various developer machine working together on a product.
The small PowerShell snippet below can automate the whole process of registering an App with a given Client Id and after that installing it in SharePoint.
For those who are interested in details, it makes use of Register-SPAppPrincipal command. This command lets an on-premise or SharePoint Online administrator register an app principal which means you can also use it for Office 365.
$clientID = "74599670-eb74-4348-9e7a-f9dc07c576a2"
$appFile = "C:\Temp\MyApp.app"
$siteCollection = "http://manasbhardwaj.net"
$appName = "My App"
$web = Get-SPWeb -Identity $siteCollection
$realm = Get-SPAuthenticationRealm -ServiceContext $web.Site;
$appIdentifier = $clientID + '@' + $realm;
#Register the App with given ClientId
Register-SPAppPrincipal -DisplayName $appName -NameIdentifier $appIdentifier -Site $web | Out-Null
$app = Import-SPAppPackage -Path $appFile -Site $siteCollection -Source ObjectModel -Confirm:$false
#Install the App
Install-SPApp -Web $siteCollection -Identity $app | Out-Null