Recently while executing a powershell script that creates a Form library in a Sharepoint 2010 site I faced this error "Cannot Open database dbName requested by the login in powershell". The error is shown below:

I tried various troubleshooting steps all the way from google search but I was no where to the solution. I even started powershell window as administrator but had no luck. I passed DB name as switch to the script but again no luck.

I finally went to my SQL Server management studio and connected the instance I was using. I then tried to access my content db of the site where this script was trying to add the form library. I was shocked that I was not able to even access the properties of the db. I tried to assign security role to my login ID but had no luck.

After various troubleshooting steps I finally managed to get it through. Here are the steps I followed:

1. I opened Sql Server Management studio as administrator.
2. I then added by ID through with I was executing Powershell in security roles and granted DB owner rights and security Admin