PowerShell Error in SharePoint Server - Cannot Access the Local Farm

I was using a PowerShell script to deploy my SharePoint solution in the development environment for some time now. But when I try to use the same script in a newly configured SharePoint environment I got the following error.

“Get-SPSolution : Cannot access the local farm. Verify that the local farm is properly configured, currently available, and that you have the appropriate permissions to access the database before trying again.”

In my deployment script I have used “Get-SPSolution” command and it seemed like this command was failing due a configuration error in the local farm. So I tried to re-run the SharePoint 2010 Products Configuration Wizard and got the following error.

“Failed to detect if this server is joined to a server farm. Possible reasons for this failure could be that you no longer have the appropriate permissions to the server farm, the database server hosting the server farm is unresponsive, the configuration database in inaccessible or this server has been removed from the server farm.”

When I click “OK” on the popupbox, Database server and Database name were marked as Unknown and “Do not disconnect from this server farm” option was disabled.

Issue in my case:
Executing sp_helpserver in SQL Server management studio of the server gave me a wrong name as the server name and same wrong name as the network name of the server. (You will come across this scenario when the computer hosting SQL Server is renamed).

In addition to the above issue, the domain account I used to do the deployment didn’t have permissions to access the databases, even though it was added as a local administrator account of the server. 

In order to correct the server name in the SQL server, first I used sp_dropserver command. This command removes a server from the list of known remote and linked servers on the local instance of SQL Server.
sp_dropserver 'WrongServer'

Then I used sp_addserver command to define the name of the local instance of SQL Server.  This command actually informs the new computer name to the instance of the SQL Server Database Engine.
sp_addserver 'CorrectServer'

Finally granting DB permission to the domain account solved my issue.